2014-09-02 17:30:01 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 12:10:42 -07:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2018-04-24 10:07:59 -07:00
|
|
|
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
2014-09-02 12:10:42 -07:00
|
|
|
|
|
|
|
This program 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, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2017-07-10 20:11:04 -06:00
|
|
|
import json
|
|
|
|
import locale
|
|
|
|
import os
|
2016-02-12 14:34:19 -08:00
|
|
|
|
2014-08-26 18:22:59 -07:00
|
|
|
strings = {}
|
2018-09-30 17:06:29 -07:00
|
|
|
translations = {}
|
2014-08-26 18:22:59 -07:00
|
|
|
|
2017-07-10 20:11:04 -06:00
|
|
|
|
2018-09-30 17:06:29 -07:00
|
|
|
def load_strings(common):
|
2015-06-25 21:55:29 +02:00
|
|
|
"""
|
|
|
|
Loads translated strings and fallback to English
|
|
|
|
if the translation does not exist.
|
|
|
|
"""
|
2018-09-30 17:06:29 -07:00
|
|
|
global strings, translations
|
2014-09-19 23:15:20 +00:00
|
|
|
|
2018-09-30 17:06:29 -07:00
|
|
|
# Load all translations
|
2015-07-25 10:37:05 +02:00
|
|
|
translations = {}
|
2018-09-30 17:06:29 -07:00
|
|
|
for locale in common.settings.available_locales:
|
|
|
|
locale_dir = common.get_resource_path('locale')
|
|
|
|
filename = os.path.join(locale_dir, "{}.json".format(locale))
|
|
|
|
with open(filename, encoding='utf-8') as f:
|
|
|
|
translations[locale] = json.load(f)
|
|
|
|
|
|
|
|
# Build strings
|
|
|
|
default_locale = 'en'
|
|
|
|
current_locale = common.settings.get('locale')
|
|
|
|
strings = {}
|
|
|
|
for s in translations[default_locale]:
|
|
|
|
if s in translations[current_locale]:
|
|
|
|
strings[s] = translations[current_locale][s]
|
|
|
|
else:
|
|
|
|
strings[s] = translations[default_locale][s]
|
2014-08-26 18:22:59 -07:00
|
|
|
|
2014-11-18 18:29:32 +01:00
|
|
|
|
2018-09-30 17:47:10 -07:00
|
|
|
def translated(k):
|
2015-06-25 21:55:29 +02:00
|
|
|
"""
|
|
|
|
Returns a translated string.
|
|
|
|
"""
|
2016-02-12 14:34:19 -08:00
|
|
|
return strings[k]
|
2014-08-26 18:22:59 -07:00
|
|
|
|
|
|
|
_ = translated
|