Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/xdg/Locale.py: 32%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Helper Module for Locale settings
4This module is based on a ROX module (LGPL):
6http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/i18n.py?rev=1.3&view=log
7"""
9import os
10from locale import normalize
12regex = r"(\[([a-zA-Z]+)(_[a-zA-Z]+)?(\.[a-zA-Z0-9-]+)?(@[a-zA-Z]+)?\])?"
14def _expand_lang(locale):
15 locale = normalize(locale)
16 COMPONENT_CODESET = 1 << 0
17 COMPONENT_MODIFIER = 1 << 1
18 COMPONENT_TERRITORY = 1 << 2
19 # split up the locale into its base components
20 mask = 0
21 pos = locale.find('@')
22 if pos >= 0:
23 modifier = locale[pos:]
24 locale = locale[:pos]
25 mask |= COMPONENT_MODIFIER
26 else:
27 modifier = ''
28 pos = locale.find('.')
29 codeset = ''
30 if pos >= 0:
31 locale = locale[:pos]
32 pos = locale.find('_')
33 if pos >= 0:
34 territory = locale[pos:]
35 locale = locale[:pos]
36 mask |= COMPONENT_TERRITORY
37 else:
38 territory = ''
39 language = locale
40 ret = []
41 for i in range(mask+1):
42 if not (i & ~mask): # if all components for this combo exist ...
43 val = language
44 if i & COMPONENT_TERRITORY: val += territory
45 if i & COMPONENT_CODESET: val += codeset
46 if i & COMPONENT_MODIFIER: val += modifier
47 ret.append(val)
48 ret.reverse()
49 return ret
51def expand_languages(languages=None):
52 # Get some reasonable defaults for arguments that were not supplied
53 if languages is None:
54 languages = []
55 for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
56 val = os.environ.get(envar)
57 if val:
58 languages = val.split(':')
59 break
60 #if 'C' not in languages:
61 # languages.append('C')
63 # now normalize and expand the languages
64 nelangs = []
65 for lang in languages:
66 for nelang in _expand_lang(lang):
67 if nelang not in nelangs:
68 nelangs.append(nelang)
69 return nelangs
71def update(language=None):
72 global langs
73 if language:
74 langs = expand_languages([language])
75 else:
76 langs = expand_languages()
78langs = []
79update()