1
# -*- coding: utf-8 -*-
3
"""module handling translations
5
Note: all translation files have to follow the convention name_localename.qm
11
from PyQt4 import QtCore, QtGui
12
#***************************************************************
14
#***************************************************************
18
# for fast lookups. Keep in sync with CT_Language() + sorted
19
LOCALE_NAMES = ('de', "en", "fr", "pt_BR", "ru")
21
#***************************************************************
23
#***************************************************************
24
class CT_Language(QtCore.QObject):
25
"""class wrapping locale names names"""
27
def trUtf8(self, what):
29
return QtCore.QObject.trUtf8(self, what)
34
QtCore.QObject.__init__(self)
36
self.LANG_EN = self.trUtf8("English")
37
self.LANG_DE = self.trUtf8("German")
38
self.LANG_FR = self.trUtf8("French")
39
self.LANG_RU = self.trUtf8("Russian")
40
self.LANG_PT_BR = self.trUtf8("Portuguese (Brazilian)")
44
#***************************************************************
46
#***************************************************************
47
class Language(object):
48
"""class handling languages for the package"""
53
# do a lazy initialize here
54
self.__languageStrings = None
55
self.__translator = None
56
self.__defaultLocale = DEFAULT_LOCALE
59
def hasLocaleName(self, localeName):
60
"""checks if a locale corrosponding to the specified locale name is available"""
61
# mimic the Qt behavior
62
haveIt = localeName in LOCALE_NAMES
63
while not haveIt and '_' in localeName:
64
localeName = '_'.join(localeName.split('_')[:-1])
65
haveIt = localeName in LOCALE_NAMES
69
def getDefaultLocaleName(self):
70
"""returns the default locale name"""
71
return self.__defaultLocale
74
def setDefaultLocaleName(self, localeName):
75
"""sets the default locale to be used by the class
77
@param localeName (string) locale name
78
@raise ValueError if locale name is not supported
80
if not self.hasLocaleName(localeName):
81
raise ValueError('no such locale: "%s"' % localeName)
82
self.__defaultLocale = localeName
87
lists all available locale names and display names
89
@return list[(QString(display-name1), locale-name1), (...)]
91
Note: the list returned is sorted by localeName
94
if not self.__languageStrings:
95
self.__languageStrings = CT_Language()
98
for localeName in LOCALE_NAMES:
99
tr = getattr(self.__languageStrings, "LANG_" + localeName.upper())()
100
out.append((tr, localeName))
105
def getLanguageFile(self, localeName):
106
"""returns the full path to the language file for the specified locale
109
If a language file for the specified locale name is not available a path
110
to a default language file is returned. See getDefaultLocale().
111
If the default language file could not be found empty string is returned.
113
if not self.hasLocaleName(localeName):
114
localeName = self.getDefaultLocaleName()
115
directory = os.path.dirname(__file__)
116
fpath = os.path.join(directory, SZ_APP + '_' + localeName + '.qm')
117
# no need to check for the files existance. Qt will handle it correctly.
121
def getTranslator(self, parent, localeName):
123
returns an initialized QTranslator for the specified locale name
125
@param parent parent of the QTranslator or None
126
@param localeName name of the locale
128
If a translator for the specified locale name is not available a default
129
translator is returned. See getDefaultLocale().
132
translator = QtCore.QTranslator(parent)
133
fpath = self.getLanguageFile(localeName)
135
translator.load(fpath)
139
def installTranslator(self, parent, localeName):
141
Installs a translator according to the specifed locale name
143
@param parent parent of the translator
144
@param localeName name of the locale for the translator
145
@return QTranslator instance
148
The new translator will replace the translator installed in the last
149
call to this method. Use this method to dynamically switch translators.
151
translator = self.getTranslator(parent, localeName)
152
if self.__translator:
153
QtGui.QApplication.removeTranslator(self.__translator)
154
self.__translator = translator
155
QtGui.QApplication.installTranslator(translator)
156
return self.__translator