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_lc_all = NULL;
7 static char *_e_intl_language = NULL;
9 static char *_e_intl_language_alias = NULL;
11 static char *_e_intl_orig_xmodifiers = NULL;
12 static char *_e_intl_orig_qt_im_module = NULL;
13 static char *_e_intl_orig_gtk_im_module = NULL;
15 static const char *_e_intl_imc_personal_path = NULL;
16 static const char *_e_intl_imc_system_path = NULL;
18 #define E_EXE_STOP(EXE) if (EXE) { ecore_exe_terminate(EXE); ecore_exe_free(EXE); EXE = NULL; }
19 #define E_EXE_IS_VALID(EXE) (!((!EXE) || (EXE[0] == 0)))
21 /* All locale parts */
22 #define E_INTL_LOC_ALL E_INTL_LOC_LANG | \
24 E_INTL_LOC_CODESET | \
27 /* Locale parts which are significant when Validating */
28 #define E_INTL_LOC_SIGNIFICANT E_INTL_LOC_LANG | \
32 /* Language Setting and Listing */
33 static char *_e_intl_language_path_find(char *language);
34 static Eina_List *_e_intl_language_dir_scan(const char *dir);
35 static int _e_intl_language_list_find(Eina_List *language_list, char *language);
37 /* Locale Validation and Discovery */
38 static Eina_Hash *_e_intl_locale_alias_hash_get(void);
39 static char *_e_intl_locale_alias_get(const char *language);
40 static Eina_List *_e_intl_locale_system_locales_get(void);
41 static Eina_List *_e_intl_locale_search_order_get(const char *locale);
42 static int _e_intl_locale_validate(const char *locale);
43 static void _e_intl_locale_hash_free(Eina_Hash *language_hash);
44 static Eina_Bool _e_intl_locale_hash_free_cb(const Eina_Hash *hash, const void *key, void *data, void *fdata);
46 /* Input Method Configuration and Management */
47 static Eina_Bool _e_intl_cb_exit(void *data, int type, void *event);
48 static Eina_List *_e_intl_imc_dir_scan(const char *dir);
57 if ((s = getenv("LC_ALL"))) _e_intl_orig_lc_all = strdup(s);
59 if ((s = getenv("GTK_IM_MODULE"))) _e_intl_orig_gtk_im_module = strdup(s);
60 if ((s = getenv("QT_IM_MODULE"))) _e_intl_orig_qt_im_module = strdup(s);
61 if ((s = getenv("XMODIFIERS"))) _e_intl_orig_xmodifiers = strdup(s);
69 E_FREE(_e_intl_language);
70 E_FREE(_e_intl_orig_lc_all);
72 E_FREE(_e_intl_orig_gtk_im_module);
73 E_FREE(_e_intl_orig_qt_im_module);
74 E_FREE(_e_intl_orig_xmodifiers);
76 if (_e_intl_imc_personal_path)
77 eina_stringshare_del(_e_intl_imc_personal_path);
78 if (_e_intl_imc_system_path)
79 eina_stringshare_del(_e_intl_imc_system_path);
81 e_intl_data_shutdown();
86 /* Setup configuration settings and start services */
88 e_intl_post_init(void)
90 if ((e_config->language) && (e_config->language[0] != 0))
91 e_intl_language_set(e_config->language);
93 e_intl_language_set(NULL);
95 if ((e_config->input_method) && (e_config->input_method[0] != 0))
96 e_intl_input_method_set(e_config->input_method);
98 _e_intl_exit_handler = ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
99 _e_intl_cb_exit, NULL);
104 e_intl_post_shutdown(void)
106 if (_e_intl_exit_handler)
108 ecore_event_handler_del(_e_intl_exit_handler);
109 _e_intl_exit_handler = NULL;
112 e_intl_input_method_set(NULL);
114 e_intl_language_set(NULL);
115 E_FREE(_e_intl_language_alias);
117 E_EXE_STOP(_e_intl_input_method_exec);
123 * - Add error dialogs explaining any errors while setting the locale
124 * * Locale aliases need to be configured
125 * * Locale is invalid
126 * * Message files are not found for this locale, then we have (en_US, POSIX, C)
127 * - Add support of compound locales i.e. (en_US;zh_CN;C) ==Defer==
128 * - Add Configuration for to-be-set environment variables
131 e_intl_language_set(const char *lang)
137 /* NULL lang means set everything back to the original environment
142 e_util_env_set("LC_ALL", _e_intl_orig_lc_all);
144 if (!lang) lang = getenv("LC_ALL");
145 if (!lang) lang = getenv("LANG");
150 E_FREE(_e_intl_language_alias);
151 _e_intl_language_alias = _e_intl_locale_alias_get(lang);
152 E_FREE(_e_intl_language);
155 _e_intl_language = strdup(lang);
157 _e_intl_language = NULL;
160 if (strcmp(_e_intl_language_alias, "C"))
162 ok = _e_intl_locale_validate(_e_intl_language_alias);
167 new_lang = _e_intl_language_alias;
168 p = strchr(new_lang, '.');
170 _e_intl_language_alias = strdup(new_lang);
172 ok = _e_intl_locale_validate(_e_intl_language_alias);
177 fprintf(stderr, "The locale '%s' cannot be found on your "
178 "system. Please install this locale or try "
179 "something else.", _e_intl_language_alias);
183 /* Only set env vars is a non NULL locale was passed */
186 e_util_env_set("LC_ALL", _e_intl_language);
189 setlocale(LC_ALL, _e_intl_language);
190 if (_e_intl_language)
194 locale_path = _e_intl_language_path_find(_e_intl_language_alias);
197 E_Locale_Parts *locale_parts;
199 locale_parts = e_intl_locale_parts_get(_e_intl_language_alias);
201 /* If locale is C or some form of en don't report an error */
202 if ((!locale_parts) &&
203 (strcmp(_e_intl_language_alias, "C")))
206 "An error occurred setting your locale. \n\n"
208 "The locale you have chosen '%s' appears to \n"
209 "be an alias, however, it can not be resloved.\n"
210 "Please make sure you have a 'locale.alias' \n"
211 "file in your 'messages' path which can resolve\n"
214 "Enlightenment will not be translated.\n",
215 _e_intl_language_alias);
217 else if ((locale_parts) && (locale_parts->lang) &&
218 (strcmp(locale_parts->lang, "en")))
221 "An error occurred setting your locale. \n\n"
223 "The translation files for the locale you \n"
224 "have chosen (%s) cannot be found in your \n"
225 "'messages' path. \n\n"
227 "Enlightenment will not be translated.\n",
228 _e_intl_language_alias);
230 e_intl_locale_parts_free(locale_parts);
235 bindtextdomain(PACKAGE, locale_path);
237 bind_textdomain_codeset(PACKAGE, "UTF-8");
246 e_intl_language_get(void)
248 return _e_intl_language;
252 e_intl_language_alias_get(void)
254 return _e_intl_language_alias;
258 e_intl_language_list(void)
262 Eina_List *all_languages;
265 all_languages = NULL;
266 dir_list = e_path_dir_list_get(path_messages);
267 EINA_LIST_FOREACH(dir_list, next, epd)
269 Eina_List *dir_languages;
272 dir_languages = _e_intl_language_dir_scan(epd->dir);
274 EINA_LIST_FREE(dir_languages, language)
275 if ((_e_intl_language_list_find(all_languages, language)) ||
276 ((strlen(language) > 2) && (!_e_intl_locale_validate(language))))
281 all_languages = eina_list_append(all_languages, language);
284 e_path_dir_list_free(dir_list);
286 return all_languages;
290 _e_intl_language_list_find(Eina_List *language_list, char *language)
295 if ((!language_list) || (!language)) return 0;
297 EINA_LIST_FOREACH(language_list, l, lang)
298 if (!strcmp(lang, language)) return 1;
304 e_intl_input_method_set(const char *imc_path)
308 E_EXE_STOP(_e_intl_input_method_exec);
309 e_util_env_set("GTK_IM_MODULE", _e_intl_orig_gtk_im_module);
310 e_util_env_set("QT_IM_MODULE", _e_intl_orig_qt_im_module);
311 e_util_env_set("XMODIFIERS", _e_intl_orig_xmodifiers);
317 E_Input_Method_Config *imc;
319 imc_ef = eet_open(imc_path, EET_FILE_MODE_READ);
322 imc = e_intl_input_method_config_read(imc_ef);
327 e_util_env_set("GTK_IM_MODULE", imc->gtk_im_module);
328 e_util_env_set("QT_IM_MODULE", imc->qt_im_module);
329 e_util_env_set("XMODIFIERS", imc->xmodifiers);
331 E_EXE_STOP(_e_intl_input_method_exec);
333 if (E_EXE_IS_VALID(imc->e_im_exec))
335 e_util_library_path_strip();
336 _e_intl_input_method_exec = ecore_exe_run(imc->e_im_exec, NULL);
337 e_util_library_path_restore();
338 ecore_exe_tag_set(_e_intl_input_method_exec,"E/im_exec");
340 if ((!_e_intl_input_method_exec) ||
341 (!ecore_exe_pid_get(_e_intl_input_method_exec)))
342 e_util_dialog_show(_("Input Method Error"),
343 _( "Error starting the input method executable<br><br>"
344 "please make sure that your input<br>"
345 "method configuration is correct and<br>"
346 "that your configuration's<br>"
347 "executable is in your PATH<br>"));
349 e_intl_input_method_config_free(imc);
356 e_intl_input_method_list(void)
358 Eina_List *input_methods;
364 im_list = _e_intl_imc_dir_scan(e_intl_imc_personal_path_get());
367 input_methods = _e_intl_imc_dir_scan(e_intl_imc_system_path_get());
368 im_list = eina_list_merge(im_list, input_methods);
374 e_intl_imc_personal_path_get(void)
376 if (!_e_intl_imc_personal_path)
380 e_user_dir_concat_static(buf, "input_methods");
381 _e_intl_imc_personal_path = eina_stringshare_add(buf);
383 return _e_intl_imc_personal_path;
387 e_intl_imc_system_path_get(void)
389 if (!_e_intl_imc_system_path)
393 e_prefix_data_concat_static(buf, "data/input_methods");
394 _e_intl_imc_system_path = eina_stringshare_add(buf);
396 return _e_intl_imc_system_path;
400 _e_intl_cb_exit(void *data __UNUSED__, int type __UNUSED__, void *event)
402 Ecore_Exe_Event_Del *ev;
405 if (!ev->exe) return ECORE_CALLBACK_PASS_ON;
407 if (!(ecore_exe_tag_get(ev->exe) &&
408 (!strcmp(ecore_exe_tag_get(ev->exe), "E/im_exec")))) return 1;
410 _e_intl_input_method_exec = NULL;
411 return ECORE_CALLBACK_PASS_ON;
415 _e_intl_locale_hash_free(Eina_Hash *locale_hash)
417 if (!locale_hash) return;
418 eina_hash_foreach(locale_hash, _e_intl_locale_hash_free_cb, NULL);
419 eina_hash_free(locale_hash);
423 _e_intl_locale_hash_free_cb(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__)
431 * get the directory associated with the language. Language Must be valid alias
432 * i.e. Already validated and already de-aliased.
435 * 1) The user does not have an enlightenment translation for this locale
436 * 2) The user does not have their locale.aliases configured correctly
438 * @return NULL if not found.
441 _e_intl_language_path_find(char *language)
446 Eina_List *search_list;
448 Eina_List *next_search;
453 search_list = _e_intl_locale_search_order_get(language);
455 if (!search_list) return NULL;
459 dir_list = e_path_dir_list_get(path_messages);
461 /* For each directory in the path */
462 EINA_LIST_FOREACH(dir_list, next_dir, epd)
466 /* Match canonicalized locale against each possible search */
467 EINA_LIST_FOREACH(search_list, next_search, search_locale)
469 char message_path[PATH_MAX];
471 snprintf(message_path, sizeof(message_path), "%s/%s/LC_MESSAGES/%s.mo", epd->dir, search_locale, PACKAGE);
473 if ((ecore_file_exists(message_path)) && (!ecore_file_is_dir(message_path)))
475 directory = strdup(epd->dir);
481 e_path_dir_list_free(dir_list);
483 EINA_LIST_FREE(search_list, data)
490 _e_intl_language_dir_scan(const char *dir)
492 Eina_List *languages;
498 files = ecore_file_ls(dir);
499 if (!files) return NULL;
503 EINA_LIST_FREE(files, file)
505 char file_path[PATH_MAX];
507 snprintf(file_path, sizeof(file_path),"%s/%s/LC_MESSAGES/%s.mo",
509 if (ecore_file_exists(file_path) && !ecore_file_is_dir(file_path))
510 languages = eina_list_append(languages, file);
518 /* get the alias for a locale
520 * return pointer to allocated alias string. never returns NULL
521 * String will be the same if its a valid locale already or there
525 _e_intl_locale_alias_get(const char *language)
527 Eina_Hash *alias_hash;
529 char *lower_language;
532 if ((!language) || (!strncmp(language, "POSIX", strlen("POSIX"))))
535 alias_hash = _e_intl_locale_alias_hash_get();
536 if (!alias_hash) /* No alias file available */
537 return strdup(language);
539 lower_language = malloc(strlen(language) + 1);
540 for (i = 0; i < strlen(language); i++)
541 lower_language[i] = tolower(language[i]);
542 lower_language[i] = 0;
544 alias = eina_hash_find(alias_hash, lower_language);
545 free(lower_language);
548 alias = strdup(alias);
550 alias = strdup(language);
552 _e_intl_locale_hash_free(alias_hash);
558 _e_intl_locale_alias_hash_get(void)
563 Eina_Hash *alias_hash;
565 dir_list = e_path_dir_list_get(path_messages);
568 EINA_LIST_FOREACH(dir_list, next, epd)
573 snprintf(buf, sizeof(buf), "%s/locale.alias", epd->dir);
577 char alias[4096], locale[4096];
579 /* read locale alias lines */
580 while (fscanf(f, "%4090s %[^\n]\n", alias, locale) == 2)
583 if ((alias[0] == '!') || (alias[0] == '#'))
587 if (eina_hash_find(alias_hash, alias))
590 if (!alias_hash) alias_hash = eina_hash_string_superfast_new(NULL);
591 eina_hash_add(alias_hash, alias, strdup(locale));
596 e_path_dir_list_free(dir_list);
601 /* return parts of the locale that are requested in the mask
602 * return null if the locale looks to be invalid (Does not have
605 * the returned string needs to be freed
608 * Not canonic, just gets the parts
610 EAPI E_Locale_Parts *
611 e_intl_locale_parts_get(const char *locale)
614 E_Locale_Parts *locale_parts;
621 int state = 0; /* start out looking for the language */
622 unsigned int locale_idx;
625 /* Parse Loop - Seperators are _ . @ */
626 for (locale_idx = 0; locale_idx < strlen(locale); locale_idx++)
629 locale_char = locale[locale_idx];
631 /* we have finished scanning the locale string */
635 /* scan this character based on the current start */
638 case 0: /* Gathering Language */
639 if (tmp_idx == 2 && locale_char == '_')
642 language[tmp_idx] = 0;
645 else if ((tmp_idx < 2) && (islower(locale_char)))
646 language[tmp_idx++] = locale_char;
650 case 1: /* Gathering Territory */
651 if (tmp_idx == 2 && locale_char == '.')
654 territory[tmp_idx] = 0;
657 else if ((tmp_idx == 2) && (locale_char == '@'))
660 territory[tmp_idx] = 0;
664 else if ((tmp_idx < 2) && isupper(locale_char))
665 territory[tmp_idx++] = locale_char;
669 case 2: /* Gathering Codeset */
670 if (locale_char == '@')
673 codeset[tmp_idx] = 0;
676 else if (tmp_idx < 32)
677 codeset[tmp_idx++] = locale_char;
681 case 3: /* Gathering modifier */
683 modifier[tmp_idx++] = locale_char;
692 /* set end-of-string \0 */
693 /* There are no breaks here on purpose */
697 language[tmp_idx] = 0;
700 territory[tmp_idx] = 0;
703 codeset[tmp_idx] = 0;
706 modifier[tmp_idx] = 0;
711 locale_parts = E_NEW(E_Locale_Parts, 1);
713 /* Put the parts of the string together */
714 if (language[0] != 0)
716 locale_parts->mask |= E_INTL_LOC_LANG;
717 locale_parts->lang = eina_stringshare_add(language);
719 if (territory[0] != 0)
721 locale_parts->mask |= E_INTL_LOC_REGION;
722 locale_parts->region = eina_stringshare_add(territory);
726 locale_parts->mask |= E_INTL_LOC_CODESET;
727 locale_parts->codeset = eina_stringshare_add(codeset);
729 if (modifier[0] != 0)
731 locale_parts->mask |= E_INTL_LOC_MODIFIER;
732 locale_parts->modifier = eina_stringshare_add(modifier);
739 e_intl_locale_parts_free(E_Locale_Parts *locale_parts)
743 if (locale_parts->lang) eina_stringshare_del(locale_parts->lang);
744 if (locale_parts->region) eina_stringshare_del(locale_parts->region);
745 if (locale_parts->codeset) eina_stringshare_del(locale_parts->codeset);
746 if (locale_parts->modifier) eina_stringshare_del(locale_parts->modifier);
747 E_FREE(locale_parts);
752 e_intl_locale_parts_combine(E_Locale_Parts *locale_parts, int mask)
757 if (!locale_parts) return NULL;
759 if ((mask & locale_parts->mask) != mask) return NULL;
761 /* Construct the clean locale string */
764 /* determine the size */
765 if (mask & E_INTL_LOC_LANG)
766 locale_size = strlen(locale_parts->lang) + 1;
768 if (mask & E_INTL_LOC_REGION)
769 locale_size += strlen(locale_parts->region) + 1;
771 if (mask & E_INTL_LOC_CODESET)
772 locale_size += strlen(locale_parts->codeset) + 1;
774 if (mask & E_INTL_LOC_MODIFIER)
775 locale_size += strlen(locale_parts->modifier) + 1;
777 /* Allocate memory */
778 locale = (char *) malloc(locale_size);
781 if (mask & E_INTL_LOC_LANG)
782 strcat(locale, locale_parts->lang);
784 if (mask & E_INTL_LOC_REGION)
786 if (locale[0] != 0) strcat(locale, "_");
787 strcat(locale, locale_parts->region);
790 if (mask & E_INTL_LOC_CODESET)
792 if (locale[0] != 0) strcat(locale, ".");
793 strcat(locale, locale_parts->codeset);
796 if (mask & E_INTL_LOC_MODIFIER)
798 if (locale[0] != 0) strcat(locale, "@");
799 strcat(locale, locale_parts->modifier);
806 e_intl_locale_charset_canonic_get(const char *charset)
808 char charset_canonic[32];
814 while ((c = charset[i++]) != 0)
817 charset_canonic[i_tmp++] = tolower(c);
819 charset_canonic[i_tmp] = 0;
821 if (!strcmp(charset, charset_canonic))
824 return strdup(charset_canonic);
828 _e_intl_locale_system_locales_get(void)
834 output = popen("locale -a", "r");
838 while (fscanf(output, "%[^\n]\n", line) == 1)
839 locales = eina_list_append(locales, strdup(line));
847 * must be an un aliased locale;
850 _e_intl_locale_validate(const char *locale)
852 Eina_List *all_locales;
853 E_Locale_Parts *locale_parts;
856 char *locale_cs_canonic;
861 locale_parts = e_intl_locale_parts_get(locale);
863 /* Gather the search information */
865 e_intl_locale_parts_combine(locale_parts,
866 E_INTL_LOC_LANG | E_INTL_LOC_REGION);
869 /* Not valid locale, maybe its an alias */
870 locale_lr = strdup(locale);
871 locale_cs_canonic = NULL;
875 if (locale_parts && locale_parts->codeset)
876 locale_cs_canonic = e_intl_locale_charset_canonic_get(locale_parts->codeset);
878 locale_cs_canonic = NULL;
881 /* Get list of all available locales and aliases */
882 all_locales = _e_intl_locale_system_locales_get();
884 /* Match locale with one from the list */
885 EINA_LIST_FREE(all_locales, locale_next)
889 E_Locale_Parts *locale_parts_next;
890 char * locale_lr_next;
892 locale_parts_next = e_intl_locale_parts_get(locale_next);
893 locale_lr_next = e_intl_locale_parts_combine(locale_parts_next,
894 E_INTL_LOC_LANG | E_INTL_LOC_REGION);
896 if ((locale_parts) && (locale_lr_next) &&
897 (!strcmp(locale_lr, locale_lr_next)))
899 /* Matched lang/region part, now if CS matches */
900 if ((!locale_parts->codeset) && (!locale_parts_next->codeset))
902 /* Lang/Region parts match and no charsets,
907 else if (locale_parts->codeset && locale_parts_next->codeset)
909 if (!strcmp(locale_parts->codeset, locale_parts_next->codeset))
911 /* Lang/Region and charsets match */
914 else if (locale_cs_canonic)
916 char *locale_cs_canonic_next;
917 /* try to match charsets in canonic form */
919 locale_cs_canonic_next =
920 e_intl_locale_charset_canonic_get(locale_parts_next->codeset);
922 if (locale_cs_canonic_next)
924 if (!strcmp(locale_cs_canonic, locale_cs_canonic_next))
926 /* Lang/Resion and charsets in canonic
931 free(locale_cs_canonic_next);
935 if (!strcmp(locale_cs_canonic, locale_parts_next->codeset))
937 /* Lang/Resion and charsets in canonic
949 if (!strcmp(locale_lr, locale_next)) found = 1;
951 e_intl_locale_parts_free(locale_parts_next);
952 E_FREE(locale_lr_next);
957 e_intl_locale_parts_free(locale_parts);
959 E_FREE(locale_cs_canonic);
964 * arg local must be an already validated and unaliased locale
965 * returns the locale search order e.g.
967 * Mask (b) Locale (en_US.UTF-8)
968 * Mask (a) Locale (en_US)
969 * Mask (9) Locale (en.UTF-8)
970 * Mask (8) Locale (en)
973 _e_intl_locale_search_order_get(const char *locale)
975 Eina_List *search_list;
976 E_Locale_Parts *locale_parts;
980 locale_parts = e_intl_locale_parts_get(locale);
981 if (!locale_parts) return NULL;
984 for (mask = E_INTL_LOC_ALL; mask >= E_INTL_LOC_LANG; mask--)
986 if ((mask & locale_parts->mask) == mask)
988 /* Only append if the mask we need is available */
989 masked_locale = e_intl_locale_parts_combine(locale_parts, mask);
990 search_list = eina_list_append(search_list, masked_locale);
993 e_intl_locale_parts_free(locale_parts);
998 _e_intl_imc_dir_scan(const char *dir)
1000 Eina_List *imcs = NULL;
1004 files = ecore_file_ls(dir);
1006 EINA_LIST_FREE(files, file)
1008 if (strstr(file, ".imc"))
1012 snprintf(buf, sizeof(buf), "%s/%s", dir, file);
1013 imcs = eina_list_append(imcs, strdup(buf));