Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa488ec

Browse files
committed
_expand_lang(), _find(): Added support for unaliasing and expanded the
language found in the environment variable, contributed by James Henstridge.
1 parent 1dce09d commit fa488ec

1 file changed

Lines changed: 53 additions & 4 deletions

File tree

Lib/gettext.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
8787
"""
8888

89-
# This module represents the integration of work from the following authors:
89+
# This module represents the integration of work, contributions, feedback, and
90+
# suggestions from the following people:
9091
#
9192
# Martin von Loewis, who wrote the initial implementation of the underlying
9293
# C-based libintlmodule (later renamed _gettext), along with a skeletal
@@ -120,6 +121,49 @@
120121
_localedirs = {}
121122

122123

124+
125+
def _expand_lang(locale):
126+
from locale import normalize
127+
locale = normalize(locale)
128+
COMPONENT_CODESET = 1 << 0
129+
COMPONENT_TERRITORY = 1 << 1
130+
COMPONENT_MODIFIER = 1 << 2
131+
# split up the locale into its base components
132+
mask = 0
133+
pos = locale.find('@')
134+
if pos >= 0:
135+
modifier = locale[pos:]
136+
locale = locale[:pos]
137+
mask |= COMPONENT_MODIFIER
138+
else:
139+
modifier = ''
140+
pos = locale.find('.')
141+
if pos >= 0:
142+
codeset = locale[pos:]
143+
locale = locale[:pos]
144+
mask |= COMPONENT_CODESET
145+
else:
146+
codeset = ''
147+
pos = locale.find('_')
148+
if pos >= 0:
149+
territory = locale[pos:]
150+
locale = locale[:pos]
151+
mask |= COMPONENT_TERRITORY
152+
else:
153+
territory = ''
154+
language = locale
155+
ret = []
156+
for i in range(mask+1):
157+
if not (i & ~mask): # if all components for this combo exist ...
158+
val = language
159+
if i & COMPONENT_TERRITORY: val += territory
160+
if i & COMPONENT_CODESET: val += codeset
161+
if i & COMPONENT_MODIFIER: val += modifier
162+
ret.append(val)
163+
ret.reverse()
164+
return ret
165+
166+
123167

124168
class GNUTranslations(UserDict):
125169
# Magic number of .mo files
@@ -158,8 +202,8 @@ def _parse(self, fp):
158202
raise IOError(0, 'File is corrupt', filename)
159203
#
160204
# advance to next entry in the seek tables
161-
masteridx = masteridx + 8
162-
transidx = transidx + 8
205+
masteridx += 8
206+
transidx += 8
163207
return catalog
164208

165209

@@ -171,7 +215,6 @@ def _parse(self, fp):
171215
def _find(localedir=None, languages=None, domain=None):
172216
global _current_domain
173217
global _localedirs
174-
175218
# Get some reasonable defaults for arguments that were not supplied
176219
if domain is None:
177220
domain = _current_domain
@@ -193,6 +236,12 @@ def _find(localedir=None, languages=None, domain=None):
193236
break
194237
if 'C' not in languages:
195238
languages.append('C')
239+
# now normalize and expand the languages
240+
langdict = {}
241+
for lang in languages:
242+
for nelang in _expand_lang(lang):
243+
langdict[nelang] = nelang
244+
languages = langdict.keys()
196245
# select a language
197246
for lang in languages:
198247
if lang == 'C':

0 commit comments

Comments
 (0)