2014-09-02 17:30:01 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 12:10:42 -07:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2017-01-06 18:58:15 -08:00
|
|
|
Copyright (C) 2017 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 = {}
|
|
|
|
|
2017-07-10 20:11:04 -06:00
|
|
|
|
2017-05-16 11:05:48 -07:00
|
|
|
def load_strings(common, default="en"):
|
2015-06-25 21:55:29 +02:00
|
|
|
"""
|
|
|
|
Loads translated strings and fallback to English
|
|
|
|
if the translation does not exist.
|
|
|
|
"""
|
2014-08-26 18:22:59 -07:00
|
|
|
global strings
|
2014-09-19 23:15:20 +00:00
|
|
|
|
|
|
|
# find locale dir
|
2017-05-16 11:05:48 -07:00
|
|
|
locale_dir = common.get_resource_path('locale')
|
2014-09-19 23:15:20 +00:00
|
|
|
|
|
|
|
# load all translations
|
2015-07-25 10:37:05 +02:00
|
|
|
translations = {}
|
2014-09-19 23:15:20 +00:00
|
|
|
for filename in os.listdir(locale_dir):
|
|
|
|
abs_filename = os.path.join(locale_dir, filename)
|
|
|
|
lang, ext = os.path.splitext(filename)
|
2017-07-10 20:11:04 -06:00
|
|
|
if ext == '.json':
|
2017-05-23 21:22:14 +10:00
|
|
|
with open(abs_filename, encoding='utf-8') as f:
|
2017-07-10 20:11:04 -06:00
|
|
|
translations[lang] = json.load(f)
|
2014-09-19 23:15:20 +00:00
|
|
|
|
2015-07-25 10:37:05 +02:00
|
|
|
strings = translations[default]
|
2014-08-26 18:22:59 -07:00
|
|
|
lc, enc = locale.getdefaultlocale()
|
|
|
|
if lc:
|
|
|
|
lang = lc[:2]
|
2015-07-25 10:37:05 +02:00
|
|
|
if lang in translations:
|
2014-08-26 18:22:59 -07:00
|
|
|
# if a string doesn't exist, fallback to English
|
2015-07-25 10:37:05 +02:00
|
|
|
for key in translations[default]:
|
|
|
|
if key in translations[lang]:
|
|
|
|
strings[key] = translations[lang][key]
|
2014-08-26 18:22:59 -07:00
|
|
|
|
2014-11-18 18:29:32 +01:00
|
|
|
|
2014-09-16 02:09:37 +00:00
|
|
|
def translated(k, gui=False):
|
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
|