[107] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
[276] | 3 | import gettext
|
---|
| 4 | import os
|
---|
| 5 | import traceback
|
---|
| 6 |
|
---|
| 7 | #------------------------------------------------------------------------------
|
---|
| 8 |
|
---|
[298] | 9 | ## @package mlx.i18n
|
---|
| 10 | #
|
---|
| 11 | # Internationalization support.
|
---|
| 12 | #
|
---|
| 13 | # This module handles the internationalization support in the logger. It is
|
---|
| 14 | # based on the GNU gettext library, and exports the \ref xstr function that
|
---|
| 15 | # returns the translation for a certain key.
|
---|
| 16 |
|
---|
| 17 | #------------------------------------------------------------------------------
|
---|
| 18 |
|
---|
[276] | 19 | _translation = None
|
---|
| 20 | _language = None
|
---|
[155] | 21 |
|
---|
| 22 | #------------------------------------------------------------------------------
|
---|
| 23 |
|
---|
[276] | 24 | def setLanguage(programDirectory, language):
|
---|
[107] | 25 | """Setup the internationalization support for the given language."""
|
---|
[919] | 26 | print("i18n.setLanguage", language)
|
---|
[276] | 27 | translation = _getTranslation(programDirectory, language)
|
---|
| 28 | fallback = _getFallbackFor(programDirectory, language)
|
---|
| 29 | if translation is None:
|
---|
| 30 | translation = fallback
|
---|
| 31 | elif fallback is not None:
|
---|
| 32 | translation.add_fallback(fallback)
|
---|
| 33 | assert translation is not None
|
---|
| 34 |
|
---|
| 35 | global _translation, _language
|
---|
| 36 | _translation = translation
|
---|
| 37 | _language = language
|
---|
[107] | 38 |
|
---|
| 39 | #------------------------------------------------------------------------------
|
---|
| 40 |
|
---|
[227] | 41 | def getLanguage():
|
---|
| 42 | """Get the two-letter language code."""
|
---|
[276] | 43 | underscoreIndex = _language.find("_")
|
---|
| 44 | return _language[:underscoreIndex] if underscoreIndex>0 else _language
|
---|
[227] | 45 |
|
---|
| 46 | #------------------------------------------------------------------------------
|
---|
| 47 |
|
---|
[107] | 48 | def xstr(key):
|
---|
| 49 | """Get the string for the given key in the current language.
|
---|
| 50 |
|
---|
| 51 | If not found, the fallback language is searched. If that is not found
|
---|
| 52 | either, the key itself is returned within curly braces."""
|
---|
[928] | 53 | return _translation.gettext(key)
|
---|
[107] | 54 |
|
---|
| 55 | #------------------------------------------------------------------------------
|
---|
| 56 |
|
---|
[276] | 57 | def _getFallbackFor(programDirectory, language):
|
---|
| 58 | """Get the fallback for the given language.
|
---|
[107] | 59 |
|
---|
[276] | 60 | If the language is English, None is returned, otherwise the English
|
---|
| 61 | language translation."""
|
---|
| 62 | if language in ["en", "en_GB"]:
|
---|
| 63 | return None
|
---|
| 64 | else:
|
---|
| 65 | return _getTranslation(programDirectory, "en")
|
---|
[107] | 66 |
|
---|
| 67 | #------------------------------------------------------------------------------
|
---|
| 68 |
|
---|
[276] | 69 | def _getTranslation(programDirectory, language):
|
---|
| 70 | """Get the translation for the given language."""
|
---|
| 71 | try:
|
---|
| 72 | return gettext.translation("mlx",
|
---|
| 73 | localedir = os.path.join(programDirectory,
|
---|
| 74 | "locale"),
|
---|
| 75 | languages = [language])
|
---|
| 76 | except:
|
---|
| 77 | traceback.print_exc()
|
---|
| 78 | return None
|
---|
[264] | 79 |
|
---|
[107] | 80 | #------------------------------------------------------------------------------
|
---|