From 6553c7902925809fd1cbc904dd08db035466a6b2 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Tue, 16 Mar 2021 11:07:04 +0100 Subject: [PATCH] [mod] replace /translations.js by embedded JSON In webapp.py, there is a new function "get_translations" lists available translations Close #2064 --- searx/static/themes/oscar/js/searx.js | 5 +++-- searx/static/themes/oscar/js/searx.min.js | Bin 5615 -> 5695 bytes searx/static/themes/oscar/js/searx.min.js.map | Bin 6562 -> 6642 bytes searx/static/themes/oscar/src/js/01_init.js | 3 ++- .../static/themes/oscar/src/js/leaflet_map.js | 2 +- searx/static/themes/simple/js/searx.head.js | 2 +- .../static/themes/simple/js/searx.head.min.js | Bin 742 -> 752 bytes .../themes/simple/js/searx.head.min.js.map | Bin 785 -> 812 bytes searx/static/themes/simple/js/searx.js | 2 +- searx/static/themes/simple/js/searx.min.js | Bin 24012 -> 24025 bytes .../static/themes/simple/js/searx.min.js.map | Bin 30481 -> 30503 bytes .../themes/simple/js/searx_head/00_init.js | 2 +- .../simple/js/searx_src/searx_search.js | 2 +- .../templates/__common__/translations.js.tpl | 1 - searx/templates/oscar/base.html | 4 ++-- searx/templates/simple/base.html | 3 +-- searx/webapp.py | 19 ++++++++++-------- 17 files changed, 24 insertions(+), 21 deletions(-) delete mode 100644 searx/templates/__common__/translations.js.tpl diff --git a/searx/static/themes/oscar/js/searx.js b/searx/static/themes/oscar/js/searx.js index 159344b28..163009481 100644 --- a/searx/static/themes/oscar/js/searx.js +++ b/searx/static/themes/oscar/js/searx.js @@ -25,7 +25,8 @@ window.searx = (function(d) { return { autocompleter: script.getAttribute('data-autocompleter') === 'true', - method: script.getAttribute('data-method') + method: script.getAttribute('data-method'), + translations: JSON.parse(script.getAttribute('data-translations')) }; })(document); ;/** @@ -302,7 +303,7 @@ $(document).ready(function(){ } }) .fail(function() { - $(result_table_loadicon).html($(result_table_loadicon).html() + "

"+could_not_load+"

"); + $(result_table_loadicon).html($(result_table_loadicon).html() + "

"+searx.translations.could_not_load+"

"); }); } } diff --git a/searx/static/themes/oscar/js/searx.min.js b/searx/static/themes/oscar/js/searx.min.js index 71d67ae7fafde26ebdbae8a25de5be08fee5e0f0..0021f599cadc855738c2ccaad02f7b7d2246a070 100644 GIT binary patch delta 177 zcmaE_yDCnpxC zD&^&;q$*kEOrFT2!k9C88;=H%yw4-Souj6vqL-MJSfP=#nTfZHS-3bgv8X~1*&@Bo IJp#$B05P^cWdHyG delta 80 zcmdn5^Im%cGvj6l#zJn{WZS&boE+=o^30Ot42?X^>g2@YRHeN9lvE|FokHqrW#uJW;3A$uX+LS;yHiIm%ln)G^!1!+CN6zu4sWY!dug zPGI%^I+;$6&Mv{5McI4UISX8M{DIoj-8Qe{+s!6r=mJ&~3N|g>T_@GiF%@WzZom5B1RG?1Z5|7P?L=9K~Z-^Yx diff --git a/searx/static/themes/oscar/src/js/01_init.js b/searx/static/themes/oscar/src/js/01_init.js index 690365c7f..dd8de4ddb 100644 --- a/searx/static/themes/oscar/src/js/01_init.js +++ b/searx/static/themes/oscar/src/js/01_init.js @@ -25,6 +25,7 @@ window.searx = (function(d) { return { autocompleter: script.getAttribute('data-autocompleter') === 'true', - method: script.getAttribute('data-method') + method: script.getAttribute('data-method'), + translations: JSON.parse(script.getAttribute('data-translations')) }; })(document); diff --git a/searx/static/themes/oscar/src/js/leaflet_map.js b/searx/static/themes/oscar/src/js/leaflet_map.js index d1e102ff6..83364b0c3 100644 --- a/searx/static/themes/oscar/src/js/leaflet_map.js +++ b/searx/static/themes/oscar/src/js/leaflet_map.js @@ -90,7 +90,7 @@ $(document).ready(function(){ } }) .fail(function() { - $(result_table_loadicon).html($(result_table_loadicon).html() + "

"+could_not_load+"

"); + $(result_table_loadicon).html($(result_table_loadicon).html() + "

"+searx.translations.could_not_load+"

"); }); } } diff --git a/searx/static/themes/simple/js/searx.head.js b/searx/static/themes/simple/js/searx.head.js index 3ac61c8ae..e6964400b 100644 --- a/searx/static/themes/simple/js/searx.head.js +++ b/searx/static/themes/simple/js/searx.head.js @@ -32,7 +32,7 @@ search_on_category_select: script.getAttribute('data-search-on-category-select') === 'true', infinite_scroll: script.getAttribute('data-infinite-scroll') === 'true', static_path: script.getAttribute('data-static-path'), - no_item_found: script.getAttribute('data-no-item-found') + translations: JSON.parse(script.getAttribute('data-translations')), } // update the css diff --git a/searx/static/themes/simple/js/searx.head.min.js b/searx/static/themes/simple/js/searx.head.min.js index 19de1e498fda540629382fac55d89d0b18431808..5c87d749bf824cbe00181e1837af4b7cac4fbfdc 100644 GIT binary patch delta 61 zcmaFH`hj(W7L!CtQDRvJ36|j>VyIn t6-++Gq^y#x;|~;$02-c_nUktyrBs}nSX7~xk(!vImz$ZVmsPA(3jieZEgAp- delta 97 zcmZ3(Hj!-uFC%YWetc$0YHoa5eraCHW(~$FMzdr`$J8_(S4T(xP@P0a$3&e{Cr8gR w=LnFHtEWz(lcQsylTM(cW1x<^qoaGGPAE`O&g7>|%IuLk{y^c7$^6W+0Llv-@Bjb+ diff --git a/searx/static/themes/simple/js/searx.js b/searx/static/themes/simple/js/searx.js index 8fb2471b0..1abe81e4d 100644 --- a/searx/static/themes/simple/js/searx.js +++ b/searx/static/themes/simple/js/searx.js @@ -1529,7 +1529,7 @@ module.exports = AutoComplete; if (searx.autocompleter) { searx.autocomplete = AutoComplete.call(w, { Url: "./autocompleter", - EmptyMessage: searx.no_item_found, + EmptyMessage: searx.translations.no_item_found, HttpMethod: searx.method, HttpHeaders: { "Content-type": "application/x-www-form-urlencoded", diff --git a/searx/static/themes/simple/js/searx.min.js b/searx/static/themes/simple/js/searx.min.js index 731e9ca7c554daf1fe0eaf697a2afd54ea87abf9..3b2b9d762b59b542f98ffc87c2832adf9da58b44 100644 GIT binary patch delta 27 jcmX@JoAKsu#tqw}cuI;A^NMp4OEUBGiZ^eI5@QAcw8abQ delta 14 Wcmcb)oAJzU#tqw}HXn%+Wd;B_`3A@U diff --git a/searx/static/themes/simple/js/searx.min.js.map b/searx/static/themes/simple/js/searx.min.js.map index fafc9644b663f012ac3393b93c3c25072e6bd5b4..efce15142a535689188defe534e8afe7da4a77c3 100644 GIT binary patch delta 158 zcmbREj&b=r#tjN0JS9bmdBr)2C7Jno#hYbCOcQt#9UT*$b&?&Ovz(15N0b-{Y6t4% zIypM#JLzOQIl7iOnNOZs;xCgPrW5Jt7zretO`S5FbX*-B{WG0_`W%z9CNr1%3uLC~ x7yz{yIq5h%I+wWUPfjX5%9ow26X@s|3=+z7$(d|grpQ-{5Xx~Xo19X%761;iGL`@U delta 138 zcmZ4fj&b5U#tjN0oApJE64;U*oeP`{C+C(J2wDW{A9xKQI diff --git a/searx/static/themes/simple/js/searx_head/00_init.js b/searx/static/themes/simple/js/searx_head/00_init.js index 3ac61c8ae..e6964400b 100644 --- a/searx/static/themes/simple/js/searx_head/00_init.js +++ b/searx/static/themes/simple/js/searx_head/00_init.js @@ -32,7 +32,7 @@ search_on_category_select: script.getAttribute('data-search-on-category-select') === 'true', infinite_scroll: script.getAttribute('data-infinite-scroll') === 'true', static_path: script.getAttribute('data-static-path'), - no_item_found: script.getAttribute('data-no-item-found') + translations: JSON.parse(script.getAttribute('data-translations')), } // update the css diff --git a/searx/static/themes/simple/js/searx_src/searx_search.js b/searx/static/themes/simple/js/searx_src/searx_search.js index 7b652fa92..0e498717c 100644 --- a/searx/static/themes/simple/js/searx_src/searx_search.js +++ b/searx/static/themes/simple/js/searx_src/searx_search.js @@ -73,7 +73,7 @@ if (searx.autocompleter) { searx.autocomplete = AutoComplete.call(w, { Url: "./autocompleter", - EmptyMessage: searx.no_item_found, + EmptyMessage: searx.translations.no_item_found, HttpMethod: searx.method, HttpHeaders: { "Content-type": "application/x-www-form-urlencoded", diff --git a/searx/templates/__common__/translations.js.tpl b/searx/templates/__common__/translations.js.tpl deleted file mode 100644 index 8453aba69..000000000 --- a/searx/templates/__common__/translations.js.tpl +++ /dev/null @@ -1 +0,0 @@ -var could_not_load = '{{ _('could not load data') }}!'; diff --git a/searx/templates/oscar/base.html b/searx/templates/oscar/base.html index f8345d9a7..b5c6ed258 100644 --- a/searx/templates/oscar/base.html +++ b/searx/templates/oscar/base.html @@ -9,7 +9,6 @@ {% block meta %}{% endblock %} - {% block title %}{% endblock %}{{ instance_name }} {% if preferences.get_value('oscar-style') -%} @@ -99,7 +98,8 @@ + data-autocompleter="{% if autocomplete %}true{% else %}false{% endif %}" + data-translations="{{ translations }}"> {% for script in scripts %} {{""}} {% endfor %} diff --git a/searx/templates/simple/base.html b/searx/templates/simple/base.html index a7255e672..17c9785e7 100644 --- a/searx/templates/simple/base.html +++ b/searx/templates/simple/base.html @@ -12,7 +12,6 @@ {% block title %}{% endblock %}{{ instance_name }} {% block meta %}{% endblock %} - {% if rtl %} {% else %} @@ -26,7 +25,7 @@ data-search-on-category-select="{{ 'true' if 'plugins/js/search_on_category_select.js' in scripts else 'false'}}" data-infinite-scroll="{{ 'true' if 'plugins/js/infinite_scroll.js' in scripts else 'false' }}" data-static-path="{{ url_for('static', filename='themes/simple') }}/" - data-no-item-found="{{ _('No item found') }}"> + data-translations="{{ translations }}"> {% block head %} diff --git a/searx/webapp.py b/searx/webapp.py index 062077292..6f0297a99 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -362,6 +362,15 @@ def image_proxify(url): urlencode(dict(url=url.encode(), h=h))) +def get_translations(): + return { + # when overpass AJAX request fails (on a map result) + 'could_not_load': gettext('could not load data'), + # when there is autocompletion + 'no_item_found': gettext('No item found') + } + + def render(template_name, override_theme=None, **kwargs): disabled_engines = request.preferences.engines.get_disabled() @@ -421,6 +430,8 @@ def render(template_name, override_theme=None, **kwargs): kwargs['brand'] = brand + kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':')) + kwargs['scripts'] = set() kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint for plugin in request.user_plugins: @@ -1084,14 +1095,6 @@ def config(): }) -@app.route('/translations.js') -def js_translations(): - return render( - 'translations.js.tpl', - override_theme='__common__', - ), {'Content-Type': 'text/javascript; charset=UTF-8'} - - @app.errorhandler(404) def page_not_found(e): return render('404.html'), 404