diff options
author | Calum Lind <calumlind+deluge@gmail.com> | 2019-05-10 12:58:33 +0100 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2019-05-11 20:40:20 +0100 |
commit | d417c4b0f9fd0702ebf986546ab884d2a9517e4e (patch) | |
tree | 15c9d05c71f87bd3fc777359abff7119e2cc2934 /deluge/i18n | |
parent | 653f80eac8593703bda727ca85d955dd1973ca1a (diff) | |
download | deluge-d417c4b0f9fd0702ebf986546ab884d2a9517e4e.tar.gz deluge-d417c4b0f9fd0702ebf986546ab884d2a9517e4e.tar.bz2 deluge-d417c4b0f9fd0702ebf986546ab884d2a9517e4e.zip |
[Core] Refactor the base argparser and translation code.
- Move baseargparser out of deluge/ui since it is also used by the
Daemon and could cause packaging issues if UI code is not available.
- Renamed baseargparser to argparserbase to follow existing Deluge
naming.
- Renamed get_version to distinguish from deluge.common.get_version.
- Translation code is usable by more than just the UIs so also move it
to Deluge namespace and re-use i18n directory and make it a package.
- Renamed setup_translations to singular as it felt more correct.
- Renamed set_dummy_trans to be more descriptive.
Closes: #3081
Diffstat (limited to 'deluge/i18n')
-rw-r--r-- | deluge/i18n/__init__.py | 15 | ||||
-rw-r--r-- | deluge/i18n/languages.py | 117 | ||||
-rw-r--r-- | deluge/i18n/util.py | 133 |
3 files changed, 265 insertions, 0 deletions
diff --git a/deluge/i18n/__init__.py b/deluge/i18n/__init__.py new file mode 100644 index 000000000..701d2afa6 --- /dev/null +++ b/deluge/i18n/__init__.py @@ -0,0 +1,15 @@ +from .util import ( + I18N_DOMAIN, + get_languages, + set_language, + setup_mock_translation, + setup_translation, +) + +__all__ = [ + 'I18N_DOMAIN', + 'set_language', + 'get_languages', + 'setup_translation', + 'setup_mock_translation', +] diff --git a/deluge/i18n/languages.py b/deluge/i18n/languages.py new file mode 100644 index 000000000..49dc53026 --- /dev/null +++ b/deluge/i18n/languages.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- +# +# This file is public domain. +# + +from __future__ import unicode_literals + +# Language code for this installation. All choices can be found here: +# http://www.i18nguy.com/unicode/language-identifiers.html +LANGUAGE_CODE = 'en-us' + + +# Deferred translation +def _(message): + return message + + +# Languages we provide translations for, out of the box. +LANGUAGES = { + 'af': _('Afrikaans'), + 'ar': _('Arabic'), + 'ast': _('Asturian'), + 'az': _('Azerbaijani'), + 'bg': _('Bulgarian'), + 'be': _('Belarusian'), + 'bn': _('Bengali'), + 'br': _('Breton'), + 'bs': _('Bosnian'), + 'ca': _('Catalan'), + 'cs': _('Czech'), + 'cy': _('Welsh'), + 'da': _('Danish'), + 'de': _('German'), + 'el': _('Greek'), + 'en': _('English'), + 'en_AU': _('English (Australia)'), + 'en_CA': _('English (Canada)'), + 'en_GB': _('English (United Kingdom)'), + 'eo': _('Esperanto'), + 'es': _('Spanish'), + 'es-ar': _('Argentinian Spanish'), + 'es-mx': _('Mexican Spanish'), + 'es-ni': _('Nicaraguan Spanish'), + 'es-ve': _('Venezuelan Spanish'), + 'et': _('Estonian'), + 'eu': _('Basque'), + 'fa': _('Persian'), + 'fi': _('Finnish'), + 'fr': _('French'), + 'fy': _('Frisian'), + 'ga': _('Irish'), + 'gl': _('Galician'), + 'he': _('Hebrew'), + 'hi': _('Hindi'), + 'hr': _('Croatian'), + 'hu': _('Hungarian'), + 'ia': _('Interlingua'), + 'id': _('Indonesian'), + 'is': _('Icelandic'), + 'it': _('Italian'), + 'iu': _('Inuktitut'), + 'ja': _('Japanese'), + 'ka': _('Georgian'), + 'kk': _('Kazakh'), + 'km': _('Khmer'), + 'kn': _('Kannada'), + 'ko': _('Korean'), + 'ku': _('Kurdish'), + 'la': _('Latin'), + 'lb': _('Luxembourgish'), + 'lt': _('Lithuanian'), + 'lv': _('Latvian'), + 'mk': _('Macedonian'), + 'ml': _('Malayalam'), + 'mn': _('Mongolian'), + 'ms': _('Mayaly'), + 'my': _('Burmese'), + 'nb': _('Norwegian Bokmal'), + 'ne': _('Nepali'), + 'nds': _('Low German'), + 'nl': _('Dutch'), + 'nn': _('Norwegian Nynorsk'), + 'os': _('Ossetic'), + 'pa': _('Punjabi'), + 'pl': _('Polish'), + 'pms': _('Piedmontese'), + 'pt': _('Portuguese'), + 'pt_BR': _('Brazilian Portuguese'), + 'ro': _('Romanian'), + 'ru': _('Russian'), + 'sk': _('Slovak'), + 'sl': _('Slovenian'), + 'si': _('Sinhalese'), + 'sq': _('Albanian'), + 'sr': _('Serbian'), + 'sr-latn': _('Serbian Latin'), + 'sv': _('Swedish'), + 'sw': _('Swahili'), + 'ta': _('Tamil'), + 'te': _('Telugu'), + 'th': _('Thai'), + 'tl': _('Tagalog'), + 'tlh': _('Klingon'), + 'tr': _('Turkish'), + 'tt': _('Tatar'), + 'udm': _('Udmurt'), + 'uk': _('Ukrainian'), + 'ur': _('Urdu'), + 'vi': _('Vietnamese'), + 'zh_CN': _('Chinese (Simplified)'), + 'zh_HK': _('Chinese (Hong Kong)'), + 'zh-hans': _('Simplified Chinese'), + 'zh-hant': _('Traditional Chinese'), + 'zh_TW': _('Chinese (Taiwan)'), +} + +del _ diff --git a/deluge/i18n/util.py b/deluge/i18n/util.py new file mode 100644 index 000000000..a2f287645 --- /dev/null +++ b/deluge/i18n/util.py @@ -0,0 +1,133 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2007,2008 Andrew Resch <andrewresch@gmail.com> +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +from __future__ import unicode_literals + +import ctypes +import gettext +import locale +import logging +import os +import sys + +from six.moves import builtins + +import deluge.common + +from .languages import LANGUAGES + +log = logging.getLogger(__name__) + +I18N_DOMAIN = 'deluge' + + +def get_translations_path(): + """Get the absolute path to the directory containing translation files""" + return deluge.common.resource_filename('deluge', 'i18n') + + +def get_languages(): + lang = [] + + translations_path = get_translations_path() + for root, dirs, files in os.walk(translations_path): + # Get the dirs + lang_dirs = dirs + break + else: + return lang + + for i, lang_code in enumerate(lang_dirs): + name = '%s (Language name missing)' % lang_code + if lang_code in LANGUAGES: + name = LANGUAGES[lang_code] + lang.append([lang_code, _(name)]) + + lang = sorted(lang, key=lambda l: l[1]) + return lang + + +def set_language(lang): + """ + Set the language to use. + + gettext and GtkBuilder will load the translations from the specified + language. + + :param lang: the language, e.g. "en", "de" or "en_GB" + :type lang: str + """ + # Necessary to set these environment variables for GtkBuilder + deluge.common.set_env_variable('LANGUAGE', lang) # Windows/Linux + deluge.common.set_env_variable('LANG', lang) # For OSX + + translations_path = get_translations_path() + try: + ro = gettext.translation( + 'deluge', localedir=translations_path, languages=[lang] + ) + ro.install() + except IOError as ex: + log.warning('IOError when loading translations: %s', ex) + + +def setup_mock_translation(warn_msg=None): + def _func(*txt): + if warn_msg: + log.warning( + '"%s" has been marked for translation, but translation is unavailable.', + txt[0], + ) + return txt[0] + + builtins.__dict__['_'] = _func + builtins.__dict__['ngettext'] = builtins.__dict__['_n'] = _func + + +# Initialize gettext +def setup_translation(): + translations_path = get_translations_path() + log.info('Setting up translations from %s', translations_path) + + try: + if hasattr(locale, 'bindtextdomain'): + locale.bindtextdomain(I18N_DOMAIN, translations_path) + if hasattr(locale, 'textdomain'): + locale.textdomain(I18N_DOMAIN) + + gettext.bindtextdomain(I18N_DOMAIN, translations_path) + gettext.bind_textdomain_codeset(I18N_DOMAIN, 'UTF-8') + gettext.textdomain(I18N_DOMAIN) + + # Workaround for Python 2 unicode gettext (keyword removed in Py3). + kwargs = {} if not deluge.common.PY2 else {'unicode': True} + + gettext.install(I18N_DOMAIN, translations_path, names='ngettext', **kwargs) + 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: + log.error('Unable to initialize gettext/locale!') + log.exception(ex) + setup_mock_translation() + + deluge.common.translate_size_units() |