[GTK3] Fix gettext translation code

Add translation setup for Gtk.Builder ui files.

Refactor and cleanup up the translations_util:
- Remove old gtk.glade code.
- Add macos libintl support.
- Remove unneeded setup_translations parameters.
This commit is contained in:
Calum Lind 2018-09-20 09:07:16 +01:00 committed by Calum Lind
parent a2857a318d
commit ac5db1b262
3 changed files with 38 additions and 49 deletions

View File

@ -24,7 +24,7 @@ gi.require_version('Gdk', '3.0') # NOQA: E402
# isort:imports-thirdparty
from gi.repository.Gdk import Display, threads_enter, threads_init, threads_leave
from gi.repository.GLib import set_prgname
from gi.repository.Gtk import ResponseType
from gi.repository.Gtk import Builder, ResponseType
from twisted.internet import defer, gtk3reactor
from twisted.internet.error import ReactorAlreadyInstalledError
from twisted.internet.task import LoopingCall
@ -51,7 +51,7 @@ from deluge.ui.client import client
from deluge.ui.hostlist import LOCALHOST
from deluge.ui.sessionproxy import SessionProxy
from deluge.ui.tracker_icons import TrackerIcons
from deluge.ui.translations_util import set_language, setup_translations
from deluge.ui.translations_util import I18N_DOMAIN, set_language, setup_translations
# isort:imports-localfolder
from .addtorrentdialog import AddTorrentDialog
@ -155,7 +155,8 @@ def windowing(like):
class GtkUI(object):
def __init__(self, args):
# Setup gtkbuilder/glade translation
setup_translations(setup_gettext=False, setup_pygtk=True)
setup_translations()
Builder().set_translation_domain(I18N_DOMAIN)
# Setup signals
def on_die(*args):

View File

@ -9,6 +9,7 @@
from __future__ import unicode_literals
import ctypes
import gettext
import locale
import logging
@ -24,6 +25,8 @@ log.addHandler(
logging.NullHandler()
) # Silence: No handlers could be found for logger "deluge.util.lang"
I18N_DOMAIN = 'deluge'
def set_dummy_trans(warn_msg=None):
def _func(*txt):
@ -91,58 +94,43 @@ def set_language(lang):
# Initialize gettext
def setup_translations(setup_gettext=True, setup_pygtk=False):
def setup_translations():
translations_path = get_translations_path()
domain = 'deluge'
log.info('Setting up translations from %s', translations_path)
if setup_pygtk:
try:
log.info('Setting up GTK translations from %s', translations_path)
try:
if hasattr(locale, 'bindtextdomain'):
locale.bindtextdomain(I18N_DOMAIN, translations_path)
if hasattr(locale, 'textdomain'):
locale.textdomain(I18N_DOMAIN)
if deluge.common.windows_check():
import ctypes
gettext.bindtextdomain(I18N_DOMAIN, translations_path)
gettext.bind_textdomain_codeset(I18N_DOMAIN, 'UTF-8')
gettext.textdomain(I18N_DOMAIN)
try:
libintl = ctypes.cdll.intl
except WindowsError:
# Fallback to named dll.
libintl = ctypes.cdll.LoadLibrary('libintl-8.dll')
# Workaround for Python 2 unicode gettext (keyword removed in Py3).
kwargs = {} if not deluge.common.PY2 else {'unicode': True}
libintl.bindtextdomain(
domain, translations_path.encode(sys.getfilesystemencoding())
)
libintl.textdomain(domain)
libintl.bind_textdomain_codeset(domain, 'UTF-8')
libintl.gettext.restype = ctypes.c_char_p
gettext.install(I18N_DOMAIN, translations_path, names='ngettext', **kwargs)
builtins.__dict__['_n'] = builtins.__dict__['ngettext']
# Use glade for plugins that still uses it
import gtk
import gtk.glade
libintl = None
if deluge.common.windows_check():
libintl = ctypes.cdll.LoadLibrary('libintl-8.dll')
elif deluge.common.osx_check():
libintl = ctypes.cdll.LoadLibrary('libintl.dylib')
gtk.glade.bindtextdomain(domain, translations_path)
gtk.glade.textdomain(domain)
except Exception as ex:
log.error('Unable to initialize glade translation: %s', ex)
if setup_gettext:
try:
if hasattr(locale, 'bindtextdomain'):
locale.bindtextdomain(domain, translations_path)
if hasattr(locale, 'textdomain'):
locale.textdomain(domain)
if libintl:
libintl.bindtextdomain(
I18N_DOMAIN, translations_path.encode(sys.getfilesystemencoding())
)
libintl.textdomain(I18N_DOMAIN)
libintl.bind_textdomain_codeset(I18N_DOMAIN, 'UTF-8')
libintl.gettext.restype = ctypes.c_char_p
gettext.bindtextdomain(domain, translations_path)
gettext.bind_textdomain_codeset(domain, 'UTF-8')
gettext.textdomain(domain)
except Exception as ex:
log.error('Unable to initialize gettext/locale!')
log.exception(ex)
set_dummy_trans()
# Workaround for Python 2 unicode gettext (keyword removed in Py3).
kwargs = {} if not deluge.common.PY2 else {'unicode': True}
gettext.install(domain, translations_path, names='ngettext', **kwargs)
builtins.__dict__['_n'] = builtins.__dict__['ngettext']
except Exception as ex:
log.error('Unable to initialize gettext/locale!')
log.exception(ex)
set_dummy_trans()
deluge.common.translate_size_units()
deluge.common.translate_size_units()

View File

@ -669,7 +669,7 @@ class DelugeWeb(component.Component):
# Strip away slashes and serve on the base path as well as root path
self.top_level.putChild(self.base.strip('/'), self.top_level)
setup_translations(setup_gettext=True, setup_pygtk=False)
setup_translations()
# Remove twisted version number from 'server' http-header for security reasons
server.version = 'TwistedWeb'