1 | # -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | import gettext
|
---|
4 | import os
|
---|
5 | import traceback
|
---|
6 |
|
---|
7 | #------------------------------------------------------------------------------
|
---|
8 |
|
---|
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 |
|
---|
19 | _translation = None
|
---|
20 | _language = None
|
---|
21 |
|
---|
22 | #------------------------------------------------------------------------------
|
---|
23 |
|
---|
24 | def setLanguage(programDirectory, language):
|
---|
25 | """Setup the internationalization support for the given language."""
|
---|
26 | print("i18n.setLanguage", language)
|
---|
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
|
---|
38 |
|
---|
39 | #------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | def getLanguage():
|
---|
42 | """Get the two-letter language code."""
|
---|
43 | underscoreIndex = _language.find("_")
|
---|
44 | return _language[:underscoreIndex] if underscoreIndex>0 else _language
|
---|
45 |
|
---|
46 | #------------------------------------------------------------------------------
|
---|
47 |
|
---|
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."""
|
---|
53 | return _translation.ugettext(key)
|
---|
54 |
|
---|
55 | #------------------------------------------------------------------------------
|
---|
56 |
|
---|
57 | def _getFallbackFor(programDirectory, language):
|
---|
58 | """Get the fallback for the given language.
|
---|
59 |
|
---|
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")
|
---|
66 |
|
---|
67 | #------------------------------------------------------------------------------
|
---|
68 |
|
---|
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
|
---|
79 |
|
---|
80 | #------------------------------------------------------------------------------
|
---|