multisplit
[iramuteq] / langue.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
5 #License: GNU/GPL
6
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
10 import os, sys
11 from configparser import ConfigParser, RawConfigParser
12 import gettext
13
14 #------------------------------------
15 # import des modules wx
16 #------------------------------------
17 import wx
18
19 #------------------------------------
20 # import des fichiers du projet
21 #------------------------------------
22 from chemins import ConstructConfigPath, ConstructDicoPath, ConstructGlobalPath, PathOut
23
24 #------------------------------------
25 #pour le mki18n
26 #------------------------------------
27 def _init():
28     global code_langues
29     global langues
30     global langueUI
31     global preslangue
32
33     # chemin de l'application
34     AppliPath = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv[0])))
35     # preferences par defaut
36     DefaultConf = ConfigParser()
37     DictConfigPath = ConstructGlobalPath(AppliPath)
38     ConfigGlob = ConfigParser()
39     ConfigGlob.read(DictConfigPath['global'])
40     DefaultConf.read(DictConfigPath['preferences'])
41     # repertoire de l'utilisateur
42     user_home = os.getenv('HOME')
43     if user_home is None :
44         user_home = os.path.expanduser('~')
45
46     # chemin des préférences
47     UserConfigPath = os.path.abspath(os.path.join(user_home, '.iramuteq-%s' % ConfigGlob.get('DEFAULT', 'version_nb')))
48     ConfigPath = ConstructConfigPath(UserConfigPath)
49
50     # dictionnaires des langues disponibles pour l'interface
51     langues = {'french' : wx.LANGUAGE_FRENCH,
52                'english' : wx.LANGUAGE_ENGLISH,
53                'portuguese' : wx.LANGUAGE_PORTUGUESE,
54                'italian' : wx.LANGUAGE_ITALIAN,
55                'spanish' : wx.LANGUAGE_SPANISH
56                }
57     code_langues = {'french' : 'fr_FR',
58                'english' : 'en',
59                'portuguese' : 'pt_PT',
60                'italian' : 'it_IT',
61                'spanish' : 'es_ES'
62                }
63     # langue par defaut de l'interface (celle des messages écrits dans le code)
64     langueUI = 'english'
65     # dictionnaire des objets "traduction"
66     preslangue = {}
67     for langue in code_langues :
68         preslangue[langue] = gettext.translation('iramuteq',\
69             os.path.join(AppliPath,'locale'),\
70             languages=[code_langues[langue]])
71     ConfigPath = ConstructConfigPath(UserConfigPath)
72     prefGlob = RawConfigParser()
73     prefGlob.read(ConfigPath['preferences'])
74     # guilanguage est l'appelation originale dans iramuteq, renommé en 'langageUI' dans le code en P3
75     try :
76         langueUI = prefGlob.get('iramuteq', 'guilanguage')
77     except :
78         langueUI = DefaultConf.get('iramuteq', 'guilanguage')
79
80 # la fonction appelée depuis tous les fichiers de module qui ont besoin du système de traduction
81 # elle appelle la fonction d'initialisation si il y a lieu
82
83 def run():
84     try:
85         langueUI
86     except:
87         _init()
88     preslangue[langueUI].install()
89
90
91 #------------------------------------
92 # héritage de P2
93 #------------------------------------
94
95 # fonction présente dans iramuteq.py
96 # self fait référence à une IraFrame, dérivé de wx.Frame
97 #fonction conservée de l'ancienne version pour le cas ou, mais qui n'est pas utilisée en principe
98 def setlangue(self) :
99     self.pref.read(self.ConfigPath['preferences'])
100     try :
101         langueUI = self.pref.get('iramuteq', 'guilanguage')
102     except :
103         langueUI = DefaultConf.get('iramuteq', 'guilanguage')
104     self.preslangue.get(langueUI, 'english').install()