我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用locale.normalize()。
def langs(): ret = [] for k in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): if k in os.environ: v = os.environ.get(k) if v: ret = v.split(':') break if 'C' not in ret: ret.append('C') retval = [] for l in ret: s = locale.normalize(l) if len(s) >= 5 and s[2] == '_': retval.append(s[:5]) retval.append(s[:2]) else: retval.append(s) return retval
def _expand_lang(locale): from locale import normalize locale = normalize(locale) COMPONENT_CODESET = 1 << 0 COMPONENT_TERRITORY = 1 << 1 COMPONENT_MODIFIER = 1 << 2 # split up the locale into its base components mask = 0 pos = locale.find('@') if pos >= 0: modifier = locale[pos:] locale = locale[:pos] mask |= COMPONENT_MODIFIER else: modifier = '' pos = locale.find('.') if pos >= 0: codeset = locale[pos:] locale = locale[:pos] mask |= COMPONENT_CODESET else: codeset = '' pos = locale.find('_') if pos >= 0: territory = locale[pos:] locale = locale[:pos] mask |= COMPONENT_TERRITORY else: territory = '' language = locale ret = [] for i in range(mask+1): if not (i & ~mask): # if all components for this combo exist ... val = language if i & COMPONENT_TERRITORY: val += territory if i & COMPONENT_CODESET: val += codeset if i & COMPONENT_MODIFIER: val += modifier ret.append(val) ret.reverse() return ret
def find(domain, localedir=None, languages=None, all=0): # Get some reasonable defaults for arguments that were not supplied if localedir is None: localedir = _default_localedir if languages is None: languages = [] for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): val = os.environ.get(envar) if val: languages = val.split(':') break if 'C' not in languages: languages.append('C') # now normalize and expand the languages nelangs = [] for lang in languages: for nelang in _expand_lang(lang): if nelang not in nelangs: nelangs.append(nelang) # select a language if all: result = [] else: result = None for lang in nelangs: if lang == 'C': break mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain) if os.path.exists(mofile): if all: result.append(mofile) else: return mofile return result # a mapping between absolute .mo file path and Translation object
def payment_form(self, request, payment): postfinance = { # Add a random suffix, because PostFinance does not like # processing the same order ID over and over. 'orderID': '%s-%s' % (payment.id.hex, get_random_string(4)), 'amount': str(payment.amount_cents), 'currency': 'CHF', 'PSPID': self.pspid, 'language': locale.normalize( to_locale(get_language())).split('.')[0], 'EMAIL': payment.email, } postfinance['SHASign'] = sha1((''.join(( postfinance['orderID'], postfinance['amount'], postfinance['currency'], postfinance['PSPID'], self.sha1_in, ))).encode('utf-8')).hexdigest() return render_to_string('mooch/postfinance_payment_form.html', { 'moocher': self, 'payment': payment, 'postfinance': postfinance, 'mode': 'prod' if self.live else 'test', 'success_url': request.build_absolute_uri(reverse( '%s:postfinance_success' % self.app_name)), 'failure_url': request.build_absolute_uri(str(self.failure_url)), }, request=request)
def main(self): egt = egtlib.Egt(config=self.config, show_archived=True) abspath = os.path.abspath(self.args.project) if os.path.exists(abspath): if self.args.stdin: proj = egt.load_project(abspath, project_fd=sys.stdin) else: proj = egt.load_project(abspath) else: if self.args.stdin: proj = egt.project(self.args.project, project_fd=sys.stdin) else: proj = egt.project(self.args.project) if proj is None: return proj.body.sync_tasks() lang = proj.meta.get("lang") if lang: import locale try: locname = locale.normalize(lang + ".UTF-8") locale.setlocale(locale.LC_ALL, locname) except locale.Error as e: log.warn("Cannot set locale %s: %s", locname, e) # Sync logs after setting locale, so we get the right day/month names proj.log.sync() proj.print(sys.stdout)
def _expand_lang(loc): loc = locale.normalize(loc) COMPONENT_CODESET = 1 << 0 COMPONENT_TERRITORY = 1 << 1 COMPONENT_MODIFIER = 1 << 2 # split up the locale into its base components mask = 0 pos = loc.find('@') if pos >= 0: modifier = loc[pos:] loc = loc[:pos] mask |= COMPONENT_MODIFIER else: modifier = '' pos = loc.find('.') if pos >= 0: codeset = loc[pos:] loc = loc[:pos] mask |= COMPONENT_CODESET else: codeset = '' pos = loc.find('_') if pos >= 0: territory = loc[pos:] loc = loc[:pos] mask |= COMPONENT_TERRITORY else: territory = '' language = loc ret = [] for i in range(mask+1): if not (i & ~mask): # if all components for this combo exist ... val = language if i & COMPONENT_TERRITORY: val += territory if i & COMPONENT_CODESET: val += codeset if i & COMPONENT_MODIFIER: val += modifier ret.append(val) ret.reverse() return ret
def find(domain, localedir=None, languages=None, all=False): # Get some reasonable defaults for arguments that were not supplied if localedir is None: localedir = _default_localedir if languages is None: languages = [] for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): val = os.environ.get(envar) if val: languages = val.split(':') break if 'C' not in languages: languages.append('C') # now normalize and expand the languages nelangs = [] for lang in languages: for nelang in _expand_lang(lang): if nelang not in nelangs: nelangs.append(nelang) # select a language if all: result = [] else: result = None for lang in nelangs: if lang == 'C': break mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain) if os.path.exists(mofile): if all: result.append(mofile) else: return mofile return result # a mapping between absolute .mo file path and Translation object
def check(self, localename, expected): self.assertEqual(locale.normalize(localename), expected, msg=localename)
def test_normalize_issue12752(self): # Issue #1813 caused a regression where locale.normalize() would no # longer accept unicode strings. self.assertEqual(locale.normalize(u'en_US'), 'en_US.ISO8859-1')
def _valid_locales(locales, normalize): """Return a list of normalized locales that do not throw an ``Exception`` when set. Parameters ---------- locales : str A string where each locale is separated by a newline. normalize : bool Whether to call ``locale.normalize`` on each locale. Returns ------- valid_locales : list A list of valid locales. """ if normalize: normalizer = lambda x: locale.normalize(x.strip()) else: normalizer = lambda x: x.strip() return list(filter(_can_set_locale, map(normalizer, locales))) #------------------------------------------------------------------------------ # Console debugging tools
def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_getter): """Get all the locales that are available on the system. Parameters ---------- prefix : str If not ``None`` then return only those locales with the prefix provided. For example to get all English language locales (those that start with ``"en"``), pass ``prefix="en"``. normalize : bool Call ``locale.normalize`` on the resulting list of available locales. If ``True``, only locales that can be set without throwing an ``Exception`` are returned. locale_getter : callable The function to use to retrieve the current locales. This should return a string with each locale separated by a newline character. Returns ------- locales : list of strings A list of locale strings that can be set with ``locale.setlocale()``. For example:: locale.setlocale(locale.LC_ALL, locale_string) On error will return None (no locale available, e.g. Windows) """ try: raw_locales = locale_getter() except: return None try: # raw_locales is "\n" seperated list of locales # it may contain non-decodable parts, so split # extract what we can and then rejoin. raw_locales = raw_locales.split(b'\n') out_locales = [] for x in raw_locales: if compat.PY3: out_locales.append(str(x, encoding=pd.options.display.encoding)) else: out_locales.append(str(x)) except TypeError: pass if prefix is None: return _valid_locales(out_locales, normalize) found = re.compile('%s.*' % prefix).findall('\n'.join(out_locales)) return _valid_locales(found, normalize)
def _language(self, item): """Returns the language of the extracted article by analyzing metatags and inspecting the visible text with langdetect""" response = item['spider_response'].body root = html.fromstring(response) # Check for lang-attributes lang = root.get('lang') if lang is None: lang = root.get('xml:lang') # Check for general meta tags if lang is None: meta = root.cssselect('meta[name="language"]') if len(meta) > 0: lang = meta[0].get('content') # Check for open graph tags if lang is None: meta = root.cssselect('meta[property="og:locale"]') if len(meta) > 0: lang = meta[0].get('content') # Look for <article> elements and inspect the one with the largest payload with langdetect if lang is None: article_list = [] for article in root.xpath('//article'): article_list.append(re.sub(r'\s+', ' ', article.text_content().strip())) if len(article_list) > 0: lang = detect(max(article_list)) # Analyze the whole body with langdetect if lang is None: try: lang = detect(root.text_content().strip()) except LangDetectException: pass # Try to normalize output if lang is not None: # First search for suitable locale in the original output matches = self.langcode_pattern.search(lang) if matches is not None: lang = matches.group(0) else: # If no match was found, normalize the original output and search again normalized = locale.normalize(re.split(r'\s|;|,', lang.strip())[0]) matches = self.langcode_pattern.search(normalized) if matches is not None: lang = matches.group(0) return lang