~ubuntu-branches/ubuntu/karmic/eric/karmic

« back to all changes in this revision

Viewing changes to eric/ThirdParty/chartables/chartables_lang/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-01-28 18:02:25 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080128180225-6nrox6yrworh2c4v
Tags: 4.0.4-1ubuntu1
* Add python-qt3 to build-depends becuase that's where Ubuntu puts 
  pyqtconfig
* Change maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
"""module handling translations
 
4
 
 
5
Note: all translation files have to follow the convention name_localename.qm
 
6
 
 
7
"""
 
8
 
 
9
 
 
10
import os
 
11
from PyQt4 import QtCore, QtGui
 
12
#***************************************************************
 
13
#
 
14
#***************************************************************
 
15
SZ_APP = "chartables"
 
16
DEFAULT_LOCALE = "en"
 
17
 
 
18
# for fast lookups. Keep in sync with CT_Language() + sorted
 
19
LOCALE_NAMES = ('de', "en", "fr", "pt_BR", "ru")
 
20
 
 
21
#***************************************************************
 
22
#
 
23
#***************************************************************
 
24
class CT_Language(QtCore.QObject):
 
25
    """class wrapping locale names names"""
 
26
 
 
27
    def trUtf8(self, what):
 
28
        def translate():
 
29
            return QtCore.QObject.trUtf8(self, what)
 
30
        return translate
 
31
 
 
32
 
 
33
    def __init__(self):
 
34
        QtCore.QObject.__init__(self)
 
35
 
 
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)")
 
41
 
 
42
 
 
43
 
 
44
#***************************************************************
 
45
#
 
46
#***************************************************************
 
47
class Language(object):
 
48
    """class handling languages for the package"""
 
49
 
 
50
    def __init__(self):
 
51
        """constructor"""
 
52
 
 
53
        # do a lazy initialize here
 
54
        self.__languageStrings = None
 
55
        self.__translator = None
 
56
        self.__defaultLocale = DEFAULT_LOCALE
 
57
 
 
58
 
 
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
 
66
        return haveIt
 
67
 
 
68
 
 
69
    def getDefaultLocaleName(self):
 
70
        """returns the default locale name"""
 
71
        return self.__defaultLocale
 
72
 
 
73
    
 
74
    def setDefaultLocaleName(self, localeName):
 
75
        """sets the default locale to be used by the class
 
76
        
 
77
        @param localeName (string) locale name
 
78
        @raise ValueError if locale name is not supported
 
79
        """
 
80
        if not self.hasLocaleName(localeName):
 
81
            raise ValueError('no such locale: "%s"' % localeName)
 
82
        self.__defaultLocale = localeName
 
83
 
 
84
 
 
85
    def listNames(self):
 
86
        """
 
87
        lists all available locale names and display names
 
88
 
 
89
        @return list[(QString(display-name1), locale-name1), (...)]
 
90
        
 
91
        Note: the list returned is sorted by localeName
 
92
        """
 
93
 
 
94
        if not self.__languageStrings:
 
95
            self.__languageStrings = CT_Language()
 
96
 
 
97
        out = []
 
98
        for localeName in LOCALE_NAMES:
 
99
            tr = getattr(self.__languageStrings, "LANG_" + localeName.upper())()
 
100
            out.append((tr, localeName))
 
101
        return out
 
102
 
 
103
 
 
104
 
 
105
    def getLanguageFile(self, localeName):
 
106
        """returns the full path to the language file for the specified locale
 
107
 
 
108
 
 
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.
 
112
        """
 
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.
 
118
        return fpath
 
119
 
 
120
 
 
121
    def getTranslator(self, parent, localeName):
 
122
        """
 
123
        returns an initialized QTranslator for the specified locale name
 
124
 
 
125
        @param parent parent of the QTranslator or None
 
126
        @param localeName name of the locale
 
127
        
 
128
        If a translator for the specified locale name is not available a default
 
129
        translator is returned. See getDefaultLocale().
 
130
 
 
131
        """
 
132
        translator = QtCore.QTranslator(parent)
 
133
        fpath = self.getLanguageFile(localeName)
 
134
        if fpath:
 
135
            translator.load(fpath)
 
136
        return translator
 
137
 
 
138
 
 
139
    def installTranslator(self, parent, localeName):
 
140
        """
 
141
        Installs a translator according to the specifed locale name
 
142
 
 
143
        @param parent parent of the translator
 
144
        @param localeName name of the locale for the translator
 
145
        @return QTranslator instance
 
146
        
 
147
        
 
148
        The new translator will replace the translator installed in the last
 
149
        call to this method. Use this method to dynamically switch translators.
 
150
        """
 
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
 
157