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