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