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 23c55bc95b
commit c4f776c42a
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
6 changed files with 55 additions and 35 deletions

View File

@ -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

View File

@ -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)
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)
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]
# 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):

View File

@ -59,7 +59,11 @@ def main():
common = Common()
common.define_css()
# Load settings right away, in order to get locale setting for strings
common.load_settings()
strings.load_strings(common)
# Display OnionShare banner
print(strings._('version_string').format(common.version))
# 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.setMinimumWidth(850)
# Load settings
# Load settings, if a custom config was passed in
self.config = config
self.common.load_settings(self.config)
if self.config:
self.common.load_settings(self.config)
# System tray
menu = QtWidgets.QMenu()

View File

@ -830,8 +830,29 @@ class SettingsDialog(QtWidgets.QDialog):
"""
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()
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()
# 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 self.onion.is_authenticated():
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, [
'connection_type', 'control_port_address',

View File

@ -181,5 +181,6 @@
"gui_upload_finished_range": "Uploaded {} to {}",
"gui_upload_finished": "Uploaded {}",
"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."
}