Skip to content

Commit

Permalink
Add list_common_locales() function
Browse files Browse the repository at this point in the history
Resolves: #15
  • Loading branch information
mike-fabian committed Sep 21, 2022
1 parent db09858 commit 6bfe001
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 43 deletions.
84 changes: 42 additions & 42 deletions langtable/data/languages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@
<script><scriptId>Syrc</scriptId><rank>10</rank></script>
</scripts>
<locales>
<locale><localeId>ar_EG.UTF-8</localeId><rank>300</rank></locale>
<locale><localeId>ar_EG.UTF-8</localeId><rank>600</rank></locale>
<locale><localeId>ar_SD.UTF-8</localeId><rank>170</rank></locale>
<locale><localeId>ar_DZ.UTF-8</localeId><rank>150</rank></locale>
<locale><localeId>ar_MA.UTF-8</localeId><rank>140</rank></locale>
Expand Down Expand Up @@ -11297,50 +11297,50 @@
</scripts>
<locales>
<locale><localeId>es_ES.UTF-8</localeId><rank>1000</rank></locale>
<locale><localeId>es_AR.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_BO.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_CL.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_CO.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_CR.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_CU.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_DO.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_EC.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_GT.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_HN.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_MX.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_NI.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_PA.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_PE.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_PR.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_PY.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_SV.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_US.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_UY.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_VE.UTF-8</localeId><rank>900</rank></locale>
<locale><localeId>es_AR.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_BO.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_CL.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_CO.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_CR.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_CU.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_DO.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_EC.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_GT.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_HN.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_MX.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_NI.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_PA.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_PE.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_PR.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_PY.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_SV.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_US.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_UY.UTF-8</localeId><rank>400</rank></locale>
<locale><localeId>es_VE.UTF-8</localeId><rank>400</rank></locale>
</locales>
<territories>
<territory><territoryId>ES</territoryId><rank>1000</rank></territory>
<territory><territoryId>419</territoryId><rank>900</rank></territory>
<territory><territoryId>AR</territoryId><rank>900</rank></territory>
<territory><territoryId>BO</territoryId><rank>900</rank></territory>
<territory><territoryId>CL</territoryId><rank>900</rank></territory>
<territory><territoryId>CO</territoryId><rank>900</rank></territory>
<territory><territoryId>CR</territoryId><rank>900</rank></territory>
<territory><territoryId>CU</territoryId><rank>900</rank></territory>
<territory><territoryId>DO</territoryId><rank>900</rank></territory>
<territory><territoryId>EC</territoryId><rank>900</rank></territory>
<territory><territoryId>GT</territoryId><rank>900</rank></territory>
<territory><territoryId>HN</territoryId><rank>900</rank></territory>
<territory><territoryId>MX</territoryId><rank>900</rank></territory>
<territory><territoryId>NI</territoryId><rank>900</rank></territory>
<territory><territoryId>PA</territoryId><rank>900</rank></territory>
<territory><territoryId>PE</territoryId><rank>900</rank></territory>
<territory><territoryId>PR</territoryId><rank>900</rank></territory>
<territory><territoryId>PY</territoryId><rank>900</rank></territory>
<territory><territoryId>SV</territoryId><rank>900</rank></territory>
<territory><territoryId>US</territoryId><rank>900</rank></territory>
<territory><territoryId>UY</territoryId><rank>900</rank></territory>
<territory><territoryId>VE</territoryId><rank>900</rank></territory>
<territory><territoryId>419</territoryId><rank>400</rank></territory>
<territory><territoryId>AR</territoryId><rank>400</rank></territory>
<territory><territoryId>BO</territoryId><rank>400</rank></territory>
<territory><territoryId>CL</territoryId><rank>400</rank></territory>
<territory><territoryId>CO</territoryId><rank>400</rank></territory>
<territory><territoryId>CR</territoryId><rank>400</rank></territory>
<territory><territoryId>CU</territoryId><rank>400</rank></territory>
<territory><territoryId>DO</territoryId><rank>400</rank></territory>
<territory><territoryId>EC</territoryId><rank>400</rank></territory>
<territory><territoryId>GT</territoryId><rank>400</rank></territory>
<territory><territoryId>HN</territoryId><rank>400</rank></territory>
<territory><territoryId>MX</territoryId><rank>400</rank></territory>
<territory><territoryId>NI</territoryId><rank>400</rank></territory>
<territory><territoryId>PA</territoryId><rank>400</rank></territory>
<territory><territoryId>PE</territoryId><rank>400</rank></territory>
<territory><territoryId>PR</territoryId><rank>400</rank></territory>
<territory><territoryId>PY</territoryId><rank>400</rank></territory>
<territory><territoryId>SV</territoryId><rank>400</rank></territory>
<territory><territoryId>US</territoryId><rank>400</rank></territory>
<territory><territoryId>UY</territoryId><rank>400</rank></territory>
<territory><territoryId>VE</territoryId><rank>400</rank></territory>
</territories>
<keyboards>
<keyboard><keyboardId>es</keyboardId><rank>501</rank></keyboard>
Expand Down
82 changes: 81 additions & 1 deletion langtable/langtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# list_locales()
# list_keyboards()
# list_common_languages()
# list_common_locales()
# list_common_keyboards()
# list_consolefonts()
# list_inputmethods()
Expand Down Expand Up @@ -118,6 +119,10 @@
# keyboard layout as prevalent
_KEYBOARD_LAYOUT_RANK_THRESHOLD = 500

# Rank threshold to qualify a
# locale as prevalent
_LOCALE_RANK_THRESHOLD = 500

# For the ICU/CLDR locale pattern see: http://userguide.icu-project.org/locale
# (We ignore the variant code here)
_cldr_locale_pattern = re.compile(
Expand Down Expand Up @@ -2210,7 +2215,7 @@ def list_keyboards(concise=True, show_weights=False, languageId = None, scriptId

def list_common_keyboards(languageId = None, scriptId = None, territoryId = None):
'''Returns highest ranked keyboard layout(s)
2
:param languageId: identifier for the language
:type languageId: string
:param scriptId: identifier for the script
Expand Down Expand Up @@ -2268,6 +2273,81 @@ def list_common_keyboards(languageId = None, scriptId = None, territoryId = None

return sorted(high_ranked_keyboards)

def list_common_locales(languageId = None, scriptId = None, territoryId = None):
'''Returns highest ranked locales
:param languageId: identifier for the language
:type languageId: string
:param scriptId: identifier for the script
:type scriptId: string
:param territoryId: identifier for the territory
:type territoryId: string
:return: list of locales
:rtype: list of strings
**Examples:**
>>> list_common_locales()
['ar_EG.UTF-8', 'en_US.UTF-8', 'en_GB.UTF-8', 'fr_FR.UTF-8', 'de_DE.UTF-8', 'ja_JP.UTF-8', 'zh_CN.UTF-8', 'ru_RU.UTF-8', 'es_ES.UTF-8']
>>> list_common_locales(languageId='fr')
['fr_FR.UTF-8']
>>> list_common_locales(territoryId='CA')
['en_CA.UTF-8']
>>> list_common_locales(territoryId='FR')
['fr_FR.UTF-8']
>>> list_common_locales(languageId='fr', territoryId='CA')
['fr_CA.UTF-8']
>>> list_common_locales(languageId='de', territoryId='FR')
['de_DE.UTF-8']
>>> list_common_locales(languageId='sr', scriptId='Latn')
['sr_RS.UTF-8@latin']
>>> list_common_locales(languageId='sr', scriptId='Cyrl')
['sr_RS.UTF-8']
>>> list_common_locales(languageId='zh', scriptId='Hans')
['zh_CN.UTF-8']
>>> list_common_locales(languageId='zh', scriptId='Hant')
['zh_TW.UTF-8']
>>> list_common_locales(languageId='zh', territoryId='TW')
['zh_TW.UTF-8']
'''
high_ranked_locales = list()
if not languageId and not scriptId and not territoryId:
for language in list_common_languages():
locales = _languages_db[language].locales
selected_locales = [locale for locale, rank
in sorted(locales.items(),
key=lambda x: (-x[1]))
if rank >= _LOCALE_RANK_THRESHOLD]
if selected_locales:
high_ranked_locales.extend(selected_locales)
return high_ranked_locales

kwargs = dict()
locale = _parse_and_split_languageId(
languageId=languageId, scriptId=scriptId, territoryId=territoryId
)
if locale.language:
kwargs.update(dict(languageId=locale.language))
if locale.script:
kwargs.update(dict(scriptId=locale.script))
if locale.territory:
kwargs.update(dict(territoryId=locale.territory))
common_locales = list_locales(**kwargs)
if common_locales:
# Picking up first locale from the list
high_ranked_locales.append(common_locales[0])
return high_ranked_locales

def list_consolefonts(concise=True, show_weights=False, languageId = None, scriptId = None, territoryId = None):
u'''List likely Linux Console fonts
Expand Down

0 comments on commit 6bfe001

Please sign in to comment.