Salome HOME
etat provisoire pour sauvegarde avant vacances. non stable
[tools/eficas.git] / Extensions / localisation.py
1 # -*- coding: utf-8 -*-
2 # copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
4 #
5 # This program is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 2.1 of the License, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License along
16 # with this program. If not, see <http://www.gnu.org/licenses/>.
17 """
18 Creates and loads two ``QTranslator`` objects, one for pure Qt, one for Eficas,
19 and installs them to a ``QApplication``.
20
21 ``PyQt4`` is currently supported.
22 """
23
24 from PyQt4.QtCore import QTranslator
25
26 qt_translator = QTranslator()
27 eficas_translator = QTranslator()
28
29 def localise(application, locale=None ):
30     """
31     localise(QApplication) -> None
32
33     Loads and installs to a ``QApplication`` two ``QTranslator``
34     objects, one for pure Qt for translating the strings 
35     available in Qt, and one or Eficas, for translating 
36     the strings specified precisely for Eficas.
37     If the Qt base translator cannot be loaded with the locale
38     specified by the user, one attempts to load Qt base 
39     translator with the system locale.
40     If no locale is specified by the user, the system locale
41     is used instead, for both Qt base and Eficas translators.
42     """
43     from PyQt4.QtCore import QLibraryInfo, QTextCodec
44     
45     #QTextCodec.setCodecForTr(QTextCodec.codecForName("utf-8"))
46     
47     from PyQt4.QtCore import QLocale
48     from PyQt4.QtGui import QApplication
49     sys_locale = QLocale.system().name()
50
51     if locale is None:
52         #locale = sys_locale
53         locale="fr"
54
55     #global qt_translator
56     #if qt_translator.load("qt_" + locale,
57     #                      QLibraryInfo.location(QLibraryInfo.TranslationsPath)):
58     #    application.installTranslator(qt_translator)
59     #elif qt_translator.load("qt_" + sys_locale,
60     #                        QLibraryInfo.location(QLibraryInfo.TranslationsPath)):
61     #    print "Qt base translator with default locale loaded!"
62     #    application.installTranslator(qt_translator)
63         # Try to load Qt base translator according to system locale.
64     #else:
65     #    print "Unable to load Qt base translator!"
66     
67     global eficas_translator
68     import os
69     monPath=os.path.join(os.path.dirname(__file__),'..','UiQT4')
70     if eficas_translator.load("eficas_" + locale, monPath):
71         QApplication.installTranslator(eficas_translator)
72     else:
73         print "Unable to load Eficas translator!"
74
75     
76
77 if __name__ == "__main__":
78     import sys
79     localise(sys.argv[1])