spirit avatar spirit committed 7fe5e09

Rewrite get_enchant_base_languages_dict().

Comments (0)

Files changed (1)

guess_language/__init__.py

         """
         global enchant_base_languages_dict
         if enchant_base_languages_dict is None:
-            def get_language_subtag(tag):
+            def get_language_sub_tag(tag):
                 return tag.split("_")[0]
-
-            languages = OrderedDict()
-            locale_language = get_locale_language()
-            for tag in sorted(enchant.list_languages()):
-                subtag = get_language_subtag(tag)
-                if tag == locale_language or subtag not in languages:
-                    languages[subtag] = tag
-            languages_keys = list(languages.keys())
-            languages_items = list(languages.items())
-            for tag in ["en", get_language_subtag(locale_language)]:
-                try:
-                    index = languages_keys.index(tag)
-                except ValueError:
-                    pass
-                else:
-                    languages_keys = (
-                        [languages_keys[index]] +
-                        languages_keys[:index] + languages_keys[index+1:]
-                    )
-                    languages_items = (
-                        [languages_items[index]] +
-                        languages_items[:index] + languages_items[index+1:]
-                    )
-            enchant_base_languages_dict = OrderedDict(languages_items)
-
+            enchant_base_languages_dict = OrderedDict()
+            enchant_languages = sorted(enchant.list_languages())
+            for full_tag in [get_locale_language(), "en_US"]:
+                sub_tag = get_language_sub_tag(full_tag)
+                if sub_tag not in enchant_base_languages_dict:
+                    for tag in [full_tag, sub_tag]:
+                        try:
+                            index = enchant_languages.index(tag)
+                        except ValueError:
+                            pass
+                        else:
+                            enchant_base_languages_dict[sub_tag] = tag
+                            del enchant_languages[index]
+                            break
+            for tag in enchant_languages:
+                sub_tag = get_language_sub_tag(tag)
+                if sub_tag not in enchant_base_languages_dict:
+                    enchant_base_languages_dict[sub_tag] = tag
         return enchant_base_languages_dict
 
     def get_locale_language():
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.