MCPcopy Index your code
hub / github.com/RustPython/RustPython / normalize

Function normalize

Lib/locale.py:386–467  ·  view source on GitHub ↗

Returns a normalized locale code for the given locale name. The returned locale code is formatted for use with setlocale(). If normalization fails, the original name is returned unchanged. If the given encoding is not known, the function defaults t

(localename)

Source from the content-addressed store, hash-verified

384 return code + '@' + modifier
385
386def normalize(localename):
387
388 """ Returns a normalized locale code for the given locale
389 name.
390
391 The returned locale code is formatted for use with
392 setlocale().
393
394 If normalization fails, the original name is returned
395 unchanged.
396
397 If the given encoding is not known, the function defaults to
398 the default encoding for the locale code just like setlocale()
399 does.
400
401 """
402 # Normalize the locale name and extract the encoding and modifier
403 code = localename.lower()
404 if ':' in code:
405 # ':' is sometimes used as encoding delimiter.
406 code = code.replace(':', '.')
407 if '@' in code:
408 code, modifier = code.split('@', 1)
409 else:
410 modifier = ''
411 if '.' in code:
412 langname, encoding = code.split('.')[:2]
413 else:
414 langname = code
415 encoding = ''
416
417 # First lookup: fullname (possibly with encoding and modifier)
418 lang_enc = langname
419 if encoding:
420 norm_encoding = encoding.replace('-', '')
421 norm_encoding = norm_encoding.replace('_', '')
422 lang_enc += '.' + norm_encoding
423 lookup_name = lang_enc
424 if modifier:
425 lookup_name += '@' + modifier
426 code = locale_alias.get(lookup_name, None)
427 if code is not None:
428 return code
429 #print('first lookup failed')
430
431 if modifier:
432 # Second try: fullname without modifier (possibly with encoding)
433 code = locale_alias.get(lang_enc, None)
434 if code is not None:
435 #print('lookup without modifier succeeded')
436 if '@' not in code:
437 return _append_modifier(code, modifier)
438 if code.split('@', 1)[1].lower() == modifier:
439 return code
440 #print('second lookup failed')
441
442 if encoding:
443 # Third try: langname (without encoding, possibly with modifier)

Callers 3

_parse_localenameFunction · 0.70
setlocaleFunction · 0.70
inverseMethod · 0.50

Calls 6

_append_modifierFunction · 0.85
_replace_encodingFunction · 0.85
lowerMethod · 0.45
replaceMethod · 0.45
splitMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected