Set OnionShare language based on the locale stored in settings, and prompt user to restart OnionShare after changing their language

This commit is contained in:
Micah Lee 2018-09-30 17:06:29 -07:00
parent 08c01db69d
commit 8d44c0f729
6 changed files with 55 additions and 35 deletions

View file

@ -33,7 +33,11 @@ def main(cwd=None):
""" """
common = Common() common = Common()
# Load settings right away, in order to get locale setting for strings
common.load_settings(config)
strings.load_strings(common) strings.load_strings(common)
# Display OnionShare banner
print(strings._('version_string').format(common.version)) print(strings._('version_string').format(common.version))
# OnionShare CLI in OSX needs to change current working directory (#132) # OnionShare CLI in OSX needs to change current working directory (#132)
@ -88,8 +92,9 @@ def main(cwd=None):
if not valid: if not valid:
sys.exit() sys.exit()
# Load settings # Re-load settings, if a custom config was passed in
common.load_settings(config) if config:
common.load_settings(config)
# Debug mode? # Debug mode?
common.debug = debug common.debug = debug

View file

@ -22,36 +22,33 @@ import locale
import os import os
strings = {} strings = {}
translations = {}
def load_strings(common, default="en"): def load_strings(common):
""" """
Loads translated strings and fallback to English Loads translated strings and fallback to English
if the translation does not exist. if the translation does not exist.
""" """
global strings global strings, translations
# find locale dir # Load all translations
locale_dir = common.get_resource_path('locale')
# load all translations
translations = {} translations = {}
for filename in os.listdir(locale_dir): for locale in common.settings.available_locales:
abs_filename = os.path.join(locale_dir, filename) locale_dir = common.get_resource_path('locale')
lang, ext = os.path.splitext(filename) filename = os.path.join(locale_dir, "{}.json".format(locale))
if ext == '.json': with open(filename, encoding='utf-8') as f:
with open(abs_filename, encoding='utf-8') as f: translations[locale] = json.load(f)
translations[lang] = json.load(f)
strings = translations[default] # Build strings
lc, enc = locale.getdefaultlocale() default_locale = 'en'
if lc: current_locale = common.settings.get('locale')
lang = lc[:2] strings = {}
if lang in translations: for s in translations[default_locale]:
# if a string doesn't exist, fallback to English if s in translations[current_locale]:
for key in translations[default]: strings[s] = translations[current_locale][s]
if key in translations[lang]: else:
strings[key] = translations[lang][key] strings[s] = translations[default_locale][s]
def translated(k, gui=False): def translated(k, gui=False):

View file

@ -59,7 +59,11 @@ def main():
common = Common() common = Common()
common.define_css() common.define_css()
# Load settings right away, in order to get locale setting for strings
common.load_settings()
strings.load_strings(common) strings.load_strings(common)
# Display OnionShare banner
print(strings._('version_string').format(common.version)) print(strings._('version_string').format(common.version))
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception # Allow Ctrl-C to smoothly quit the program instead of throwing an exception

View file

@ -57,9 +57,10 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.setWindowIcon(QtGui.QIcon(self.common.get_resource_path('images/logo.png'))) self.setWindowIcon(QtGui.QIcon(self.common.get_resource_path('images/logo.png')))
self.setMinimumWidth(850) self.setMinimumWidth(850)
# Load settings # Load settings, if a custom config was passed in
self.config = config self.config = config
self.common.load_settings(self.config) if self.config:
self.common.load_settings(self.config)
# System tray # System tray
menu = QtWidgets.QMenu() menu = QtWidgets.QMenu()

View file

@ -830,8 +830,29 @@ class SettingsDialog(QtWidgets.QDialog):
""" """
self.common.log('SettingsDialog', 'save_clicked') self.common.log('SettingsDialog', 'save_clicked')
def changed(s1, s2, keys):
"""
Compare the Settings objects s1 and s2 and return true if any values
have changed for the given keys.
"""
for key in keys:
if s1.get(key) != s2.get(key):
return True
return False
settings = self.settings_from_fields() settings = self.settings_from_fields()
if settings: if settings:
# If language changed, inform user they need to restart OnionShare
if changed(settings, self.old_settings, ['locale']):
# Look up error message in different locale
new_locale = settings.get('locale')
if new_locale in strings.translations and 'gui_settings_language_changed_notice' in strings.translations[new_locale]:
notice = strings.translations[new_locale]['gui_settings_language_changed_notice']
else:
notice = strings._('gui_settings_language_changed_notice')
Alert(self.common, notice, QtWidgets.QMessageBox.Information)
# Save the new settings
settings.save() settings.save()
# If Tor isn't connected, or if Tor settings have changed, Reinitialize # If Tor isn't connected, or if Tor settings have changed, Reinitialize
@ -840,15 +861,6 @@ class SettingsDialog(QtWidgets.QDialog):
if not self.local_only: if not self.local_only:
if self.onion.is_authenticated(): if self.onion.is_authenticated():
self.common.log('SettingsDialog', 'save_clicked', 'Connected to Tor') self.common.log('SettingsDialog', 'save_clicked', 'Connected to Tor')
def changed(s1, s2, keys):
"""
Compare the Settings objects s1 and s2 and return true if any values
have changed for the given keys.
"""
for key in keys:
if s1.get(key) != s2.get(key):
return True
return False
if changed(settings, self.old_settings, [ if changed(settings, self.old_settings, [
'connection_type', 'control_port_address', 'connection_type', 'control_port_address',

View file

@ -181,5 +181,6 @@
"gui_upload_finished_range": "Uploaded {} to {}", "gui_upload_finished_range": "Uploaded {} to {}",
"gui_upload_finished": "Uploaded {}", "gui_upload_finished": "Uploaded {}",
"gui_open_folder_error_nautilus": "Cannot open folder because nautilus is not available. The file is here: {}", "gui_open_folder_error_nautilus": "Cannot open folder because nautilus is not available. The file is here: {}",
"gui_settings_language_label": "Preferred language" "gui_settings_language_label": "Preferred language",
"gui_settings_language_changed_notice": "Restart OnionShare for your change in language to take effect."
} }