Probleme char input
Dependencies: TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG lvgl_RB FastPWM millis
lv_i18n.c@4:6a3d12663549, 2020-01-26 (annotated)
- Committer:
- becanromain
- Date:
- Sun Jan 26 16:19:54 2020 +0000
- Revision:
- 4:6a3d12663549
- Parent:
- 1:404ee28a0b60
- Child:
- 6:519d681050af
THEME AND FONT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kisvegabor | 1:404ee28a0b60 | 1 | #include "./lv_i18n.h" |
kisvegabor | 1:404ee28a0b60 | 2 | |
kisvegabor | 1:404ee28a0b60 | 3 | |
kisvegabor | 1:404ee28a0b60 | 4 | //////////////////////////////////////////////////////////////////////////////// |
kisvegabor | 1:404ee28a0b60 | 5 | // Define plural operands |
kisvegabor | 1:404ee28a0b60 | 6 | // http://unicode.org/reports/tr35/tr35-numbers.html#Operands |
kisvegabor | 1:404ee28a0b60 | 7 | |
kisvegabor | 1:404ee28a0b60 | 8 | // Integer version, simplified |
kisvegabor | 1:404ee28a0b60 | 9 | |
kisvegabor | 1:404ee28a0b60 | 10 | #define UNUSED(x) (void)(x) |
kisvegabor | 1:404ee28a0b60 | 11 | |
becanromain | 4:6a3d12663549 | 12 | static lv_i18n_phrase_t fr_texts[] = { |
becanromain | 4:6a3d12663549 | 13 | {"M100", "Mesures"}, |
becanromain | 4:6a3d12663549 | 14 | {"M200", "Prélevement"}, |
becanromain | 4:6a3d12663549 | 15 | {"M300", "Paramètres"}, |
becanromain | 4:6a3d12663549 | 16 | {"M400", "Menu 4"}, |
becanromain | 4:6a3d12663549 | 17 | {"M500", "Menu 5"}, |
becanromain | 4:6a3d12663549 | 18 | {"M600", "Menu 6"}, |
becanromain | 4:6a3d12663549 | 19 | {"SM301", "langage"}, |
becanromain | 4:6a3d12663549 | 20 | {"Temps de cycle = %3.3f us", "Temps de cycle = %3.3f us"}, |
becanromain | 4:6a3d12663549 | 21 | {NULL, NULL} // End mark |
becanromain | 4:6a3d12663549 | 22 | }; |
becanromain | 4:6a3d12663549 | 23 | |
becanromain | 4:6a3d12663549 | 24 | static const lv_i18n_lang_t fr_lang = { |
becanromain | 4:6a3d12663549 | 25 | .locale_name = "fr", |
becanromain | 4:6a3d12663549 | 26 | .singulars = fr_texts, |
becanromain | 4:6a3d12663549 | 27 | }; |
becanromain | 4:6a3d12663549 | 28 | |
becanromain | 4:6a3d12663549 | 29 | static lv_i18n_phrase_t en_gb_texts[] = { |
becanromain | 4:6a3d12663549 | 30 | {"M100", "Measures"}, |
becanromain | 4:6a3d12663549 | 31 | {"M200", "Sample"}, |
becanromain | 4:6a3d12663549 | 32 | {"M300", "Setting"}, |
becanromain | 4:6a3d12663549 | 33 | {"M400", "Menu 4"}, |
becanromain | 4:6a3d12663549 | 34 | {"M500", "Menu 5"}, |
becanromain | 4:6a3d12663549 | 35 | {"M600", "Menu 6"}, |
becanromain | 4:6a3d12663549 | 36 | {"SM301", "language"}, |
becanromain | 4:6a3d12663549 | 37 | {"Temps de cycle = %3.3f us", "Cycle time= %3.3f us"}, |
kisvegabor | 1:404ee28a0b60 | 38 | {"cat", "This is a cat"}, |
kisvegabor | 1:404ee28a0b60 | 39 | {NULL, NULL} // End mark |
kisvegabor | 1:404ee28a0b60 | 40 | }; |
kisvegabor | 1:404ee28a0b60 | 41 | |
kisvegabor | 1:404ee28a0b60 | 42 | |
kisvegabor | 1:404ee28a0b60 | 43 | static const lv_i18n_lang_t en_gb_lang = { |
kisvegabor | 1:404ee28a0b60 | 44 | .locale_name = "en-GB", |
kisvegabor | 1:404ee28a0b60 | 45 | .singulars = en_gb_texts, |
kisvegabor | 1:404ee28a0b60 | 46 | }; |
kisvegabor | 1:404ee28a0b60 | 47 | |
kisvegabor | 1:404ee28a0b60 | 48 | const lv_i18n_language_pack_t lv_i18n_language_pack[] = { |
kisvegabor | 1:404ee28a0b60 | 49 | &en_gb_lang, |
kisvegabor | 1:404ee28a0b60 | 50 | &fr_lang, |
kisvegabor | 1:404ee28a0b60 | 51 | NULL // End mark |
kisvegabor | 1:404ee28a0b60 | 52 | }; |
kisvegabor | 1:404ee28a0b60 | 53 | |
kisvegabor | 1:404ee28a0b60 | 54 | //////////////////////////////////////////////////////////////////////////////// |
kisvegabor | 1:404ee28a0b60 | 55 | |
kisvegabor | 1:404ee28a0b60 | 56 | |
kisvegabor | 1:404ee28a0b60 | 57 | // Internal state |
kisvegabor | 1:404ee28a0b60 | 58 | static const lv_i18n_language_pack_t * current_lang_pack; |
kisvegabor | 1:404ee28a0b60 | 59 | static const lv_i18n_lang_t * current_lang; |
kisvegabor | 1:404ee28a0b60 | 60 | |
kisvegabor | 1:404ee28a0b60 | 61 | |
kisvegabor | 1:404ee28a0b60 | 62 | /** |
kisvegabor | 1:404ee28a0b60 | 63 | * Reset internal state. For testing. |
kisvegabor | 1:404ee28a0b60 | 64 | */ |
kisvegabor | 1:404ee28a0b60 | 65 | void __lv_i18n_reset(void) |
kisvegabor | 1:404ee28a0b60 | 66 | { |
kisvegabor | 1:404ee28a0b60 | 67 | current_lang_pack = NULL; |
kisvegabor | 1:404ee28a0b60 | 68 | current_lang = NULL; |
kisvegabor | 1:404ee28a0b60 | 69 | } |
kisvegabor | 1:404ee28a0b60 | 70 | |
kisvegabor | 1:404ee28a0b60 | 71 | /** |
kisvegabor | 1:404ee28a0b60 | 72 | * Set the languages for internationalization |
kisvegabor | 1:404ee28a0b60 | 73 | * @param langs pointer to the array of languages. (Last element has to be `NULL`) |
kisvegabor | 1:404ee28a0b60 | 74 | */ |
kisvegabor | 1:404ee28a0b60 | 75 | int lv_i18n_init(const lv_i18n_language_pack_t * langs) |
kisvegabor | 1:404ee28a0b60 | 76 | { |
kisvegabor | 1:404ee28a0b60 | 77 | if(langs == NULL) return -1; |
kisvegabor | 1:404ee28a0b60 | 78 | if(langs[0] == NULL) return -1; |
kisvegabor | 1:404ee28a0b60 | 79 | |
kisvegabor | 1:404ee28a0b60 | 80 | current_lang_pack = langs; |
kisvegabor | 1:404ee28a0b60 | 81 | current_lang = langs[0]; /*Automatically select the first language*/ |
kisvegabor | 1:404ee28a0b60 | 82 | return 0; |
kisvegabor | 1:404ee28a0b60 | 83 | } |
kisvegabor | 1:404ee28a0b60 | 84 | |
kisvegabor | 1:404ee28a0b60 | 85 | /** |
kisvegabor | 1:404ee28a0b60 | 86 | * Change the localization (language) |
kisvegabor | 1:404ee28a0b60 | 87 | * @param l_name name of the translation locale to use. E.g. "en-GB" |
kisvegabor | 1:404ee28a0b60 | 88 | */ |
kisvegabor | 1:404ee28a0b60 | 89 | int lv_i18n_set_locale(const char * l_name) |
kisvegabor | 1:404ee28a0b60 | 90 | { |
kisvegabor | 1:404ee28a0b60 | 91 | if(current_lang_pack == NULL) return -1; |
kisvegabor | 1:404ee28a0b60 | 92 | |
kisvegabor | 1:404ee28a0b60 | 93 | uint16_t i; |
kisvegabor | 1:404ee28a0b60 | 94 | |
kisvegabor | 1:404ee28a0b60 | 95 | for(i = 0; current_lang_pack[i] != NULL; i++) { |
kisvegabor | 1:404ee28a0b60 | 96 | // Found -> finish |
kisvegabor | 1:404ee28a0b60 | 97 | if(strcmp(current_lang_pack[i]->locale_name, l_name) == 0) { |
kisvegabor | 1:404ee28a0b60 | 98 | current_lang = current_lang_pack[i]; |
kisvegabor | 1:404ee28a0b60 | 99 | return 0; |
kisvegabor | 1:404ee28a0b60 | 100 | } |
kisvegabor | 1:404ee28a0b60 | 101 | } |
kisvegabor | 1:404ee28a0b60 | 102 | |
kisvegabor | 1:404ee28a0b60 | 103 | return -1; |
kisvegabor | 1:404ee28a0b60 | 104 | } |
kisvegabor | 1:404ee28a0b60 | 105 | |
kisvegabor | 1:404ee28a0b60 | 106 | |
kisvegabor | 1:404ee28a0b60 | 107 | static const char * __lv_i18n_get_text_core(lv_i18n_phrase_t * trans, const char * msg_id) |
kisvegabor | 1:404ee28a0b60 | 108 | { |
kisvegabor | 1:404ee28a0b60 | 109 | uint16_t i; |
kisvegabor | 1:404ee28a0b60 | 110 | for(i = 0; trans[i].msg_id != NULL; i++) { |
kisvegabor | 1:404ee28a0b60 | 111 | if(strcmp(trans[i].msg_id, msg_id) == 0) { |
kisvegabor | 1:404ee28a0b60 | 112 | /*The msg_id has found. Check the translation*/ |
kisvegabor | 1:404ee28a0b60 | 113 | if(trans[i].translation) return trans[i].translation; |
kisvegabor | 1:404ee28a0b60 | 114 | } |
kisvegabor | 1:404ee28a0b60 | 115 | } |
kisvegabor | 1:404ee28a0b60 | 116 | |
kisvegabor | 1:404ee28a0b60 | 117 | return NULL; |
kisvegabor | 1:404ee28a0b60 | 118 | } |
kisvegabor | 1:404ee28a0b60 | 119 | |
kisvegabor | 1:404ee28a0b60 | 120 | |
kisvegabor | 1:404ee28a0b60 | 121 | /** |
kisvegabor | 1:404ee28a0b60 | 122 | * Get the translation from a message ID |
kisvegabor | 1:404ee28a0b60 | 123 | * @param msg_id message ID |
kisvegabor | 1:404ee28a0b60 | 124 | * @return the translation of `msg_id` on the set local |
kisvegabor | 1:404ee28a0b60 | 125 | */ |
kisvegabor | 1:404ee28a0b60 | 126 | const char * lv_i18n_get_text(const char * msg_id) |
kisvegabor | 1:404ee28a0b60 | 127 | { |
kisvegabor | 1:404ee28a0b60 | 128 | if(current_lang == NULL) return msg_id; |
kisvegabor | 1:404ee28a0b60 | 129 | |
kisvegabor | 1:404ee28a0b60 | 130 | const lv_i18n_lang_t * lang = current_lang; |
kisvegabor | 1:404ee28a0b60 | 131 | const void * txt; |
kisvegabor | 1:404ee28a0b60 | 132 | |
kisvegabor | 1:404ee28a0b60 | 133 | // Search in current locale |
kisvegabor | 1:404ee28a0b60 | 134 | if(lang->singulars != NULL) { |
kisvegabor | 1:404ee28a0b60 | 135 | txt = __lv_i18n_get_text_core(lang->singulars, msg_id); |
kisvegabor | 1:404ee28a0b60 | 136 | if (txt != NULL) return txt; |
kisvegabor | 1:404ee28a0b60 | 137 | } |
kisvegabor | 1:404ee28a0b60 | 138 | |
kisvegabor | 1:404ee28a0b60 | 139 | // Try to fallback |
kisvegabor | 1:404ee28a0b60 | 140 | if(lang == current_lang_pack[0]) return msg_id; |
kisvegabor | 1:404ee28a0b60 | 141 | lang = current_lang_pack[0]; |
kisvegabor | 1:404ee28a0b60 | 142 | |
kisvegabor | 1:404ee28a0b60 | 143 | // Repeat search for default locale |
kisvegabor | 1:404ee28a0b60 | 144 | if(lang->singulars != NULL) { |
kisvegabor | 1:404ee28a0b60 | 145 | txt = __lv_i18n_get_text_core(lang->singulars, msg_id); |
kisvegabor | 1:404ee28a0b60 | 146 | if (txt != NULL) return txt; |
kisvegabor | 1:404ee28a0b60 | 147 | } |
kisvegabor | 1:404ee28a0b60 | 148 | |
kisvegabor | 1:404ee28a0b60 | 149 | return msg_id; |
kisvegabor | 1:404ee28a0b60 | 150 | } |
kisvegabor | 1:404ee28a0b60 | 151 | |
kisvegabor | 1:404ee28a0b60 | 152 | /** |
kisvegabor | 1:404ee28a0b60 | 153 | * Get the translation from a message ID and apply the language's plural rule to get correct form |
kisvegabor | 1:404ee28a0b60 | 154 | * @param msg_id message ID |
kisvegabor | 1:404ee28a0b60 | 155 | * @param num an integer to select the correct plural form |
kisvegabor | 1:404ee28a0b60 | 156 | * @return the translation of `msg_id` on the set local |
kisvegabor | 1:404ee28a0b60 | 157 | */ |
kisvegabor | 1:404ee28a0b60 | 158 | const char * lv_i18n_get_text_plural(const char * msg_id, int32_t num) |
kisvegabor | 1:404ee28a0b60 | 159 | { |
kisvegabor | 1:404ee28a0b60 | 160 | if(current_lang == NULL) return msg_id; |
kisvegabor | 1:404ee28a0b60 | 161 | |
kisvegabor | 1:404ee28a0b60 | 162 | const lv_i18n_lang_t * lang = current_lang; |
kisvegabor | 1:404ee28a0b60 | 163 | const void * txt; |
kisvegabor | 1:404ee28a0b60 | 164 | lv_i18n_plural_type_t ptype; |
kisvegabor | 1:404ee28a0b60 | 165 | |
kisvegabor | 1:404ee28a0b60 | 166 | // Search in current locale |
kisvegabor | 1:404ee28a0b60 | 167 | if(lang->locale_plural_fn != NULL) { |
kisvegabor | 1:404ee28a0b60 | 168 | ptype = lang->locale_plural_fn(num); |
kisvegabor | 1:404ee28a0b60 | 169 | |
kisvegabor | 1:404ee28a0b60 | 170 | if(lang->plurals[ptype] != NULL) { |
kisvegabor | 1:404ee28a0b60 | 171 | txt = __lv_i18n_get_text_core(lang->plurals[ptype], msg_id); |
kisvegabor | 1:404ee28a0b60 | 172 | if (txt != NULL) return txt; |
kisvegabor | 1:404ee28a0b60 | 173 | } |
kisvegabor | 1:404ee28a0b60 | 174 | } |
kisvegabor | 1:404ee28a0b60 | 175 | |
kisvegabor | 1:404ee28a0b60 | 176 | // Try to fallback |
kisvegabor | 1:404ee28a0b60 | 177 | if(lang == current_lang_pack[0]) return msg_id; |
kisvegabor | 1:404ee28a0b60 | 178 | lang = current_lang_pack[0]; |
kisvegabor | 1:404ee28a0b60 | 179 | |
kisvegabor | 1:404ee28a0b60 | 180 | // Repeat search for default locale |
kisvegabor | 1:404ee28a0b60 | 181 | if(lang->locale_plural_fn != NULL) { |
kisvegabor | 1:404ee28a0b60 | 182 | ptype = lang->locale_plural_fn(num); |
kisvegabor | 1:404ee28a0b60 | 183 | |
kisvegabor | 1:404ee28a0b60 | 184 | if(lang->plurals[ptype] != NULL) { |
kisvegabor | 1:404ee28a0b60 | 185 | txt = __lv_i18n_get_text_core(lang->plurals[ptype], msg_id); |
kisvegabor | 1:404ee28a0b60 | 186 | if (txt != NULL) return txt; |
kisvegabor | 1:404ee28a0b60 | 187 | } |
kisvegabor | 1:404ee28a0b60 | 188 | } |
kisvegabor | 1:404ee28a0b60 | 189 | |
kisvegabor | 1:404ee28a0b60 | 190 | return msg_id; |
kisvegabor | 1:404ee28a0b60 | 191 | } |
kisvegabor | 1:404ee28a0b60 | 192 | |
kisvegabor | 1:404ee28a0b60 | 193 | /** |
kisvegabor | 1:404ee28a0b60 | 194 | * Get the name of the currently used locale. |
kisvegabor | 1:404ee28a0b60 | 195 | * @return name of the currently used locale. E.g. "en-GB" |
kisvegabor | 1:404ee28a0b60 | 196 | */ |
kisvegabor | 1:404ee28a0b60 | 197 | const char * lv_i18n_get_current_locale(void) |
kisvegabor | 1:404ee28a0b60 | 198 | { |
kisvegabor | 1:404ee28a0b60 | 199 | if(!current_lang) return NULL; |
kisvegabor | 1:404ee28a0b60 | 200 | return current_lang->locale_name; |
kisvegabor | 1:404ee28a0b60 | 201 | } |