on master help sign in
[enh] configurable localization
commit852dfc77c652fcd5557b097d37a3b5b5543391f9 authorasciimoo <asciimoo@gmail.com> dateWed 22 Jan 2014, 00:59 (13 years ago) parenta6c31ef
7 files changed+55−12
Makefile+4−1 searx/settings.yml+1−1 searx/static/css/style.css+2 searx/templates/categories.html+1−1 searx/templates/preferences.html+13−3 searx/webapp.py+31−4 setup.py+3−2
Makefile+4−1
@@ -43,8 +43,11 @@ production: bin/buildout production.cfg setup.py
4343minimal: bin/buildout minimal.cfg setup.py
4444 bin/buildout -c minimal.cfg $(options)
4545
46+locales:
47+ @pybabel compile -d searx/translations
48+
4649clean:
4750 @rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \
4851 searx.egg-info lib include .coverage coverage
4952
50−.PHONY: all tests robot flake8 coverage production minimal clean
53+.PHONY: all tests robot flake8 coverage production minimal locales clean
searx/settings.yml+1−1
@@ -106,6 +106,6 @@ engines:
106106 title_xpath : ./a/div[@class="data"]/p[@class="title"]/text()
107107 content_xpath : ./a/img/@src
108108
109−languages:
109+locales:
110110 en : English
111111 hu : Magyar
searx/static/css/style.css+2
@@ -49,6 +49,8 @@ input[type="submit"] { border: 1px solid #666666; color: #444444; padding: 4px;
4949
5050input[type="checkbox"] { visibility: hidden; }
5151
52+fieldset { margin: 8px; }
53+
5254#categories { margin: 0 10px; }
5355
5456.checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; }
searx/templates/categories.html+1−1
@@ -1,7 +1,7 @@
11<div id="categories">
22{% for category in categories %}
33 <div class="checkbox_container">
4− <input type="checkbox" id="checkbox_{{ category|replace(' ', '_') }}" name="category_{{ category }}" {% if category in selected_categories %}checked="checked"{% endif %} /><label for="checkbox_{{ category|replace(' ', '_') }}">{{ category }}</label>
4+ <input type="checkbox" id="checkbox_{{ category|replace(' ', '_') }}" name="category_{{ category }}" {% if category in selected_categories %}checked="checked"{% endif %} /><label for="checkbox_{{ category|replace(' ', '_') }}">{{ _(category) }}</label>
55 </div>
66{% endfor %}
77</div>
searx/templates/preferences.html+13−3
@@ -5,15 +5,25 @@
55 <h2>{{ _('Preferences') }}</h2>
66
77
8+ <form method="post" action="/preferences" id="search_form">
89 <fieldset>
910 <legend>{{ _('Default categories') }}</legend>
10− <form method="post" action="/preferences" id="search_form">
1111 <p>
1212 {% include 'categories.html' %}
1313 </p>
14− <input type="submit" value="{{ _('save') }}" />
15− </form>
1614 </fieldset>
15+ <fieldset>
16+ <legend>{{ _('Interface language') }}</legend>
17+ <p>
18+ <select name='locale'>
19+ {% for locale_id,locale_name in locales.items() %}
20+ <option value={{ locale_id }} {% if locale_id == current_locale %}selected="selected"{% endif %}>{{ locale_name}}</option>
21+ {% endfor %}
22+ </select>
23+ </p>
24+ </fieldset>
25+ <input type="submit" value="{{ _('save') }}" />
26+ </form>
1727 <div class="right"><a href="/">{{ _('back') }}</a></div>
1828</div>
1929{% endblock %}
searx/webapp.py+31−4
@@ -63,7 +63,20 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
6363
6464@babel.localeselector
6565def get_locale():
66− return request.accept_languages.best_match(settings['languages'].keys())
66+ locale = request.accept_languages.best_match(settings['locales'].keys())
67+
68+ if request.cookies.get('locale', '') in settings['locales']:
69+ locale = request.cookies.get('locale', '')
70+
71+ if 'locale' in request.args\
72+ and request.args['locale'] in settings['locales']:
73+ locale = request.args['locale']
74+
75+ if 'locale' in request.form\
76+ and request.form['locale'] in settings['locales']:
77+ locale = request.form['locale']
78+
79+ return locale
6780
6881
6982def get_base_url():
@@ -213,21 +226,35 @@ def preferences():
213226
214227 if request.method == 'POST':
215228 selected_categories = []
229+ locale = None
216230 for pd_name, pd in request.form.items():
217231 if pd_name.startswith('category_'):
218232 category = pd_name[9:]
219233 if not category in categories:
220234 continue
221235 selected_categories.append(category)
236+ elif pd_name == 'locale' and pd in settings['locales']:
237+ locale = pd
238+
239+ resp = make_response(redirect('/'))
240+
241+ if locale:
242+ # cookie max age: 4 weeks
243+ resp.set_cookie(
244+ 'locale', locale,
245+ max_age=60 * 60 * 24 * 7 * 4
246+ )
247+
222248 if selected_categories:
223− resp = make_response(redirect('/'))
224249 # cookie max age: 4 weeks
225250 resp.set_cookie(
226251 'categories', ','.join(selected_categories),
227252 max_age=60 * 60 * 24 * 7 * 4
228253 )
229− return resp
230− return render('preferences.html')
254+ return resp
255+ return render('preferences.html'
256+ ,locales=settings['locales']
257+ ,current_locale=get_locale())
231258
232259
233260@app.route('/stats', methods=['GET'])
setup.py+3−2
@@ -15,8 +15,8 @@ long_description = read('README.rst')
1515
1616setup(
1717 name='searx',
18− version="0.1.1",
19− description="",
18+ version="0.1.2",
19+ description="A privacy-respecting, hackable metasearch engine",
2020 long_description=long_description,
2121 classifiers=[
2222 "Programming Language :: Python",
@@ -60,6 +60,7 @@ setup(
6060 'settings.yml',
6161 '../README.rst',
6262 'static/*/*',
63+ 'translations/*/*',
6364 'templates/*.html',
6465 'templates/result_templates/*.html',
6566 ],