MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / normalize

Function normalize

tools/python-3.11.9-amd64/Lib/locale.py:396–477  ·  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 d

(localename)

Source from the content-addressed store, hash-verified

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

Callers 3

_parse_localenameFunction · 0.70
setlocaleFunction · 0.70

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