Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG lvgl_RB FastPWM millis
lv_i18n.c
00001 #include "./lv_i18n.h" 00002 00003 00004 //////////////////////////////////////////////////////////////////////////////// 00005 // Define plural operands 00006 // http://unicode.org/reports/tr35/tr35-numbers.html#Operands 00007 00008 // Integer version, simplified 00009 00010 #define UNUSED(x) (void)(x) 00011 00012 static lv_i18n_phrase_t fr_texts[] = { 00013 {"M100", "Mesures"}, 00014 {"M200", "Prélevement"}, 00015 {"M300", "Paramètres"}, 00016 {"M400", "Menu 4"}, 00017 {"M500", "Menu 5"}, 00018 {"M600", "Menu 6"}, 00019 {"SM301", "langage"}, 00020 {"Temps de cycle = %3.3f us", "Temps de cycle = %3.3f us"}, 00021 {NULL, NULL} // End mark 00022 }; 00023 00024 static const lv_i18n_lang_t fr_lang = { 00025 .locale_name = "fr", 00026 .singulars = fr_texts, 00027 }; 00028 00029 static lv_i18n_phrase_t en_gb_texts[] = { 00030 {"M100", "Measures"}, 00031 {"M200", "Sample"}, 00032 {"M300", "Setting"}, 00033 {"M400", "Menu 4"}, 00034 {"M500", "Menu 5"}, 00035 {"M600", "Menu 6"}, 00036 {"SM301", "language"}, 00037 {"Temps de cycle = %3.3f us", "Cycle time= %3.3f us"}, 00038 {"cat", "This is a cat"}, 00039 {NULL, NULL} // End mark 00040 }; 00041 00042 00043 static const lv_i18n_lang_t en_gb_lang = { 00044 .locale_name = "en-GB", 00045 .singulars = en_gb_texts, 00046 }; 00047 00048 const lv_i18n_language_pack_t lv_i18n_language_pack[] = { 00049 &en_gb_lang, 00050 &fr_lang, 00051 NULL // End mark 00052 }; 00053 00054 //////////////////////////////////////////////////////////////////////////////// 00055 00056 00057 // Internal state 00058 static const lv_i18n_language_pack_t * current_lang_pack; 00059 static const lv_i18n_lang_t * current_lang; 00060 00061 00062 /** 00063 * Reset internal state. For testing. 00064 */ 00065 void __lv_i18n_reset(void) 00066 { 00067 current_lang_pack = NULL; 00068 current_lang = NULL; 00069 } 00070 00071 /** 00072 * Set the languages for internationalization 00073 * @param langs pointer to the array of languages. (Last element has to be `NULL`) 00074 */ 00075 int lv_i18n_init(const lv_i18n_language_pack_t * langs) 00076 { 00077 if(langs == NULL) return -1; 00078 if(langs[0] == NULL) return -1; 00079 00080 current_lang_pack = langs; 00081 current_lang = langs[0]; /*Automatically select the first language*/ 00082 return 0; 00083 } 00084 00085 /** 00086 * Change the localization (language) 00087 * @param l_name name of the translation locale to use. E.g. "en-GB" 00088 */ 00089 int lv_i18n_set_locale(const char * l_name) 00090 { 00091 if(current_lang_pack == NULL) return -1; 00092 00093 uint16_t i; 00094 00095 for(i = 0; current_lang_pack[i] != NULL; i++) { 00096 // Found -> finish 00097 if(strcmp(current_lang_pack[i]->locale_name, l_name) == 0) { 00098 current_lang = current_lang_pack[i]; 00099 return 0; 00100 } 00101 } 00102 00103 return -1; 00104 } 00105 00106 00107 static const char * __lv_i18n_get_text_core(lv_i18n_phrase_t * trans, const char * msg_id) 00108 { 00109 uint16_t i; 00110 for(i = 0; trans[i].msg_id != NULL; i++) { 00111 if(strcmp(trans[i].msg_id, msg_id) == 0) { 00112 /*The msg_id has found. Check the translation*/ 00113 if(trans[i].translation) return trans[i].translation; 00114 } 00115 } 00116 00117 return NULL; 00118 } 00119 00120 00121 /** 00122 * Get the translation from a message ID 00123 * @param msg_id message ID 00124 * @return the translation of `msg_id` on the set local 00125 */ 00126 const char * lv_i18n_get_text(const char * msg_id) 00127 { 00128 if(current_lang == NULL) return msg_id; 00129 00130 const lv_i18n_lang_t * lang = current_lang; 00131 const void * txt; 00132 00133 // Search in current locale 00134 if(lang->singulars != NULL) { 00135 txt = __lv_i18n_get_text_core(lang->singulars, msg_id); 00136 if (txt != NULL) return txt; 00137 } 00138 00139 // Try to fallback 00140 if(lang == current_lang_pack[0]) return msg_id; 00141 lang = current_lang_pack[0]; 00142 00143 // Repeat search for default locale 00144 if(lang->singulars != NULL) { 00145 txt = __lv_i18n_get_text_core(lang->singulars, msg_id); 00146 if (txt != NULL) return txt; 00147 } 00148 00149 return msg_id; 00150 } 00151 00152 /** 00153 * Get the translation from a message ID and apply the language's plural rule to get correct form 00154 * @param msg_id message ID 00155 * @param num an integer to select the correct plural form 00156 * @return the translation of `msg_id` on the set local 00157 */ 00158 const char * lv_i18n_get_text_plural(const char * msg_id, int32_t num) 00159 { 00160 if(current_lang == NULL) return msg_id; 00161 00162 const lv_i18n_lang_t * lang = current_lang; 00163 const void * txt; 00164 lv_i18n_plural_type_t ptype; 00165 00166 // Search in current locale 00167 if(lang->locale_plural_fn != NULL) { 00168 ptype = lang->locale_plural_fn(num); 00169 00170 if(lang->plurals[ptype] != NULL) { 00171 txt = __lv_i18n_get_text_core(lang->plurals[ptype], msg_id); 00172 if (txt != NULL) return txt; 00173 } 00174 } 00175 00176 // Try to fallback 00177 if(lang == current_lang_pack[0]) return msg_id; 00178 lang = current_lang_pack[0]; 00179 00180 // Repeat search for default locale 00181 if(lang->locale_plural_fn != NULL) { 00182 ptype = lang->locale_plural_fn(num); 00183 00184 if(lang->plurals[ptype] != NULL) { 00185 txt = __lv_i18n_get_text_core(lang->plurals[ptype], msg_id); 00186 if (txt != NULL) return txt; 00187 } 00188 } 00189 00190 return msg_id; 00191 } 00192 00193 /** 00194 * Get the name of the currently used locale. 00195 * @return name of the currently used locale. E.g. "en-GB" 00196 */ 00197 const char * lv_i18n_get_current_locale(void) 00198 { 00199 if(!current_lang) return NULL; 00200 return current_lang->locale_name; 00201 }
Generated on Thu Jul 14 2022 09:39:43 by
