Python3 + Gtk3 + Windows : Internationalisation de Glade
Bonsoir à tou(te)s,
Je vous écris ce message car je suis un peu désepéré. J'ai passé ma journée à chercher une solution pour avoir un fonctionnement multiplateforme de l'internationalisation des fichiers Glade.
Le problème est simple, sur Linux un simple
Code:
1 2
| self.builder = Gtk.Builder()
self.builder.set_translation_domain(MON_DOMAINE) |
fonctionne.
Mais sur Windows, impossible d'avoir une traduction des fichiers Glade, alors que le reste fonctionne avec gettext.
Sur beaucoup de forums ils indiquent qu'il faut utiliser le module locale avec Glade et la fonction
Code:
locale.bindtextdomain(MON_DOMAINE,MON_DOSSIER)
. Le problème c'est que cette fonction n'existe plus !
Pour l'instant j'en suis là :
Sur windows mes fichiers mo sont localisé ici : C:\locale\fr\LC_MESSAGES
Le code de mon fichier main est celui-ci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from gi.repository import Gtk, GObject, Gdk, GLib
from src.gui.mainwindow import MainWindow
import os
import locale
import os
import sys
import gettext
######
# PubliPhoto version alpha 1.0
# Créé par *******
# Licence : GNU GPL v3 ou suppérieure
# Contact : contacter [dot] oradisson [dot] fr
######
# setup translation support
if sys.platform.startswith('linux'):
(lang_code, encoding) = locale.getlocale()
LOCALE_DOMAIN = 'publiphoto'
LOCALE_DIR = os.path.join(sys.prefix, 'share', 'locale')
gettext.bindtextdomain(LOCALE_DOMAIN, LOCALE_DIR) # (1)
gettext.textdomain(LOCALE_DOMAIN) # (2)
gettext.install(LOCALE_DOMAIN) # (3)
else:
os.environ['LC_MESSAGES'] = locale.getdefaultlocale()[0]
APP_NAME = "publiphoto"
local_path = os.path.realpath( os.path.dirname( sys.argv[0] ) )
local_path = os.path.join( "C:/", 'locale' )
print(local_path)
# Récupère la langue du système.
os.environ['LANGUAGE'] = 'fr_FR'
os.environ['LANG'] = 'fr_FR'
os.putenv('fr_FR', 'LANG')
print(locale.LC_ALL)
locale.setlocale(locale.LC_ALL,'French')
print(locale.getlocale())
lc, encoding = locale.getlocale()
gettext.bindtextdomain( APP_NAME, local_path )
gettext.bind_textdomain_codeset(APP_NAME,"UTF-8")
gettext.textdomain( APP_NAME )
# On crée notre fonction de translation. J'imagine qu'il y a un lien entre
# entre la liste des langages et les deux lignes du dessus.
#lang = gettext.translation( APP_NAME, local_path, languages=[ lc ], fallback = True)
#gettext.install(APP_NAME)
#fallback = true évite qu'une Exception IOError sois relevée quand
# APP_NAME n'est pas trouvé.
# Gtk.bindtextdomain(LOCALE_DOMAIN, LOCALE_DIR) # (4)
# Gtk.textdomain(LOCALE_DOMAIN) # (5)
# Set the current directory to match files
os.chdir(os.path.dirname(os.path.abspath(__file__)))
window = MainWindow()
window.show_all()
Gtk.main() |
Le code de mon fichier qui charge les fichiers glade est celui-ci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| # -*- coding: utf-8 -*-
from gi.repository import Gtk
from src.tools import root_path
class View(Gtk.Alignment):
"""
This class define a view and a constructor
which load a glade view and connect theses signals
"""
def __init__(self,gladeFile,viewName):
Gtk.Alignment.__init__(self)
## Load from Glade
self.builder = Gtk.Builder()
#self.builder.bindtextdomain('C:\locale','publiphoto')
self.builder.set_translation_domain('publiphoto.mo')
self.builder.add_from_file(gladeFile)
self.builder.connect_signals(self)
self.viewBox = self.builder.get_object(viewName)
try:
self.viewBox.unparent()
except AttributeError as e:
print(e)
self.add(self.viewBox)
def load_objects(self,object_liste):
"""
Return a dictionary contains objects loaded
"""
result = dict()
for object in object_liste:
result[object] = self.builder.get_object(object)
return result |
Avez vous une idée de solution ?
Merci d'avance.
Olivier.