From 528a21c7e6fef21a71464a8e0ec4159ebdfee58c Mon Sep 17 00:00:00 2001 From: Michal Pawluk Date: Fri, 18 Sep 2015 13:21:36 +0200 Subject: [PATCH] [SAMPLE APP][INTERNATIONALIZATION] Text translation section added Change-Id: I3f79a1e586d64cd8d26d1fabf8009af76dbb4a2e Signed-off-by: Michal Pawluk --- .../html/mobile_n/internationalization_sd_mn.htm | 138 ++++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) diff --git a/org.tizen.sampledescriptions/html/mobile_n/internationalization_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/internationalization_sd_mn.htm index f4142a3..62e77af 100644 --- a/org.tizen.sampledescriptions/html/mobile_n/internationalization_sd_mn.htm +++ b/org.tizen.sampledescriptions/html/mobile_n/internationalization_sd_mn.htm @@ -223,9 +223,143 @@ static bool __get_available_locale_cb(const char *locale, void *data) The view related source code is not listed here, as it is not a subject of this document.

-

Application termination

+

Text translation

-

Text translation files handling

+

+ The application demonstrates two different approaches to the text strings translation: +

+ Both methods require that the strings translation files are available (see the Language Explorer section for more details). +

+ +

Hangling global language change

+ +

+ 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. +

+ +

Handling local language change

+ +

+ 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). +

Model

-- 2.7.4