mirror of
https://github.com/onionshare/onionshare.git
synced 2025-01-25 10:42:58 -03:00
Add locale to Settings, and make it default to the system locale, or English
This commit is contained in:
parent
2ffcdbb108
commit
d28f38b1a0
2 changed files with 22 additions and 2 deletions
|
@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
|
import locale
|
||||||
|
|
||||||
from . import strings
|
from . import strings
|
||||||
|
|
||||||
|
@ -47,6 +48,12 @@ class Settings(object):
|
||||||
else:
|
else:
|
||||||
self.common.log('Settings', '__init__', 'Supplied config does not exist or is unreadable. Falling back to default location')
|
self.common.log('Settings', '__init__', 'Supplied config does not exist or is unreadable. Falling back to default location')
|
||||||
|
|
||||||
|
# Available languages in this version of OnionShare
|
||||||
|
self.available_locales = [
|
||||||
|
'cs', 'da', 'de', 'en', 'eo', 'es', 'fi',
|
||||||
|
'fr', 'it', 'nl', 'no', 'pt', 'ru', 'tr'
|
||||||
|
]
|
||||||
|
|
||||||
# These are the default settings. They will get overwritten when loading from disk
|
# These are the default settings. They will get overwritten when loading from disk
|
||||||
self.default_settings = {
|
self.default_settings = {
|
||||||
'version': self.common.version,
|
'version': self.common.version,
|
||||||
|
@ -74,7 +81,8 @@ class Settings(object):
|
||||||
'slug': '',
|
'slug': '',
|
||||||
'hidservauth_string': '',
|
'hidservauth_string': '',
|
||||||
'downloads_dir': self.build_default_downloads_dir(),
|
'downloads_dir': self.build_default_downloads_dir(),
|
||||||
'receive_allow_receiver_shutdown': True
|
'receive_allow_receiver_shutdown': True,
|
||||||
|
'locale': None # this gets defined in fill_in_defaults()
|
||||||
}
|
}
|
||||||
self._settings = {}
|
self._settings = {}
|
||||||
self.fill_in_defaults()
|
self.fill_in_defaults()
|
||||||
|
@ -88,6 +96,13 @@ class Settings(object):
|
||||||
if key not in self._settings:
|
if key not in self._settings:
|
||||||
self._settings[key] = self.default_settings[key]
|
self._settings[key] = self.default_settings[key]
|
||||||
|
|
||||||
|
# Choose the default locale based on the OS preference, and fall-back to English
|
||||||
|
if self._settings['locale'] is None:
|
||||||
|
default_locale = locale.getdefaultlocale()[0][:2]
|
||||||
|
if default_locale not in self.available_locales:
|
||||||
|
default_locale = 'en'
|
||||||
|
self._settings['locale'] = default_locale
|
||||||
|
|
||||||
def build_filename(self):
|
def build_filename(self):
|
||||||
"""
|
"""
|
||||||
Returns the path of the settings file.
|
Returns the path of the settings file.
|
||||||
|
|
|
@ -40,7 +40,7 @@ def settings_obj(sys_onionshare_dev_mode, platform_linux):
|
||||||
|
|
||||||
class TestSettings:
|
class TestSettings:
|
||||||
def test_init(self, settings_obj):
|
def test_init(self, settings_obj):
|
||||||
assert settings_obj._settings == settings_obj.default_settings == {
|
expected_settings = {
|
||||||
'version': 'DUMMY_VERSION_1.2.3',
|
'version': 'DUMMY_VERSION_1.2.3',
|
||||||
'connection_type': 'bundled',
|
'connection_type': 'bundled',
|
||||||
'control_port_address': '127.0.0.1',
|
'control_port_address': '127.0.0.1',
|
||||||
|
@ -68,6 +68,11 @@ class TestSettings:
|
||||||
'receive_allow_receiver_shutdown': True,
|
'receive_allow_receiver_shutdown': True,
|
||||||
'public_mode': False
|
'public_mode': False
|
||||||
}
|
}
|
||||||
|
for key in settings_obj._settings:
|
||||||
|
# Skip locale, it will not always default to the same thing
|
||||||
|
if key != 'locale':
|
||||||
|
assert settings_obj._settings[key] == settings_obj.default_settings[key]
|
||||||
|
assert settings_obj._settings[key] == expected_settings[key]
|
||||||
|
|
||||||
def test_fill_in_defaults(self, settings_obj):
|
def test_fill_in_defaults(self, settings_obj):
|
||||||
del settings_obj._settings['version']
|
del settings_obj._settings['version']
|
||||||
|
|
Loading…
Add table
Reference in a new issue