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