3 static Ecore_Exe *_e_intl_input_method_exec = NULL;
4 static Ecore_Event_Handler *_e_intl_exit_handler = NULL;
6 static char *_e_intl_orig_lang = NULL;
7 static char *_e_intl_orig_language = NULL;
8 static char *_e_intl_language = NULL;
10 static char *_e_intl_language_alias = NULL;
12 static char *_e_intl_orig_xmodifiers = NULL;
13 static char *_e_intl_orig_qt_im_module = NULL;
14 static char *_e_intl_orig_gtk_im_module = NULL;
15 static char *_e_intl_orig_ecore_imf_module = NULL;
17 static const char *_e_intl_imc_personal_path = NULL;
18 static const char *_e_intl_imc_system_path = NULL;
20 #define E_EXE_STOP(EXE) if (EXE) { ecore_exe_terminate(EXE); ecore_exe_free(EXE); EXE = NULL; }
21 #define E_EXE_IS_VALID(EXE) (!((!EXE) || (EXE[0] == 0)))
23 /* All locale parts */
24 #define E_INTL_LOC_ALL E_INTL_LOC_LANG | \
26 E_INTL_LOC_CODESET | \
29 /* Locale parts which are significant when Validating */
30 #define E_INTL_LOC_SIGNIFICANT E_INTL_LOC_LANG | \
34 /* Language Setting and Listing */
35 static char *_e_intl_language_path_find(char *language);
36 static Eina_List *_e_intl_language_dir_scan(const char *dir);
37 static int _e_intl_language_list_find(Eina_List *language_list, char *language);
39 /* Locale Validation and Discovery */
40 static Eina_Hash *_e_intl_locale_alias_hash_get(void);
41 static char *_e_intl_locale_alias_get(const char *language);
42 static Eina_List *_e_intl_locale_system_locales_get(void);
43 static Eina_List *_e_intl_locale_search_order_get(const char *locale);
44 static int _e_intl_locale_validate(const char *locale);
45 static void _e_intl_locale_hash_free(Eina_Hash *language_hash);
46 static Eina_Bool _e_intl_locale_hash_free_cb(const Eina_Hash *hash, const void *key, void *data, void *fdata);
48 /* Input Method Configuration and Management */
49 static Eina_Bool _e_intl_cb_exit(void *data, int type, void *event);
50 static Eina_List *_e_intl_imc_dir_scan(const char *dir);
59 if ((s = getenv("LANG"))) _e_intl_orig_lang = strdup(s);
60 if ((s = getenv("LANGUAGE"))) _e_intl_orig_language = strdup(s);
62 if ((s = getenv("GTK_IM_MODULE"))) _e_intl_orig_gtk_im_module = strdup(s);
63 if ((s = getenv("QT_IM_MODULE"))) _e_intl_orig_qt_im_module = strdup(s);
64 if ((s = getenv("XMODIFIERS"))) _e_intl_orig_xmodifiers = strdup(s);
65 if ((s = getenv("ECORE_IMF_MODULE"))) _e_intl_orig_ecore_imf_module = strdup(s);
73 E_FREE(_e_intl_language);
74 E_FREE(_e_intl_orig_lang);
75 E_FREE(_e_intl_orig_language);
77 E_FREE(_e_intl_orig_gtk_im_module);
78 E_FREE(_e_intl_orig_qt_im_module);
79 E_FREE(_e_intl_orig_xmodifiers);
80 E_FREE(_e_intl_orig_ecore_imf_module);
82 if (_e_intl_imc_personal_path)
83 eina_stringshare_del(_e_intl_imc_personal_path);
84 if (_e_intl_imc_system_path)
85 eina_stringshare_del(_e_intl_imc_system_path);
87 e_intl_data_shutdown();
92 /* Setup configuration settings and start services */
94 e_intl_post_init(void)
96 if ((e_config->language) && (e_config->language[0] != 0))
97 e_intl_language_set(e_config->language);
99 e_intl_language_set(NULL);
101 if ((e_config->input_method) && (e_config->input_method[0] != 0))
102 e_intl_input_method_set(e_config->input_method);
104 _e_intl_exit_handler = ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
105 _e_intl_cb_exit, NULL);
110 e_intl_post_shutdown(void)
112 if (_e_intl_exit_handler)
114 ecore_event_handler_del(_e_intl_exit_handler);
115 _e_intl_exit_handler = NULL;
118 e_intl_input_method_set(NULL);
120 e_intl_language_set(NULL);
121 E_FREE(_e_intl_language_alias);
123 E_EXE_STOP(_e_intl_input_method_exec);
129 * - Add error dialogs explaining any errors while setting the locale
130 * * Locale aliases need to be configured
131 * * Locale is invalid
132 * * Message files are not found for this locale, then we have (en_US, POSIX, C)
133 * - Add support of compound locales i.e. (en_US;zh_CN;C) ==Defer==
134 * - Add Configuration for to-be-set environment variables
137 e_intl_language_set(const char *lang)
143 /* NULL lang means set everything back to the original environment
148 e_util_env_set("LANG", _e_intl_orig_lang);
150 if (!lang) lang = getenv("LC_ALL");
151 if (!lang) lang = getenv("LANG");
156 E_FREE(_e_intl_language_alias);
157 _e_intl_language_alias = _e_intl_locale_alias_get(lang);
158 E_FREE(_e_intl_language);
161 _e_intl_language = strdup(lang);
163 _e_intl_language = NULL;
166 if (strcmp(_e_intl_language_alias, "C"))
168 ok = _e_intl_locale_validate(_e_intl_language_alias);
173 new_lang = _e_intl_language_alias;
174 p = strchr(new_lang, '.');
176 _e_intl_language_alias = strdup(new_lang);
178 ok = _e_intl_locale_validate(_e_intl_language_alias);
183 fprintf(stderr, "The locale '%s' cannot be found on your "
184 "system. Please install this locale or try "
185 "something else.", _e_intl_language_alias);
189 /* Only set env vars is a non NULL locale was passed */
192 e_util_env_set("LANG", lang);
193 /* Unset LANGUAGE, apparently causes issues if set */
194 e_util_env_set("LANGUAGE", NULL);
197 setlocale(LC_ALL, lang);
198 if (_e_intl_language)
202 locale_path = _e_intl_language_path_find(_e_intl_language_alias);
205 E_Locale_Parts *locale_parts;
207 locale_parts = e_intl_locale_parts_get(_e_intl_language_alias);
209 /* If locale is C or some form of en don't report an error */
210 if ((!locale_parts) &&
211 (strcmp(_e_intl_language_alias, "C")))
214 "An error occurred setting your locale. \n\n"
216 "The locale you have chosen '%s' appears to \n"
217 "be an alias, however, it can not be resloved.\n"
218 "Please make sure you have a 'locale.alias' \n"
219 "file in your 'messages' path which can resolve\n"
222 "Enlightenment will not be translated.\n",
223 _e_intl_language_alias);
225 else if ((locale_parts) && (locale_parts->lang) &&
226 (strcmp(locale_parts->lang, "en")))
229 "An error occurred setting your locale. \n\n"
231 "The translation files for the locale you \n"
232 "have chosen (%s) cannot be found in your \n"
233 "'messages' path. \n\n"
235 "Enlightenment will not be translated.\n",
236 _e_intl_language_alias);
238 e_intl_locale_parts_free(locale_parts);
243 bindtextdomain(PACKAGE, locale_path);
245 bind_textdomain_codeset(PACKAGE, "UTF-8");
254 e_intl_language_get(void)
256 return _e_intl_language;
260 e_intl_language_alias_get(void)
262 return _e_intl_language_alias;
266 e_intl_language_list(void)
270 Eina_List *all_languages;
273 all_languages = NULL;
274 dir_list = e_path_dir_list_get(path_messages);
275 EINA_LIST_FOREACH(dir_list, next, epd)
277 Eina_List *dir_languages;
280 dir_languages = _e_intl_language_dir_scan(epd->dir);
282 EINA_LIST_FREE(dir_languages, language)
283 if ((_e_intl_language_list_find(all_languages, language)) ||
284 ((strlen(language) > 2) && (!_e_intl_locale_validate(language))))
289 all_languages = eina_list_append(all_languages, language);
292 e_path_dir_list_free(dir_list);
294 return all_languages;
298 _e_intl_language_list_find(Eina_List *language_list, char *language)
303 if ((!language_list) || (!language)) return 0;
305 EINA_LIST_FOREACH(language_list, l, lang)
306 if (!strcmp(lang, language)) return 1;
312 e_intl_input_method_set(const char *imc_path)
316 E_EXE_STOP(_e_intl_input_method_exec);
317 e_util_env_set("GTK_IM_MODULE", _e_intl_orig_gtk_im_module);
318 e_util_env_set("QT_IM_MODULE", _e_intl_orig_qt_im_module);
319 e_util_env_set("XMODIFIERS", _e_intl_orig_xmodifiers);
320 e_util_env_set("ECORE_IMF_MODULE", _e_intl_orig_ecore_imf_module);
326 E_Input_Method_Config *imc;
328 imc_ef = eet_open(imc_path, EET_FILE_MODE_READ);
331 imc = e_intl_input_method_config_read(imc_ef);
336 e_util_env_set("GTK_IM_MODULE", imc->gtk_im_module);
337 e_util_env_set("QT_IM_MODULE", imc->qt_im_module);
338 e_util_env_set("XMODIFIERS", imc->xmodifiers);
339 e_util_env_set("ECORE_IMF_MODULE", imc->ecore_imf_module);
341 E_EXE_STOP(_e_intl_input_method_exec);
343 if (E_EXE_IS_VALID(imc->e_im_exec))
345 e_util_library_path_strip();
346 _e_intl_input_method_exec = ecore_exe_run(imc->e_im_exec, NULL);
347 e_util_library_path_restore();
348 ecore_exe_tag_set(_e_intl_input_method_exec,"E/im_exec");
350 if ((!_e_intl_input_method_exec) ||
351 (!ecore_exe_pid_get(_e_intl_input_method_exec)))
352 e_util_dialog_show(_("Input Method Error"),
353 _( "Error starting the input method executable<br><br>"
354 "please make sure that your input<br>"
355 "method configuration is correct and<br>"
356 "that your configuration's<br>"
357 "executable is in your PATH<br>"));
359 e_intl_input_method_config_free(imc);
366 e_intl_input_method_list(void)
368 Eina_List *input_methods;
374 im_list = _e_intl_imc_dir_scan(e_intl_imc_personal_path_get());
377 input_methods = _e_intl_imc_dir_scan(e_intl_imc_system_path_get());
378 im_list = eina_list_merge(im_list, input_methods);
384 e_intl_imc_personal_path_get(void)
386 if (!_e_intl_imc_personal_path)
390 e_user_dir_concat_static(buf, "input_methods");
391 _e_intl_imc_personal_path = eina_stringshare_add(buf);
393 return _e_intl_imc_personal_path;
397 e_intl_imc_system_path_get(void)
399 if (!_e_intl_imc_system_path)
403 e_prefix_data_concat_static(buf, "data/input_methods");
404 _e_intl_imc_system_path = eina_stringshare_add(buf);
406 return _e_intl_imc_system_path;
410 _e_intl_cb_exit(void *data __UNUSED__, int type __UNUSED__, void *event)
412 Ecore_Exe_Event_Del *ev;
415 if (!ev->exe) return ECORE_CALLBACK_PASS_ON;
417 if (!(ecore_exe_tag_get(ev->exe) &&
418 (!strcmp(ecore_exe_tag_get(ev->exe), "E/im_exec")))) return 1;
420 _e_intl_input_method_exec = NULL;
421 return ECORE_CALLBACK_PASS_ON;
425 _e_intl_locale_hash_free(Eina_Hash *locale_hash)
427 if (!locale_hash) return;
428 eina_hash_foreach(locale_hash, _e_intl_locale_hash_free_cb, NULL);
429 eina_hash_free(locale_hash);
433 _e_intl_locale_hash_free_cb(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__)
441 * get the directory associated with the language. Language Must be valid alias
442 * i.e. Already validated and already de-aliased.
445 * 1) The user does not have an enlightenment translation for this locale
446 * 2) The user does not have their locale.aliases configured correctly
448 * @return NULL if not found.
451 _e_intl_language_path_find(char *language)
456 Eina_List *search_list;
458 Eina_List *next_search;
463 search_list = _e_intl_locale_search_order_get(language);
465 if (!search_list) return NULL;
469 dir_list = e_path_dir_list_get(path_messages);
471 /* For each directory in the path */
472 EINA_LIST_FOREACH(dir_list, next_dir, epd)
476 /* Match canonicalized locale against each possible search */
477 EINA_LIST_FOREACH(search_list, next_search, search_locale)
479 char message_path[PATH_MAX];
481 snprintf(message_path, sizeof(message_path), "%s/%s/LC_MESSAGES/%s.mo", epd->dir, search_locale, PACKAGE);
483 if ((ecore_file_exists(message_path)) && (!ecore_file_is_dir(message_path)))
485 directory = strdup(epd->dir);
491 e_path_dir_list_free(dir_list);
493 EINA_LIST_FREE(search_list, data)
500 _e_intl_language_dir_scan(const char *dir)
502 Eina_List *languages;
508 files = ecore_file_ls(dir);
509 if (!files) return NULL;
513 EINA_LIST_FREE(files, file)
515 char file_path[PATH_MAX];
517 snprintf(file_path, sizeof(file_path),"%s/%s/LC_MESSAGES/%s.mo",
519 if (ecore_file_exists(file_path) && !ecore_file_is_dir(file_path))
520 languages = eina_list_append(languages, file);
528 /* get the alias for a locale
530 * return pointer to allocated alias string. never returns NULL
531 * String will be the same if its a valid locale already or there
535 _e_intl_locale_alias_get(const char *language)
537 Eina_Hash *alias_hash;
539 char *lower_language;
542 if ((!language) || (!strncmp(language, "POSIX", strlen("POSIX"))))
545 alias_hash = _e_intl_locale_alias_hash_get();
546 if (!alias_hash) /* No alias file available */
547 return strdup(language);
549 lower_language = malloc(strlen(language) + 1);
550 for (i = 0; i < strlen(language); i++)
551 lower_language[i] = tolower(language[i]);
552 lower_language[i] = 0;
554 alias = eina_hash_find(alias_hash, lower_language);
555 free(lower_language);
558 alias = strdup(alias);
560 alias = strdup(language);
562 _e_intl_locale_hash_free(alias_hash);
568 _e_intl_locale_alias_hash_get(void)
573 Eina_Hash *alias_hash;
575 dir_list = e_path_dir_list_get(path_messages);
578 EINA_LIST_FOREACH(dir_list, next, epd)
583 snprintf(buf, sizeof(buf), "%s/locale.alias", epd->dir);
587 char alias[4096], locale[4096];
589 /* read locale alias lines */
590 while (fscanf(f, "%4090s %[^\n]\n", alias, locale) == 2)
593 if ((alias[0] == '!') || (alias[0] == '#'))
597 if (eina_hash_find(alias_hash, alias))
600 if (!alias_hash) alias_hash = eina_hash_string_superfast_new(NULL);
601 eina_hash_add(alias_hash, alias, strdup(locale));
606 e_path_dir_list_free(dir_list);
611 /* return parts of the locale that are requested in the mask
612 * return null if the locale looks to be invalid (Does not have
615 * the returned string needs to be freed
618 * Not canonic, just gets the parts
620 EAPI E_Locale_Parts *
621 e_intl_locale_parts_get(const char *locale)
624 E_Locale_Parts *locale_parts;
631 int state = 0; /* start out looking for the language */
632 unsigned int locale_idx;
635 /* Parse Loop - Seperators are _ . @ */
636 for (locale_idx = 0; locale_idx < strlen(locale); locale_idx++)
639 locale_char = locale[locale_idx];
641 /* we have finished scanning the locale string */
645 /* scan this character based on the current start */
648 case 0: /* Gathering Language */
649 if (tmp_idx == 2 && locale_char == '_')
652 language[tmp_idx] = 0;
655 else if ((tmp_idx < 2) && (islower(locale_char)))
656 language[tmp_idx++] = locale_char;
660 case 1: /* Gathering Territory */
661 if (tmp_idx == 2 && locale_char == '.')
664 territory[tmp_idx] = 0;
667 else if ((tmp_idx == 2) && (locale_char == '@'))
670 territory[tmp_idx] = 0;
674 else if ((tmp_idx < 2) && isupper(locale_char))
675 territory[tmp_idx++] = locale_char;
679 case 2: /* Gathering Codeset */
680 if (locale_char == '@')
683 codeset[tmp_idx] = 0;
686 else if (tmp_idx < 32)
687 codeset[tmp_idx++] = locale_char;
691 case 3: /* Gathering modifier */
693 modifier[tmp_idx++] = locale_char;
702 /* set end-of-string \0 */
703 /* There are no breaks here on purpose */
707 language[tmp_idx] = 0;
710 territory[tmp_idx] = 0;
713 codeset[tmp_idx] = 0;
716 modifier[tmp_idx] = 0;
721 locale_parts = E_NEW(E_Locale_Parts, 1);
723 /* Put the parts of the string together */
724 if (language[0] != 0)
726 locale_parts->mask |= E_INTL_LOC_LANG;
727 locale_parts->lang = eina_stringshare_add(language);
729 if (territory[0] != 0)
731 locale_parts->mask |= E_INTL_LOC_REGION;
732 locale_parts->region = eina_stringshare_add(territory);
736 locale_parts->mask |= E_INTL_LOC_CODESET;
737 locale_parts->codeset = eina_stringshare_add(codeset);
739 if (modifier[0] != 0)
741 locale_parts->mask |= E_INTL_LOC_MODIFIER;
742 locale_parts->modifier = eina_stringshare_add(modifier);
749 e_intl_locale_parts_free(E_Locale_Parts *locale_parts)
753 if (locale_parts->lang) eina_stringshare_del(locale_parts->lang);
754 if (locale_parts->region) eina_stringshare_del(locale_parts->region);
755 if (locale_parts->codeset) eina_stringshare_del(locale_parts->codeset);
756 if (locale_parts->modifier) eina_stringshare_del(locale_parts->modifier);
757 E_FREE(locale_parts);
762 e_intl_locale_parts_combine(E_Locale_Parts *locale_parts, int mask)
767 if (!locale_parts) return NULL;
769 if ((mask & locale_parts->mask) != mask) return NULL;
771 /* Construct the clean locale string */
774 /* determine the size */
775 if (mask & E_INTL_LOC_LANG)
776 locale_size = strlen(locale_parts->lang) + 1;
778 if (mask & E_INTL_LOC_REGION)
779 locale_size += strlen(locale_parts->region) + 1;
781 if (mask & E_INTL_LOC_CODESET)
782 locale_size += strlen(locale_parts->codeset) + 1;
784 if (mask & E_INTL_LOC_MODIFIER)
785 locale_size += strlen(locale_parts->modifier) + 1;
787 if (!locale_size) return NULL;
789 /* Allocate memory */
790 locale = (char *) malloc(locale_size);
793 if (mask & E_INTL_LOC_LANG)
794 strcat(locale, locale_parts->lang);
796 if (mask & E_INTL_LOC_REGION)
798 if (locale[0] != 0) strcat(locale, "_");
799 strcat(locale, locale_parts->region);
802 if (mask & E_INTL_LOC_CODESET)
804 if (locale[0] != 0) strcat(locale, ".");
805 strcat(locale, locale_parts->codeset);
808 if (mask & E_INTL_LOC_MODIFIER)
810 if (locale[0] != 0) strcat(locale, "@");
811 strcat(locale, locale_parts->modifier);
818 e_intl_locale_charset_canonic_get(const char *charset)
820 char charset_canonic[32];
826 while ((c = charset[i++]) != 0)
829 charset_canonic[i_tmp++] = tolower(c);
831 charset_canonic[i_tmp] = 0;
833 if (!strcmp(charset, charset_canonic))
836 return strdup(charset_canonic);
840 _e_intl_locale_system_locales_get(void)
846 output = popen("locale -a", "r");
850 while (fscanf(output, "%[^\n]\n", line) == 1)
851 locales = eina_list_append(locales, strdup(line));
859 * must be an un aliased locale;
862 _e_intl_locale_validate(const char *locale)
864 Eina_List *all_locales;
865 E_Locale_Parts *locale_parts;
868 char *locale_cs_canonic;
873 locale_parts = e_intl_locale_parts_get(locale);
875 /* Gather the search information */
877 e_intl_locale_parts_combine(locale_parts,
878 E_INTL_LOC_LANG | E_INTL_LOC_REGION);
881 /* Not valid locale, maybe its an alias */
882 locale_lr = strdup(locale);
883 locale_cs_canonic = NULL;
887 if (locale_parts && locale_parts->codeset)
888 locale_cs_canonic = e_intl_locale_charset_canonic_get(locale_parts->codeset);
890 locale_cs_canonic = NULL;
893 /* Get list of all available locales and aliases */
894 all_locales = _e_intl_locale_system_locales_get();
896 /* Match locale with one from the list */
897 EINA_LIST_FREE(all_locales, locale_next)
901 E_Locale_Parts *locale_parts_next;
902 char * locale_lr_next;
904 locale_parts_next = e_intl_locale_parts_get(locale_next);
905 locale_lr_next = e_intl_locale_parts_combine(locale_parts_next,
906 E_INTL_LOC_LANG | E_INTL_LOC_REGION);
908 if ((locale_parts) && (locale_lr_next) &&
909 (!strcmp(locale_lr, locale_lr_next)))
911 /* Matched lang/region part, now if CS matches */
912 if ((!locale_parts->codeset) && (!locale_parts_next->codeset))
914 /* Lang/Region parts match and no charsets,
919 else if (locale_parts->codeset && locale_parts_next->codeset)
921 if (!strcmp(locale_parts->codeset, locale_parts_next->codeset))
923 /* Lang/Region and charsets match */
926 else if (locale_cs_canonic)
928 char *locale_cs_canonic_next;
929 /* try to match charsets in canonic form */
931 locale_cs_canonic_next =
932 e_intl_locale_charset_canonic_get(locale_parts_next->codeset);
934 if (locale_cs_canonic_next)
936 if (!strcmp(locale_cs_canonic, locale_cs_canonic_next))
938 /* Lang/Resion and charsets in canonic
943 free(locale_cs_canonic_next);
947 if (!strcmp(locale_cs_canonic, locale_parts_next->codeset))
949 /* Lang/Resion and charsets in canonic
961 if (!strcmp(locale_lr, locale_next)) found = 1;
963 e_intl_locale_parts_free(locale_parts_next);
964 E_FREE(locale_lr_next);
969 e_intl_locale_parts_free(locale_parts);
971 E_FREE(locale_cs_canonic);
976 * arg local must be an already validated and unaliased locale
977 * returns the locale search order e.g.
979 * Mask (b) Locale (en_US.UTF-8)
980 * Mask (a) Locale (en_US)
981 * Mask (9) Locale (en.UTF-8)
982 * Mask (8) Locale (en)
985 _e_intl_locale_search_order_get(const char *locale)
987 Eina_List *search_list;
988 E_Locale_Parts *locale_parts;
992 locale_parts = e_intl_locale_parts_get(locale);
993 if (!locale_parts) return NULL;
996 for (mask = E_INTL_LOC_ALL; mask >= E_INTL_LOC_LANG; mask--)
998 if ((mask & locale_parts->mask) == mask)
1000 /* Only append if the mask we need is available */
1001 masked_locale = e_intl_locale_parts_combine(locale_parts, mask);
1002 search_list = eina_list_append(search_list, masked_locale);
1005 e_intl_locale_parts_free(locale_parts);
1010 _e_intl_imc_dir_scan(const char *dir)
1012 Eina_List *imcs = NULL;
1016 files = ecore_file_ls(dir);
1018 EINA_LIST_FREE(files, file)
1020 if (strstr(file, ".imc"))
1024 snprintf(buf, sizeof(buf), "%s/%s", dir, file);
1025 imcs = eina_list_append(imcs, strdup(buf));