(domain, localedir=None, languages=None,
class_=None, fallback=False)
| 517 | |
| 518 | |
| 519 | def translation(domain, localedir=None, languages=None, |
| 520 | class_=None, fallback=False): |
| 521 | if class_ is None: |
| 522 | class_ = GNUTranslations |
| 523 | mofiles = find(domain, localedir, languages, all=True) |
| 524 | if not mofiles: |
| 525 | if fallback: |
| 526 | return NullTranslations() |
| 527 | from errno import ENOENT |
| 528 | raise FileNotFoundError(ENOENT, |
| 529 | 'No translation file found for domain', domain) |
| 530 | # Avoid opening, reading, and parsing the .mo file after it's been done |
| 531 | # once. |
| 532 | result = None |
| 533 | for mofile in mofiles: |
| 534 | key = (class_, os.path.abspath(mofile)) |
| 535 | t = _translations.get(key) |
| 536 | if t is None: |
| 537 | with open(mofile, 'rb') as fp: |
| 538 | t = _translations.setdefault(key, class_(fp)) |
| 539 | # Copy the translation object to allow setting fallbacks and |
| 540 | # output charset. All other instance data is shared with the |
| 541 | # cached object. |
| 542 | # Delay copy import for speeding up gettext import when .mo files |
| 543 | # are not used. |
| 544 | import copy |
| 545 | t = copy.copy(t) |
| 546 | if result is None: |
| 547 | result = t |
| 548 | else: |
| 549 | result.add_fallback(t) |
| 550 | return result |
| 551 | |
| 552 | |
| 553 | def install(domain, localedir=None, *, names=None): |
no test coverage detected