2014-09-02 17:30:01 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 12:10:42 -07:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2022-01-16 16:15:49 -08:00
|
|
|
Copyright (C) 2014-2022 Micah Lee, et al. <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 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
|
|
|
|
2020-10-13 17:28:54 -07:00
|
|
|
def load_strings(common, locale_dir):
|
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:
|
2019-10-20 10:15:16 -07:00
|
|
|
filename = os.path.join(locale_dir, f"{locale}.json")
|
2019-10-12 21:01:25 -07:00
|
|
|
with open(filename, encoding="utf-8") as f:
|
2018-09-30 17:06:29 -07:00
|
|
|
translations[locale] = json.load(f)
|
|
|
|
|
|
|
|
# Build strings
|
2019-10-12 21:01:25 -07:00
|
|
|
default_locale = "en"
|
|
|
|
current_locale = common.settings.get("locale")
|
2018-09-30 17:06:29 -07:00
|
|
|
strings = {}
|
|
|
|
for s in translations[default_locale]:
|
2021-10-27 11:47:17 +02:00
|
|
|
if (
|
|
|
|
current_locale in translations
|
|
|
|
and s in translations[current_locale]
|
|
|
|
and translations[current_locale][s] != ""
|
|
|
|
):
|
2018-09-30 17:06:29 -07:00
|
|
|
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
|
|
|
|
2019-10-12 21:01:25 -07:00
|
|
|
|
2014-08-26 18:22:59 -07:00
|
|
|
_ = translated
|