char input

Dependencies:   TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG lvgl_RB FastPWM millis

Committer:
becanromain
Date:
Fri Feb 28 00:05:30 2020 +0000
Revision:
6:21e96e439978
Parent:
4:6a3d12663549

        

Who changed what in which revision?

UserRevisionLine numberNew 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 6:21e96e439978 13 {"M100", "1 Etat"},
becanromain 6:21e96e439978 14 {"M101", "1.1 Etat d'avancement"},
becanromain 6:21e96e439978 15 {"M102", "1.2 Informations générales"},
becanromain 6:21e96e439978 16 {"M200", "2 Je prélève"},
becanromain 6:21e96e439978 17 {"M201", "2.1 Séquence unique"},
becanromain 6:21e96e439978 18 {"M202", "2.2 Séquences répétées"},
becanromain 6:21e96e439978 19 {"M203", "2.3 Séquences prédéfinies"},
becanromain 6:21e96e439978 20 {"M300", "3 Je vérifie"},
becanromain 6:21e96e439978 21 {"M301", "3.1 Etat des vérifications"},
becanromain 6:21e96e439978 22 {"M302", "3.2 Volume"},
becanromain 6:21e96e439978 23 {"M303", "3.3 Température 1"},
becanromain 6:21e96e439978 24 {"M304", "3.4 Température 2"},
becanromain 6:21e96e439978 25 {"M305", "3.5 Température 3"},
becanromain 6:21e96e439978 26 {"M306", "3.6 Pression 1"},
becanromain 6:21e96e439978 27 {"M307", "3.7 Pression 2"},
becanromain 6:21e96e439978 28 {"M308", "3.8 Pression 2"},
becanromain 6:21e96e439978 29 {"M309", "3.9 Humidité relative"},
becanromain 6:21e96e439978 30 {"M400", "4 Je supervise"},
becanromain 6:21e96e439978 31 {"M401", "4.1 Choix de langue"},
becanromain 6:21e96e439978 32 {"M402", "4.2 Droits d'accès"},
becanromain 6:21e96e439978 33 {"M403", "4.3 Exigences métrologiques"},
becanromain 6:21e96e439978 34 {"M404", "4.4 Bibliothèque des étalons "},
becanromain 6:21e96e439978 35 {"M405", "4.5 Paramètres généraux"},
becanromain 6:21e96e439978 36 {"M500", "5 Je consulte les archives"},
becanromain 6:21e96e439978 37 {"M501", "5.1 Prélevements"},
becanromain 6:21e96e439978 38 {"M502", "5.2 Vérifications / ajustages"},
becanromain 6:21e96e439978 39 {"M503", "5.2 Vérifications / ajustages"},
becanromain 6:21e96e439978 40 {"M600", "6 Constructeur"},
becanromain 6:21e96e439978 41 {"M601", "6.1 Régulation"},
becanromain 4:6a3d12663549 42 {"Temps de cycle = %3.3f us", "Temps de cycle = %3.3f us"},
becanromain 4:6a3d12663549 43 {NULL, NULL} // End mark
becanromain 4:6a3d12663549 44 };
becanromain 4:6a3d12663549 45
becanromain 4:6a3d12663549 46 static const lv_i18n_lang_t fr_lang = {
becanromain 4:6a3d12663549 47 .locale_name = "fr",
becanromain 4:6a3d12663549 48 .singulars = fr_texts,
becanromain 4:6a3d12663549 49 };
becanromain 4:6a3d12663549 50
becanromain 4:6a3d12663549 51 static lv_i18n_phrase_t en_gb_texts[] = {
becanromain 4:6a3d12663549 52 {"M100", "Measures"},
becanromain 4:6a3d12663549 53 {"M200", "Sample"},
becanromain 4:6a3d12663549 54 {"M300", "Setting"},
becanromain 4:6a3d12663549 55 {"M400", "Menu 4"},
becanromain 4:6a3d12663549 56 {"M500", "Menu 5"},
becanromain 4:6a3d12663549 57 {"M600", "Menu 6"},
becanromain 4:6a3d12663549 58 {"SM301", "language"},
becanromain 4:6a3d12663549 59 {"Temps de cycle = %3.3f us", "Cycle time= %3.3f us"},
kisvegabor 1:404ee28a0b60 60 {"cat", "This is a cat"},
kisvegabor 1:404ee28a0b60 61 {NULL, NULL} // End mark
kisvegabor 1:404ee28a0b60 62 };
kisvegabor 1:404ee28a0b60 63
kisvegabor 1:404ee28a0b60 64
kisvegabor 1:404ee28a0b60 65 static const lv_i18n_lang_t en_gb_lang = {
kisvegabor 1:404ee28a0b60 66 .locale_name = "en-GB",
kisvegabor 1:404ee28a0b60 67 .singulars = en_gb_texts,
kisvegabor 1:404ee28a0b60 68 };
kisvegabor 1:404ee28a0b60 69
kisvegabor 1:404ee28a0b60 70 const lv_i18n_language_pack_t lv_i18n_language_pack[] = {
kisvegabor 1:404ee28a0b60 71 &en_gb_lang,
kisvegabor 1:404ee28a0b60 72 &fr_lang,
kisvegabor 1:404ee28a0b60 73 NULL // End mark
kisvegabor 1:404ee28a0b60 74 };
kisvegabor 1:404ee28a0b60 75
kisvegabor 1:404ee28a0b60 76 ////////////////////////////////////////////////////////////////////////////////
kisvegabor 1:404ee28a0b60 77
kisvegabor 1:404ee28a0b60 78
kisvegabor 1:404ee28a0b60 79 // Internal state
kisvegabor 1:404ee28a0b60 80 static const lv_i18n_language_pack_t * current_lang_pack;
kisvegabor 1:404ee28a0b60 81 static const lv_i18n_lang_t * current_lang;
kisvegabor 1:404ee28a0b60 82
kisvegabor 1:404ee28a0b60 83
kisvegabor 1:404ee28a0b60 84 /**
kisvegabor 1:404ee28a0b60 85 * Reset internal state. For testing.
kisvegabor 1:404ee28a0b60 86 */
kisvegabor 1:404ee28a0b60 87 void __lv_i18n_reset(void)
kisvegabor 1:404ee28a0b60 88 {
kisvegabor 1:404ee28a0b60 89 current_lang_pack = NULL;
kisvegabor 1:404ee28a0b60 90 current_lang = NULL;
kisvegabor 1:404ee28a0b60 91 }
kisvegabor 1:404ee28a0b60 92
kisvegabor 1:404ee28a0b60 93 /**
kisvegabor 1:404ee28a0b60 94 * Set the languages for internationalization
kisvegabor 1:404ee28a0b60 95 * @param langs pointer to the array of languages. (Last element has to be `NULL`)
kisvegabor 1:404ee28a0b60 96 */
kisvegabor 1:404ee28a0b60 97 int lv_i18n_init(const lv_i18n_language_pack_t * langs)
kisvegabor 1:404ee28a0b60 98 {
kisvegabor 1:404ee28a0b60 99 if(langs == NULL) return -1;
kisvegabor 1:404ee28a0b60 100 if(langs[0] == NULL) return -1;
kisvegabor 1:404ee28a0b60 101
kisvegabor 1:404ee28a0b60 102 current_lang_pack = langs;
kisvegabor 1:404ee28a0b60 103 current_lang = langs[0]; /*Automatically select the first language*/
kisvegabor 1:404ee28a0b60 104 return 0;
kisvegabor 1:404ee28a0b60 105 }
kisvegabor 1:404ee28a0b60 106
kisvegabor 1:404ee28a0b60 107 /**
kisvegabor 1:404ee28a0b60 108 * Change the localization (language)
kisvegabor 1:404ee28a0b60 109 * @param l_name name of the translation locale to use. E.g. "en-GB"
kisvegabor 1:404ee28a0b60 110 */
kisvegabor 1:404ee28a0b60 111 int lv_i18n_set_locale(const char * l_name)
kisvegabor 1:404ee28a0b60 112 {
kisvegabor 1:404ee28a0b60 113 if(current_lang_pack == NULL) return -1;
kisvegabor 1:404ee28a0b60 114
kisvegabor 1:404ee28a0b60 115 uint16_t i;
kisvegabor 1:404ee28a0b60 116
kisvegabor 1:404ee28a0b60 117 for(i = 0; current_lang_pack[i] != NULL; i++) {
kisvegabor 1:404ee28a0b60 118 // Found -> finish
kisvegabor 1:404ee28a0b60 119 if(strcmp(current_lang_pack[i]->locale_name, l_name) == 0) {
kisvegabor 1:404ee28a0b60 120 current_lang = current_lang_pack[i];
kisvegabor 1:404ee28a0b60 121 return 0;
kisvegabor 1:404ee28a0b60 122 }
kisvegabor 1:404ee28a0b60 123 }
kisvegabor 1:404ee28a0b60 124
kisvegabor 1:404ee28a0b60 125 return -1;
kisvegabor 1:404ee28a0b60 126 }
kisvegabor 1:404ee28a0b60 127
kisvegabor 1:404ee28a0b60 128
kisvegabor 1:404ee28a0b60 129 static const char * __lv_i18n_get_text_core(lv_i18n_phrase_t * trans, const char * msg_id)
kisvegabor 1:404ee28a0b60 130 {
kisvegabor 1:404ee28a0b60 131 uint16_t i;
kisvegabor 1:404ee28a0b60 132 for(i = 0; trans[i].msg_id != NULL; i++) {
kisvegabor 1:404ee28a0b60 133 if(strcmp(trans[i].msg_id, msg_id) == 0) {
kisvegabor 1:404ee28a0b60 134 /*The msg_id has found. Check the translation*/
kisvegabor 1:404ee28a0b60 135 if(trans[i].translation) return trans[i].translation;
kisvegabor 1:404ee28a0b60 136 }
kisvegabor 1:404ee28a0b60 137 }
kisvegabor 1:404ee28a0b60 138
kisvegabor 1:404ee28a0b60 139 return NULL;
kisvegabor 1:404ee28a0b60 140 }
kisvegabor 1:404ee28a0b60 141
kisvegabor 1:404ee28a0b60 142
kisvegabor 1:404ee28a0b60 143 /**
kisvegabor 1:404ee28a0b60 144 * Get the translation from a message ID
kisvegabor 1:404ee28a0b60 145 * @param msg_id message ID
kisvegabor 1:404ee28a0b60 146 * @return the translation of `msg_id` on the set local
kisvegabor 1:404ee28a0b60 147 */
kisvegabor 1:404ee28a0b60 148 const char * lv_i18n_get_text(const char * msg_id)
kisvegabor 1:404ee28a0b60 149 {
kisvegabor 1:404ee28a0b60 150 if(current_lang == NULL) return msg_id;
kisvegabor 1:404ee28a0b60 151
kisvegabor 1:404ee28a0b60 152 const lv_i18n_lang_t * lang = current_lang;
kisvegabor 1:404ee28a0b60 153 const void * txt;
kisvegabor 1:404ee28a0b60 154
kisvegabor 1:404ee28a0b60 155 // Search in current locale
kisvegabor 1:404ee28a0b60 156 if(lang->singulars != NULL) {
kisvegabor 1:404ee28a0b60 157 txt = __lv_i18n_get_text_core(lang->singulars, msg_id);
kisvegabor 1:404ee28a0b60 158 if (txt != NULL) return txt;
kisvegabor 1:404ee28a0b60 159 }
kisvegabor 1:404ee28a0b60 160
kisvegabor 1:404ee28a0b60 161 // Try to fallback
kisvegabor 1:404ee28a0b60 162 if(lang == current_lang_pack[0]) return msg_id;
kisvegabor 1:404ee28a0b60 163 lang = current_lang_pack[0];
kisvegabor 1:404ee28a0b60 164
kisvegabor 1:404ee28a0b60 165 // Repeat search for default locale
kisvegabor 1:404ee28a0b60 166 if(lang->singulars != NULL) {
kisvegabor 1:404ee28a0b60 167 txt = __lv_i18n_get_text_core(lang->singulars, msg_id);
kisvegabor 1:404ee28a0b60 168 if (txt != NULL) return txt;
kisvegabor 1:404ee28a0b60 169 }
kisvegabor 1:404ee28a0b60 170
kisvegabor 1:404ee28a0b60 171 return msg_id;
kisvegabor 1:404ee28a0b60 172 }
kisvegabor 1:404ee28a0b60 173
kisvegabor 1:404ee28a0b60 174 /**
kisvegabor 1:404ee28a0b60 175 * Get the translation from a message ID and apply the language's plural rule to get correct form
kisvegabor 1:404ee28a0b60 176 * @param msg_id message ID
kisvegabor 1:404ee28a0b60 177 * @param num an integer to select the correct plural form
kisvegabor 1:404ee28a0b60 178 * @return the translation of `msg_id` on the set local
kisvegabor 1:404ee28a0b60 179 */
kisvegabor 1:404ee28a0b60 180 const char * lv_i18n_get_text_plural(const char * msg_id, int32_t num)
kisvegabor 1:404ee28a0b60 181 {
kisvegabor 1:404ee28a0b60 182 if(current_lang == NULL) return msg_id;
kisvegabor 1:404ee28a0b60 183
kisvegabor 1:404ee28a0b60 184 const lv_i18n_lang_t * lang = current_lang;
kisvegabor 1:404ee28a0b60 185 const void * txt;
kisvegabor 1:404ee28a0b60 186 lv_i18n_plural_type_t ptype;
kisvegabor 1:404ee28a0b60 187
kisvegabor 1:404ee28a0b60 188 // Search in current locale
kisvegabor 1:404ee28a0b60 189 if(lang->locale_plural_fn != NULL) {
kisvegabor 1:404ee28a0b60 190 ptype = lang->locale_plural_fn(num);
kisvegabor 1:404ee28a0b60 191
kisvegabor 1:404ee28a0b60 192 if(lang->plurals[ptype] != NULL) {
kisvegabor 1:404ee28a0b60 193 txt = __lv_i18n_get_text_core(lang->plurals[ptype], msg_id);
kisvegabor 1:404ee28a0b60 194 if (txt != NULL) return txt;
kisvegabor 1:404ee28a0b60 195 }
kisvegabor 1:404ee28a0b60 196 }
kisvegabor 1:404ee28a0b60 197
kisvegabor 1:404ee28a0b60 198 // Try to fallback
kisvegabor 1:404ee28a0b60 199 if(lang == current_lang_pack[0]) return msg_id;
kisvegabor 1:404ee28a0b60 200 lang = current_lang_pack[0];
kisvegabor 1:404ee28a0b60 201
kisvegabor 1:404ee28a0b60 202 // Repeat search for default locale
kisvegabor 1:404ee28a0b60 203 if(lang->locale_plural_fn != NULL) {
kisvegabor 1:404ee28a0b60 204 ptype = lang->locale_plural_fn(num);
kisvegabor 1:404ee28a0b60 205
kisvegabor 1:404ee28a0b60 206 if(lang->plurals[ptype] != NULL) {
kisvegabor 1:404ee28a0b60 207 txt = __lv_i18n_get_text_core(lang->plurals[ptype], msg_id);
kisvegabor 1:404ee28a0b60 208 if (txt != NULL) return txt;
kisvegabor 1:404ee28a0b60 209 }
kisvegabor 1:404ee28a0b60 210 }
kisvegabor 1:404ee28a0b60 211
kisvegabor 1:404ee28a0b60 212 return msg_id;
kisvegabor 1:404ee28a0b60 213 }
kisvegabor 1:404ee28a0b60 214
kisvegabor 1:404ee28a0b60 215 /**
kisvegabor 1:404ee28a0b60 216 * Get the name of the currently used locale.
kisvegabor 1:404ee28a0b60 217 * @return name of the currently used locale. E.g. "en-GB"
kisvegabor 1:404ee28a0b60 218 */
kisvegabor 1:404ee28a0b60 219 const char * lv_i18n_get_current_locale(void)
kisvegabor 1:404ee28a0b60 220 {
kisvegabor 1:404ee28a0b60 221 if(!current_lang) return NULL;
kisvegabor 1:404ee28a0b60 222 return current_lang->locale_name;
kisvegabor 1:404ee28a0b60 223 }