From 528a21c7e6fef21a71464a8e0ec4159ebdfee58c Mon Sep 17 00:00:00 2001
From: Michal Pawluk
+ The application demonstrates two different approaches to the text strings translation: +
+ Once the display language is changed via the Settings ➜ Language and input ➜ Display language, the __ui_app_lang_changed() + callback function is invoked (see the Application initialization section for details of event hooking with the + __ui_app_lang_changed() callback function). +
+ ++static void __ui_app_lang_changed(app_event_info_h event_info, void *user_data) +{ + appdata_s *ad = (appdata_s *)user_data; + + char *language = NULL; + + if (!model_get_locale_language(&language)) + return; + + view_set_language(&ad->view, language); + free(language); + + return; +} ++ +
+ When the __ui_app_lang_changed() callback function is invoked, the current language has to be obtained from the system settings, + where the Settings application stores the user's selection regarding the display language. This operation is wrapped by the + model_get_locale_language() function (see the code snippet below for more details). The display language setting is stored + as a key-value pair, where the valid key is identified as the SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE constant value. As a result, + the valid localization code is returned, ex. "en_US" for English (United States) language, "ko_KR" for Korean (Republic of) language, etc. +
+ ++bool model_get_locale_language(char **language) +{ + int ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, language); + if (ret != SYSTEM_SETTINGS_ERROR_NONE) { + controller_log(DLOG_ERROR, "Function system_settings_get_value_string() failed with error %d.", ret); + return false; + } + + return true; +} ++ +
+ Once the valid localization code is obtained, it is passed to the view_set_language() function, where the + elm_language_set() function applies the language to the application's text strings. +
+ ++bool view_set_language(viewdata_s *vd, const char *locale) +{ + if (!locale) { + controller_log(DLOG_ERROR, "Invalid input arguments."); + return false; + } + + /* This portion of code appends the ".UTF-8" string to the localization code which is required by the elm_language_set() function. */ + int str_len = strlen(locale) + strlen(".UTF-8") + 1; + char *localization_string = (char *)calloc(str_len, sizeof(char)); + snprintf(localization_string, str_len, "%s.UTF-8", locale); + + controller_log(DLOG_INFO, "Language changed to: %s (%s)", locale, localization_string); + + elm_language_set(localization_string); + free(localization_string); + + /* The text strings are translated using the i18n functions. */ + __view_display_text_translation(vd); + + return true; +} ++ +
+ The final texts' translation and display is performed by the __view_display_text_translation() function, where the + texts' IDs are used (IDS_BODY_EXAMPLE_TEXT, + IDS_BODY_HELLO_WORLD) together with the i18n_get_text() function. +
+ ++static void __view_display_text_translation(viewdata_s *vd) +{ + elm_object_part_text_set(vd->layout_text_middle, PART_TEXT_MIDDLE_LABEL_TOP, i18n_get_text("IDS_BODY_EXAMPLE_TEXT")); + elm_object_part_text_set(vd->layout_text_middle, PART_TEXT_MIDDLE_LABEL_BOTTOM, i18n_get_text("IDS_BODY_HELLO_WORLD")); +} ++ +
+ The i18n_get_text() function is responsible for proper text string selection from the po files according to the current language + setting. +
+ ++ In order to change the language locally, the user is requested to select desired language from the drop-down list of localization codes and confirm using the "Apply" button. Once the "Apply" button is pressed, + the __view_apply_button_click_cb() callback function is called within the View module. +
+ ++static void __view_apply_button_click_cb(void *data, Evas_Object *obj, void *event_info) +{ + /* The localization code is obtained from the language selection widget. */ + const char *locale = elm_object_text_get(__viewdata->hoversel); + viewdata_s *vd = (viewdata_s *)data; + + if (locale) + view_set_language(vd, locale); +} ++ +
+ Localization code obtained from the language selection widget is passed to the view_set_language() function as an + argument. Further processing is strictly the same as described in the global language change procedure (for reference, see the Hangling global language change section). +