- Clean up the best match function and make it work
authorstffrdhrn <stffrdhrn>
Sat, 26 Nov 2005 11:44:02 +0000 (11:44 +0000)
committerstffrdhrn <stffrdhrn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sat, 26 Nov 2005 11:44:02 +0000 (11:44 +0000)
- In e_path the user list should be before the default list
- Add a function to list all available locales wrapping "locale -a"

SVN revision: 18647

src/bin/e_intl.c
src/bin/e_path.c

index 0b949c6..a1181ab 100644 (file)
@@ -33,6 +33,7 @@ static Evas_Hash *_e_intl_language_path_scan(E_Path *path);
 static void _e_intl_language_hash_free(Evas_Hash *language_hash);
 static char *_e_intl_language_hash_find(Evas_Hash *language_hash, char *language);
 static int _e_intl_language_list_find(Evas_List *language_list, char *language);
+static Evas_List *_e_intl_language_system_locales_get(void);
 Evas_Bool _e_intl_cb_free_language_hash(Evas_Hash *hash, const char *key, void *data, void *fdata);
 static Evas_List *_e_intl_language_dir_scan(const char *dir);
 static int _e_intl_cb_exit(void *data, int type, void *event);
@@ -483,53 +484,57 @@ _e_intl_cb_free_language_hash(Evas_Hash *hash __UNUSED__, const char *key __UNUS
 static char *
 _e_intl_language_hash_find(Evas_Hash *language_hash, char *language)
 {
-   Evas_List *l;
-   Evas_List *all_languages;
-   char *best_language;
-   char *directory;
-   int state;
+   Evas_List   *l;
+   Evas_List   *all_languages;
+   char                *best_language;
+   int          best_chars;
+   char                *directory;
    
    if (!language_hash) return NULL;
    if (!language) return NULL;
 
    best_language = NULL;
+   best_chars = 0;
    all_languages = e_intl_language_list();
    
    /* Do a best match:
     * If language is ja_JP.UTF-8 we should match ja 
-    * If language is zh we should match the first in the list, of zh_CN and zh_TW
+    * If language is zh we should match the first in the list of zh_CN and zh_TW
     */
    for ( l = all_languages ; l ; l = l->next )
      {
-       char *lang;
-       int comp_len;
-        int lang_len;
-        int language_len;      
-       
-       lang = l->data;
-       lang_len = strlen(lang);
-       language_len = strlen(language);
-       /* return shorter */
-       comp_len = lang_len > language_len ? language_len : lang_len;
+       char    *list_lang;
+       int      cmp_ret;
        
-       if ( !strncmp(lang, language, comp_len) )
+       list_lang = l->data;    
+       cmp_ret = strncmp(list_lang, language, 2);
+        if ( cmp_ret == 0 )
          {
-            if ( best_language == NULL ) 
+            int list_lang_len;
+            int language_len;
+            int compare_len;
+            
+            if (best_language == NULL) 
               {
-                 best_language = lang;
-                 if ( lang_len > language_len ) 
-                   state = 1; /* looking for shorter */
-                 else 
-                   state = 0; /* looking for longer */
+                 best_language = list_lang;
+                 best_chars = 2;
+                 continue;
               }
-            else if ( (state == 1 && lang_len > language_len) ||
-                      (state == 0 && lang_len < language_len) )
-              best_language = lang;
             
-            if ( strlen(best_language) == language_len ) break;          
+            list_lang_len = strlen(list_lang);
+            language_len = strlen(language);
+            compare_len = list_lang_len < language_len ?       list_lang_len : 
+                                                               language_len;
+            if ( (compare_len > best_chars ) && 
+                  !strncmp(list_lang, language, compare_len)
+               )
+              {
+                 best_language = list_lang;
+                 best_chars = compare_len;
+              }
          }
      }
-   
+  
    directory = evas_hash_find(language_hash, best_language);
    
    while (all_languages)
@@ -620,6 +625,27 @@ _e_intl_language_dir_scan(const char *dir)
 }
 
 static Evas_List *
+_e_intl_language_system_locales_get(void)
+{
+   Evas_List   *locales;
+   FILE                *output;
+
+   locales = NULL;
+   output = popen("locale -a", "r");
+   if ( output ) 
+     {
+       char line[32];
+       while ( fscanf(output, "%[^\n]\n", line) == 1)
+         {
+            locales = evas_list_append(locales, strdup(line));
+         }
+                 
+       pclose(output);
+     }
+   return locales;
+}
+
+static Evas_List *
 _e_intl_imc_path_scan(E_Path *path)
 {
 
index 74ff31c..4dbb64a 100644 (file)
@@ -334,7 +334,7 @@ e_path_evas_append(E_Path *ep, Evas *evas)
    if (dir_list) evas_list_free(dir_list);
 }
 
-/* compine default_list and and user_list int and easy to use list */
+/* combine default_list and and user_list in and easy to use list */
 Evas_List *
 e_path_dir_list_get(E_Path *ep)
 {
@@ -345,14 +345,6 @@ e_path_dir_list_get(E_Path *ep)
 
    dir_list = NULL;
 
-   for (l = ep->default_dir_list; l; l = l->next)
-     {
-       epd = l->data;
-       new_epd = malloc(sizeof(E_Path_Dir));
-       new_epd->dir = strdup(epd->dir);
-       dir_list = evas_list_append(dir_list, new_epd);
-     }
-
    if (ep->user_dir_list)
      {
        for (l = *(ep->user_dir_list); l; l = l->next)
@@ -364,6 +356,14 @@ e_path_dir_list_get(E_Path *ep)
          }
      }
 
+   for (l = ep->default_dir_list; l; l = l->next)
+     {
+       epd = l->data;
+       new_epd = malloc(sizeof(E_Path_Dir));
+       new_epd->dir = strdup(epd->dir);
+       dir_list = evas_list_append(dir_list, new_epd);
+     }
+
    return dir_list;
 }