From ef6bb5fec09d45729a5e88ac8d9c3ddf780123f9 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Mon, 24 May 2021 13:28:55 -0400 Subject: [PATCH] Replace hardcoded strings using translation json file This introduces a new "translations.json" file under app/static/settings that is loaded on app init and uses the user config value for interface language to determine the appropriate strings to use in Whoogle-specific elements of the UI (primarily only on the home page). --- app/__init__.py | 2 ++ app/models/config.py | 12 +++++++ app/routes.py | 6 ++++ app/static/settings/countries.json | 2 +- app/static/settings/languages.json | 2 +- app/static/settings/translations.json | 31 +++++++++++++++++ app/templates/display.html | 2 +- app/templates/index.html | 50 +++++++++++++-------------- 8 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 app/static/settings/translations.json diff --git a/app/__init__.py b/app/__init__.py index c3d781b..06d9306 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -33,6 +33,8 @@ app.config['LANGUAGES'] = json.load(open( os.path.join(app.config['STATIC_FOLDER'], 'settings/languages.json'))) app.config['COUNTRIES'] = json.load(open( os.path.join(app.config['STATIC_FOLDER'], 'settings/countries.json'))) +app.config['TRANSLATIONS'] = json.load(open( + os.path.join(app.config['STATIC_FOLDER'], 'settings/translations.json'))) app.config['CONFIG_PATH'] = os.getenv( 'CONFIG_VOLUME', os.path.join(app.config['STATIC_FOLDER'], 'config')) diff --git a/app/models/config.py b/app/models/config.py index 5b2f192..a4fac6a 100644 --- a/app/models/config.py +++ b/app/models/config.py @@ -77,6 +77,18 @@ class Config: return key in self.safe_keys + def get_interface_lang(self): + """Returns the correct language to use for localization, but falls + back to english if not set. + + Returns: + str -- the interface language string + """ + if self.lang_interface: + return self.lang_interface + + return 'lang_en' + def from_params(self, params) -> 'Config': """Modify user config with search parameters. This is primarily used for specifying configuration on a search-by-search basis on diff --git a/app/routes.py b/app/routes.py index 2e8152a..0919c6a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -130,6 +130,9 @@ def index(): return render_template('index.html', languages=app.config['LANGUAGES'], countries=app.config['COUNTRIES'], + translation=app.config['TRANSLATIONS'][ + g.user_config.get_interface_lang() + ], logo=render_template( 'logo.html', dark=g.user_config.dark), @@ -235,6 +238,9 @@ def search(): query=urlparse.unquote(query), search_type=search_util.search_type, config=g.user_config, + translation=app.config['TRANSLATIONS'][ + g.user_config.get_interface_lang() + ], response=response, version_number=app.config['VERSION_NUMBER'], search_header=(render_template( diff --git a/app/static/settings/countries.json b/app/static/settings/countries.json index 57c4619..da5ce2f 100644 --- a/app/static/settings/countries.json +++ b/app/static/settings/countries.json @@ -1,5 +1,5 @@ [ - {"name": "Default (none)", "value": ""}, + {"name": "-------", "value": ""}, {"name": "Afghanistan", "value": "countryAF"}, {"name": "Albania", "value": "countryAL"}, {"name": "Algeria", "value": "countryDZ"}, diff --git a/app/static/settings/languages.json b/app/static/settings/languages.json index 4666b7c..0a5e5c8 100644 --- a/app/static/settings/languages.json +++ b/app/static/settings/languages.json @@ -1,5 +1,5 @@ [ - {"name": "Default (none specified)", "value": ""}, + {"name": "-------", "value": ""}, {"name": "English", "value": "lang_en"}, {"name": "Afrikaans", "value": "lang_af"}, {"name": "Arabic", "value": "lang_ar"}, diff --git a/app/static/settings/translations.json b/app/static/settings/translations.json new file mode 100644 index 0000000..02af302 --- /dev/null +++ b/app/static/settings/translations.json @@ -0,0 +1,31 @@ +{ + "lang_en": { + "default-none": "Default (none specified)", + "search": "Search", + "config": "Configuration", + "config-country": "Filter Results by Country", + "config-country-help": "Note: If enabled, a website will only appear in the search results if it is *hosted* in the selected country.", + "config-lang": "Interface Language", + "config-lang-search": "Search Language", + "config-near": "Near", + "config-near-help": "City Name", + "config-block": "Block", + "config-block-help": "Comma-separated site list", + "config-nojs": "Show NoJS Links", + "config-dark": "Dark Mode", + "config-safe": "Safe Search", + "config-alts": "Replace Social Media Links", + "config-alts-help": "Replaces Twitter/YouTube/Instagram/etc links with privacy respecting alternatives.", + "config-new-tab": "Open Links in New Tab", + "config-images": "Full Size Image Search", + "config-images-help": "(Experimental) Adds the 'View Image' option to desktop image searches. This will cause image result thumbnails to be lower resolution.", + "config-tor": "Use Tor", + "config-get-only": "GET Requests Only", + "config-url": "Root URL", + "config-css": "Custom CSS", + "load": "Load", + "apply": "Apply", + "save-as": "Save As...", + "github-link": "View on GitHub" + } +} diff --git a/app/templates/display.html b/app/templates/display.html index 398e276..8c30f6e 100644 --- a/app/templates/display.html +++ b/app/templates/display.html @@ -20,7 +20,7 @@ diff --git a/app/templates/index.html b/app/templates/index.html index d063ba5..5cba56c 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -53,17 +53,17 @@ autocorrect="off" autocomplete="off"> - + {% if not config_disabled %}
- +
- + -
— Note: If enabled, a website will only appear in the results if it is *hosted* in the selected country.
+
— {{ translation['config-country-help'] }}
- + {% for lang in languages %}
- +
- +
- +
- +
- +
- + -
— Replaces Twitter/YouTube/Instagram/Reddit links - with Nitter/Invidious/Bibliogram/Libreddit links.
+
— {{ translation['config-alts-help'] }}
- +
- + -
— (Experimental) Adds the "View Image" option on desktop to view full size images in search results. - This will cause image result thumbnails to be lower resolution.
+
— {{ translation['config-images-help'] }}
- +
- +
- +
- +