diff options
author | Micah Lee <micah@micahflee.com> | 2018-09-30 17:06:29 -0700 |
---|---|---|
committer | Micah Lee <micah@micahflee.com> | 2018-09-30 17:06:29 -0700 |
commit | c4f776c42ada8b6a8c2c8326dc91d3aa87670675 (patch) | |
tree | 03bd7129b0779bd5229f9267a4e1c39d32771a2b /onionshare | |
parent | 23c55bc95b2c99a6efad03f0c6c0fda91c18cf26 (diff) | |
download | onionshare-c4f776c42ada8b6a8c2c8326dc91d3aa87670675.tar.gz onionshare-c4f776c42ada8b6a8c2c8326dc91d3aa87670675.zip |
Set OnionShare language based on the locale stored in settings, and prompt user to restart OnionShare after changing their language
Diffstat (limited to 'onionshare')
-rw-r--r-- | onionshare/__init__.py | 9 | ||||
-rw-r--r-- | onionshare/strings.py | 41 |
2 files changed, 26 insertions, 24 deletions
diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 715c5571..2f0ef14c 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -33,7 +33,11 @@ def main(cwd=None): """ common = Common() + # Load settings right away, in order to get locale setting for strings + common.load_settings(config) strings.load_strings(common) + + # Display OnionShare banner print(strings._('version_string').format(common.version)) # OnionShare CLI in OSX needs to change current working directory (#132) @@ -88,8 +92,9 @@ def main(cwd=None): if not valid: sys.exit() - # Load settings - common.load_settings(config) + # Re-load settings, if a custom config was passed in + if config: + common.load_settings(config) # Debug mode? common.debug = debug diff --git a/onionshare/strings.py b/onionshare/strings.py index 3e9df56d..edae7849 100644 --- a/onionshare/strings.py +++ b/onionshare/strings.py @@ -22,36 +22,33 @@ import locale import os strings = {} +translations = {} -def load_strings(common, default="en"): +def load_strings(common): """ Loads translated strings and fallback to English if the translation does not exist. """ - global strings + global strings, translations - # find locale dir - locale_dir = common.get_resource_path('locale') - - # load all translations + # Load all translations translations = {} - for filename in os.listdir(locale_dir): - abs_filename = os.path.join(locale_dir, filename) - lang, ext = os.path.splitext(filename) - if ext == '.json': - with open(abs_filename, encoding='utf-8') as f: - translations[lang] = json.load(f) - - strings = translations[default] - lc, enc = locale.getdefaultlocale() - if lc: - lang = lc[:2] - if lang in translations: - # if a string doesn't exist, fallback to English - for key in translations[default]: - if key in translations[lang]: - strings[key] = translations[lang][key] + 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] def translated(k, gui=False): |