Skip to content

Commit

Permalink
Workaround issue with gtk3 translation on windows.
Browse files Browse the repository at this point in the history
Windows with python3 and Gtk3/pygi has this really nasty bug that translations
are actually working, but somehow the enconding is messed up by Gtk. This might
be fixed in newer versions of Gtk/pygi, but currently, we are stuck with 3.24
on Windows.

The idea of the workaround here is to parse the glade xml, and translate in
python all translatable strings, dump the xml on a temp file and then
load the interface from this tempfile. Works like a charm

Other references from a user with the same issue
https://stackoverflow.com/questions/32037573/
https://sourceforge.net/p/pygobjectwin32/tickets/22/
https://bugzilla.gnome.org/show_bug.cgi?id=753991

And the source of the workaround

tobias47n9e/pygobject-locale#1 (comment)

Change-Id: I7f381e4de0ad9796e27d4c8e1c2c9e2331f25e91
  • Loading branch information
romaia committed Sep 6, 2017
1 parent 587fbed commit ae00516
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions kiwi/ui/builderloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#

import logging
import platform

from gi.repository import Gtk

Expand Down Expand Up @@ -90,6 +91,30 @@ def __init__(self, view, gladefile=None, domain=None, data=None):
if domain is not None:
self._builder.set_translation_domain(domain)

if platform.system() == 'Windows' and gladefile:
# Windows with python3 and Gtk3/pygi has this really nasty bug that
# translations are actually working, but somehow the enconding is
# messed up by Gtk. This might be fixed in newer versions of
# Gtk/pygi, but currently, we are stuck with 3.24 on Windows. Other
# References from a user with the same issue
# https://stackoverflow.com/questions/32037573/
# https://sourceforge.net/p/pygobjectwin32/tickets/22/
# https://bugzilla.gnome.org/show_bug.cgi?id=753991
# And the source of the workaround
# https://github.com/tobias47n9e/pygobject-locale/issues/1#issuecomment-222287650
import xml.etree.ElementTree as ET
from io import BytesIO
import gettext
tree = ET.parse(gladefile)
for node in tree.iter():
if 'translatable' in node.attrib:
del node.attrib['translatable']
node.text = gettext.dgettext(domain, node.text)
temp_file = BytesIO()
tree.write(temp_file, encoding='utf-8', xml_declaration=True)
data = temp_file.getvalue().decode()
gladefile = None

if gladefile is not None:
self._builder.add_from_file(gladefile)
elif data is not None:
Expand Down

0 comments on commit ae00516

Please sign in to comment.