[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 # isort:imports-thirdparty
from gi.repository.Gdk import Display, threads_enter, threads_init, threads_leave from gi.repository.Gdk import Display, threads_enter, threads_init, threads_leave
from gi.repository.GLib import set_prgname 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 import defer, gtk3reactor
from twisted.internet.error import ReactorAlreadyInstalledError from twisted.internet.error import ReactorAlreadyInstalledError
from twisted.internet.task import LoopingCall 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.hostlist import LOCALHOST
from deluge.ui.sessionproxy import SessionProxy from deluge.ui.sessionproxy import SessionProxy
from deluge.ui.tracker_icons import TrackerIcons 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 # isort:imports-localfolder
from .addtorrentdialog import AddTorrentDialog from .addtorrentdialog import AddTorrentDialog
@ -155,7 +155,8 @@ def windowing(like):
class GtkUI(object): class GtkUI(object):
def __init__(self, args): def __init__(self, args):
# Setup gtkbuilder/glade translation # Setup gtkbuilder/glade translation
setup_translations(setup_gettext=False, setup_pygtk=True) setup_translations()
Builder().set_translation_domain(I18N_DOMAIN)
# Setup signals # Setup signals
def on_die(*args): def on_die(*args):

View File

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

View File

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