From 852dfc77c652fcd5557b097d37a3b5b5543391f9 Mon Sep 17 00:00:00 2001 From: asciimoo Date: Wed, 22 Jan 2014 00:59:18 +0100 Subject: [PATCH] [enh] configurable localization --- Makefile | 5 ++++- searx/settings.yml | 2 +- searx/static/css/style.css | 2 ++ searx/templates/categories.html | 2 +- searx/templates/preferences.html | 16 ++++++++++++--- searx/webapp.py | 35 ++++++++++++++++++++++++++++---- setup.py | 5 +++-- 7 files changed, 55 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 9bf8f7052..cc5967581 100644 --- a/Makefile +++ b/Makefile @@ -43,8 +43,11 @@ production: bin/buildout production.cfg setup.py minimal: bin/buildout minimal.cfg setup.py bin/buildout -c minimal.cfg $(options) +locales: + @pybabel compile -d searx/translations + clean: @rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \ searx.egg-info lib include .coverage coverage -.PHONY: all tests robot flake8 coverage production minimal clean +.PHONY: all tests robot flake8 coverage production minimal locales clean diff --git a/searx/settings.yml b/searx/settings.yml index 355b07cf5..c207f3f57 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -106,6 +106,6 @@ engines: title_xpath : ./a/div[@class="data"]/p[@class="title"]/text() content_xpath : ./a/img/@src -languages: +locales: en : English hu : Magyar diff --git a/searx/static/css/style.css b/searx/static/css/style.css index 3d3f46036..4163e753d 100644 --- a/searx/static/css/style.css +++ b/searx/static/css/style.css @@ -49,6 +49,8 @@ input[type="submit"] { border: 1px solid #666666; color: #444444; padding: 4px; input[type="checkbox"] { visibility: hidden; } +fieldset { margin: 8px; } + #categories { margin: 0 10px; } .checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; } diff --git a/searx/templates/categories.html b/searx/templates/categories.html index b1fd3d1fc..57e63c85d 100644 --- a/searx/templates/categories.html +++ b/searx/templates/categories.html @@ -1,7 +1,7 @@
{% for category in categories %}
- +
{% endfor %}
diff --git a/searx/templates/preferences.html b/searx/templates/preferences.html index d47dd4834..3c2afef21 100644 --- a/searx/templates/preferences.html +++ b/searx/templates/preferences.html @@ -5,15 +5,25 @@

{{ _('Preferences') }}

+
{{ _('Default categories') }} -

{% include 'categories.html' %}

- -
+
+ {{ _('Interface language') }} +

+ +

+
+ +
{{ _('back') }}
{% endblock %} diff --git a/searx/webapp.py b/searx/webapp.py index e042443b4..cf1e71ef2 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -63,7 +63,20 @@ opensearch_xml = ''' @babel.localeselector def get_locale(): - return request.accept_languages.best_match(settings['languages'].keys()) + locale = request.accept_languages.best_match(settings['locales'].keys()) + + if request.cookies.get('locale', '') in settings['locales']: + locale = request.cookies.get('locale', '') + + if 'locale' in request.args\ + and request.args['locale'] in settings['locales']: + locale = request.args['locale'] + + if 'locale' in request.form\ + and request.form['locale'] in settings['locales']: + locale = request.form['locale'] + + return locale def get_base_url(): @@ -213,21 +226,35 @@ def preferences(): if request.method == 'POST': selected_categories = [] + locale = None for pd_name, pd in request.form.items(): if pd_name.startswith('category_'): category = pd_name[9:] if not category in categories: continue selected_categories.append(category) + elif pd_name == 'locale' and pd in settings['locales']: + locale = pd + + resp = make_response(redirect('/')) + + if locale: + # cookie max age: 4 weeks + resp.set_cookie( + 'locale', locale, + max_age=60 * 60 * 24 * 7 * 4 + ) + if selected_categories: - resp = make_response(redirect('/')) # cookie max age: 4 weeks resp.set_cookie( 'categories', ','.join(selected_categories), max_age=60 * 60 * 24 * 7 * 4 ) - return resp - return render('preferences.html') + return resp + return render('preferences.html' + ,locales=settings['locales'] + ,current_locale=get_locale()) @app.route('/stats', methods=['GET']) diff --git a/setup.py b/setup.py index 64c317a9c..ecb7f056d 100644 --- a/setup.py +++ b/setup.py @@ -15,8 +15,8 @@ long_description = read('README.rst') setup( name='searx', - version="0.1.1", - description="", + version="0.1.2", + description="A privacy-respecting, hackable metasearch engine", long_description=long_description, classifiers=[ "Programming Language :: Python", @@ -60,6 +60,7 @@ setup( 'settings.yml', '../README.rst', 'static/*/*', + 'translations/*/*', 'templates/*.html', 'templates/result_templates/*.html', ],