Salome HOME
sauvegarde du 28/07
[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 """
22
23 import os
24 from determine import monEnvQT5
25 if monEnvQT5 :
26    from PyQt5.QtCore import QTranslator
27 else :
28    from PyQt4.QtCore import QTranslator
29
30 qt_translator = QTranslator()
31 eficas_translator = QTranslator()
32
33 def localise(application, locale=None,file=None ):
34     """
35     localise(QApplication) -> None
36
37     Loads and installs to a ``QApplication`` two ``QTranslator``
38     objects, one for pure Qt for translating the strings 
39     available in Qt, and one or Eficas, for translating 
40     the strings specified precisely for Eficas.
41     If the Qt base translator cannot be loaded with the locale
42     specified by the user, one attempts to load Qt base 
43     translator with the system locale.
44     If no locale is specified by the user, the system locale
45     is used instead, for both Qt base and Eficas translators.
46     """
47     if monEnvQT5 :
48        from PyQt5.QtCore import QLibraryInfo
49        from PyQt5.QtCore import QLocale
50        from PyQt5.QtWidgets import QApplication
51        monPath=os.path.join(os.path.dirname(__file__),'..','UiQT5')
52     else :
53        from PyQt4.QtCore import QLibraryInfo
54        from PyQt4.QtCore import QLocale
55        from PyQt4.QtGui import QApplication
56        monPath=os.path.join(os.path.dirname(__file__),'..','UiQT4')
57
58     sys_locale = QLocale.system().name()
59
60     if locale is None:
61         #locale = sys_locale
62         locale="fr"
63
64     #global qt_translator
65     #if qt_translator.load("qt_" + locale,
66     #                      QLibraryInfo.location(QLibraryInfo.TranslationsPath)):
67     #    application.installTranslator(qt_translator)
68     #elif qt_translator.load("qt_" + sys_locale,
69     #                        QLibraryInfo.location(QLibraryInfo.TranslationsPath)):
70     #    print "Qt base translator with default locale loaded!"
71     #    application.installTranslator(qt_translator)
72         # Try to load Qt base translator according to system locale.
73     #else:
74     #    print "Unable to load Qt base translator!"
75     
76     global eficas_translator
77     if locale=="ang" : locale="en"
78     #print "eficas_" + locale, monPath
79     if file != None :
80        print 'chagrement de ', file,monPath
81        print eficas_translator.load(file,monPath)
82        print QApplication.installTranslator(eficas_translator)
83        return
84      
85
86     if eficas_translator.load("eficas_" + locale, monPath):
87         QApplication.installTranslator(eficas_translator)
88         print "chargement eficas_", locale, monPath
89     else:
90         print "Unable to load Eficas translator!"
91
92     
93
94 if __name__ == "__main__":
95     import sys
96     localise(sys.argv[1])