2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# Copyright (C) 2004 Vincent Hanquez <tab AT snarc.org>
|
|
|
|
# Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
# Copyright (C) 2009 Benjamin Richter <br AT waldteufel-online.net>
|
|
|
|
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2004-05-17 01:47:14 +02:00
|
|
|
|
2018-10-05 00:08:35 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2005-05-31 19:53:28 +02:00
|
|
|
import locale
|
|
|
|
import gettext
|
2009-09-25 17:11:38 +02:00
|
|
|
import unicodedata
|
2018-10-05 00:08:35 +02:00
|
|
|
from pathlib import Path
|
2009-09-25 17:11:38 +02:00
|
|
|
|
2018-04-24 22:54:18 +02:00
|
|
|
DOMAIN = 'gajim'
|
|
|
|
LANG = 'en'
|
2013-08-01 19:05:00 +02:00
|
|
|
direction_mark = '\u200E'
|
2018-10-05 00:08:35 +02:00
|
|
|
_translation = gettext.NullTranslations()
|
2013-07-23 15:55:24 +02:00
|
|
|
|
2009-11-26 12:58:12 +01:00
|
|
|
|
2018-10-05 00:08:35 +02:00
|
|
|
def get_locale_dirs():
|
2018-04-24 22:54:18 +02:00
|
|
|
if os.name == 'nt':
|
2018-10-05 00:08:35 +02:00
|
|
|
return
|
|
|
|
|
2018-04-24 22:54:18 +02:00
|
|
|
path = gettext.find(DOMAIN)
|
2018-10-05 00:08:35 +02:00
|
|
|
if path is not None:
|
|
|
|
# gettext can find the location itself
|
|
|
|
# so we dont need the localedir
|
|
|
|
return
|
|
|
|
|
|
|
|
if Path('/app/share/run-as-flatpak').exists():
|
2018-05-23 08:37:54 +02:00
|
|
|
# Check if we run as flatpak
|
2018-10-05 00:08:35 +02:00
|
|
|
return [Path('/app/share/')]
|
|
|
|
|
|
|
|
data_dirs = os.getenv('XDG_DATA_DIRS')
|
|
|
|
if data_dirs:
|
|
|
|
return list(map(Path, data_dirs.split(':')))
|
|
|
|
return [Path('/usr/local/share/'), Path('/usr/share/')]
|
|
|
|
|
|
|
|
|
|
|
|
def iter_locale_dirs():
|
|
|
|
locale_dirs = get_locale_dirs()
|
|
|
|
if locale_dirs is None:
|
|
|
|
yield None
|
|
|
|
return
|
2018-04-24 22:54:18 +02:00
|
|
|
|
2018-10-05 00:08:35 +02:00
|
|
|
# gettext fallback
|
|
|
|
locale_dirs.append(Path(sys.base_prefix) / 'share')
|
|
|
|
|
|
|
|
found_paths = []
|
|
|
|
for path in locale_dirs:
|
|
|
|
locale_dir = path / 'locale'
|
|
|
|
if locale_dir in found_paths:
|
|
|
|
continue
|
|
|
|
found_paths.append(locale_dir)
|
|
|
|
if locale_dir.is_dir():
|
2018-10-07 18:18:52 +02:00
|
|
|
yield str(locale_dir)
|
2018-04-24 22:54:18 +02:00
|
|
|
|
2019-01-05 15:17:45 +01:00
|
|
|
def get_default_lang():
|
|
|
|
if os.name == "nt":
|
|
|
|
import ctypes
|
|
|
|
windll = ctypes.windll.kernel32
|
|
|
|
lang = locale.windows_locale[windll.GetUserDefaultUILanguage()]
|
|
|
|
return lang[:2]
|
|
|
|
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
from AppKit import NSLocale
|
|
|
|
return NSLocale.currentLocale().languageCode()
|
|
|
|
|
|
|
|
default = locale.getdefaultlocale()[0]
|
|
|
|
if default is not None:
|
|
|
|
return default[:2]
|
|
|
|
return 'en'
|
|
|
|
|
2018-04-24 22:54:18 +02:00
|
|
|
def initialize_direction_mark():
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
global direction_mark
|
|
|
|
|
|
|
|
if Gtk.Widget.get_default_direction() == Gtk.TextDirection.RTL:
|
|
|
|
direction_mark = '\u200F'
|
2017-10-17 16:28:56 +02:00
|
|
|
|
|
|
|
|
2018-04-24 22:54:18 +02:00
|
|
|
def paragraph_direction_mark(text):
|
|
|
|
"""
|
|
|
|
Determine paragraph writing direction according to
|
|
|
|
http://www.unicode.org/reports/tr9/#The_Paragraph_Level
|
|
|
|
|
|
|
|
Returns either Unicode LTR mark or RTL mark.
|
|
|
|
"""
|
|
|
|
for char in text:
|
|
|
|
bidi = unicodedata.bidirectional(char)
|
|
|
|
if bidi == 'L':
|
|
|
|
return '\u200E'
|
2018-09-11 19:27:10 +02:00
|
|
|
if bidi in ('AL', 'R'):
|
2018-04-24 22:54:18 +02:00
|
|
|
return '\u200F'
|
|
|
|
|
|
|
|
return '\u200E'
|
2017-09-08 00:41:30 +02:00
|
|
|
|
2005-08-12 19:53:25 +02:00
|
|
|
|
2010-04-07 03:09:58 +02:00
|
|
|
def Q_(text):
|
|
|
|
"""
|
|
|
|
Translate the given text, optionally qualified with a special
|
|
|
|
construction, which will help translators to disambiguate between
|
|
|
|
same terms, but in different contexts.
|
|
|
|
|
|
|
|
When translated text is returned - this rudimentary construction
|
|
|
|
will be stripped off, if it's present.
|
|
|
|
|
|
|
|
Here is the construction to use:
|
|
|
|
Q_("?vcard:Unknown")
|
|
|
|
|
|
|
|
Everything between ? and : - is the qualifier to convey the context
|
|
|
|
to the translators. Everything after : - is the text itself.
|
|
|
|
"""
|
|
|
|
text = _(text)
|
|
|
|
if text.startswith('?'):
|
2018-09-11 19:27:10 +02:00
|
|
|
text = text.split(':', 1)[1]
|
2010-04-07 03:09:58 +02:00
|
|
|
return text
|
2005-08-24 13:54:47 +02:00
|
|
|
|
2018-04-24 22:54:18 +02:00
|
|
|
|
|
|
|
def ngettext(s_sing, s_plural, n, replace_sing=None, replace_plural=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Use as:
|
2018-09-11 19:27:10 +02:00
|
|
|
i18n.ngettext(
|
|
|
|
'leave room %s', 'leave rooms %s', len(rooms), 'a', 'a, b, c')
|
2008-12-03 22:56:12 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
In other words this is a hack to ngettext() to support %s %d etc..
|
|
|
|
"""
|
2018-10-05 00:08:35 +02:00
|
|
|
text = _translation.ngettext(s_sing, s_plural, n)
|
2010-02-08 15:08:40 +01:00
|
|
|
if n == 1 and replace_sing is not None:
|
|
|
|
text = text % replace_sing
|
|
|
|
elif n > 1 and replace_plural is not None:
|
|
|
|
text = text % replace_plural
|
|
|
|
return text
|
2018-04-24 22:54:18 +02:00
|
|
|
|
|
|
|
|
2018-09-11 19:27:10 +02:00
|
|
|
try:
|
|
|
|
locale.setlocale(locale.LC_ALL, '')
|
|
|
|
except locale.Error as error:
|
2019-02-16 10:36:27 +01:00
|
|
|
print(error, file=sys.stderr)
|
2018-09-11 19:27:10 +02:00
|
|
|
|
2019-01-05 15:17:45 +01:00
|
|
|
try:
|
|
|
|
LANG = get_default_lang()
|
2019-01-19 19:36:50 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
# Set the env var on Windows because gettext.find() uses it to
|
|
|
|
# find the translation
|
2019-04-05 19:39:37 +02:00
|
|
|
# Use LANGUAGE instead of LANG, LANG sets LC_ALL and thus
|
|
|
|
# doesn't retain other region settings like LC_TIME
|
|
|
|
os.environ['LANGUAGE'] = LANG
|
2019-01-05 15:17:45 +01:00
|
|
|
except Exception as error:
|
2019-02-16 10:36:27 +01:00
|
|
|
print('Failed to determine default language', file=sys.stderr)
|
2019-01-05 15:17:45 +01:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2018-09-11 19:27:10 +02:00
|
|
|
|
2018-10-05 00:08:35 +02:00
|
|
|
# Search for the translation in all locale dirs
|
|
|
|
for dir_ in iter_locale_dirs():
|
|
|
|
try:
|
2018-10-07 18:18:52 +02:00
|
|
|
_translation = gettext.translation(DOMAIN, dir_)
|
2018-10-05 00:08:35 +02:00
|
|
|
_ = _translation.gettext
|
2018-10-10 19:33:43 +02:00
|
|
|
if hasattr(locale, 'bindtextdomain'):
|
|
|
|
locale.bindtextdomain(DOMAIN, dir_) # type: ignore
|
2018-10-05 00:08:35 +02:00
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
2018-09-11 19:27:10 +02:00
|
|
|
else:
|
2019-02-16 10:36:27 +01:00
|
|
|
print('No translations found', file=sys.stderr)
|
|
|
|
print('Dirs searched: %s' % get_locale_dirs(), file=sys.stderr)
|
2018-10-05 00:08:35 +02:00
|
|
|
_ = _translation.gettext
|