Browse Source

[enh] configurable localization

asciimoo 11 years ago
parent
commit
852dfc77c6
7 changed files with 55 additions and 12 deletions
  1. 4
    1
      Makefile
  2. 1
    1
      searx/settings.yml
  3. 2
    0
      searx/static/css/style.css
  4. 1
    1
      searx/templates/categories.html
  5. 13
    3
      searx/templates/preferences.html
  6. 31
    4
      searx/webapp.py
  7. 3
    2
      setup.py

+ 4
- 1
Makefile View File

@@ -43,8 +43,11 @@ production: bin/buildout production.cfg setup.py
43 43
 minimal: bin/buildout minimal.cfg setup.py
44 44
 	bin/buildout -c minimal.cfg $(options)
45 45
 
46
+locales:
47
+	@pybabel compile -d searx/translations
48
+
46 49
 clean:
47 50
 	@rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \
48 51
 		searx.egg-info lib include .coverage coverage
49 52
 
50
-.PHONY: all tests robot flake8 coverage production minimal clean
53
+.PHONY: all tests robot flake8 coverage production minimal locales clean

+ 1
- 1
searx/settings.yml View File

@@ -106,6 +106,6 @@ engines:
106 106
     title_xpath : ./a/div[@class="data"]/p[@class="title"]/text()
107 107
     content_xpath : ./a/img/@src
108 108
 
109
-languages:
109
+locales:
110 110
     en : English
111 111
     hu : Magyar

+ 2
- 0
searx/static/css/style.css View File

@@ -49,6 +49,8 @@ input[type="submit"] { border: 1px solid #666666; color: #444444;  padding: 4px;
49 49
 
50 50
 input[type="checkbox"] { visibility: hidden; }
51 51
 
52
+fieldset { margin: 8px; }
53
+
52 54
 #categories { margin: 0 10px; }
53 55
 
54 56
 .checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; }

+ 1
- 1
searx/templates/categories.html View File

@@ -1,7 +1,7 @@
1 1
 <div id="categories">
2 2
 {% for category in categories %}
3 3
     <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>
5 5
     </div>
6 6
 {% endfor %}
7 7
 </div>

+ 13
- 3
searx/templates/preferences.html View File

@@ -5,15 +5,25 @@
5 5
     <h2>{{ _('Preferences') }}</h2>
6 6
 
7 7
 
8
+    <form method="post" action="/preferences" id="search_form">
8 9
     <fieldset>
9 10
         <legend>{{ _('Default categories') }}</legend>
10
-        <form method="post" action="/preferences" id="search_form">
11 11
         <p>
12 12
         {% include 'categories.html' %}
13 13
         </p>
14
-        <input type="submit" value="{{ _('save') }}" />
15
-        </form>
16 14
     </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>
17 27
     <div class="right"><a href="/">{{ _('back') }}</a></div>
18 28
 </div>
19 29
 {% endblock %}

+ 31
- 4
searx/webapp.py View File

@@ -63,7 +63,20 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
63 63
 
64 64
 @babel.localeselector
65 65
 def 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
67 80
 
68 81
 
69 82
 def get_base_url():
@@ -213,21 +226,35 @@ def preferences():
213 226
 
214 227
     if request.method == 'POST':
215 228
         selected_categories = []
229
+        locale = None
216 230
         for pd_name, pd in request.form.items():
217 231
             if pd_name.startswith('category_'):
218 232
                 category = pd_name[9:]
219 233
                 if not category in categories:
220 234
                     continue
221 235
                 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
+
222 248
         if selected_categories:
223
-            resp = make_response(redirect('/'))
224 249
             # cookie max age: 4 weeks
225 250
             resp.set_cookie(
226 251
                 'categories', ','.join(selected_categories),
227 252
                 max_age=60 * 60 * 24 * 7 * 4
228 253
             )
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())
231 258
 
232 259
 
233 260
 @app.route('/stats', methods=['GET'])

+ 3
- 2
setup.py View File

@@ -15,8 +15,8 @@ long_description = read('README.rst')
15 15
 
16 16
 setup(
17 17
     name='searx',
18
-    version="0.1.1",
19
-    description="",
18
+    version="0.1.2",
19
+    description="A privacy-respecting, hackable metasearch engine",
20 20
     long_description=long_description,
21 21
     classifiers=[
22 22
         "Programming Language :: Python",
@@ -60,6 +60,7 @@ setup(
60 60
             'settings.yml',
61 61
             '../README.rst',
62 62
             'static/*/*',
63
+            'translations/*/*',
63 64
             'templates/*.html',
64 65
             'templates/result_templates/*.html',
65 66
         ],