浏览代码

[enh] configurable localization

asciimoo 11 年前
父节点
当前提交
852dfc77c6
共有 7 个文件被更改,包括 55 次插入12 次删除
  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 查看文件

43
 minimal: bin/buildout minimal.cfg setup.py
43
 minimal: bin/buildout minimal.cfg setup.py
44
 	bin/buildout -c minimal.cfg $(options)
44
 	bin/buildout -c minimal.cfg $(options)
45
 
45
 
46
+locales:
47
+	@pybabel compile -d searx/translations
48
+
46
 clean:
49
 clean:
47
 	@rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \
50
 	@rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \
48
 		searx.egg-info lib include .coverage coverage
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 查看文件

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

+ 2
- 0
searx/static/css/style.css 查看文件

49
 
49
 
50
 input[type="checkbox"] { visibility: hidden; }
50
 input[type="checkbox"] { visibility: hidden; }
51
 
51
 
52
+fieldset { margin: 8px; }
53
+
52
 #categories { margin: 0 10px; }
54
 #categories { margin: 0 10px; }
53
 
55
 
54
 .checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; }
56
 .checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; }

+ 1
- 1
searx/templates/categories.html 查看文件

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

+ 13
- 3
searx/templates/preferences.html 查看文件

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

+ 31
- 4
searx/webapp.py 查看文件

63
 
63
 
64
 @babel.localeselector
64
 @babel.localeselector
65
 def get_locale():
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
 def get_base_url():
82
 def get_base_url():
213
 
226
 
214
     if request.method == 'POST':
227
     if request.method == 'POST':
215
         selected_categories = []
228
         selected_categories = []
229
+        locale = None
216
         for pd_name, pd in request.form.items():
230
         for pd_name, pd in request.form.items():
217
             if pd_name.startswith('category_'):
231
             if pd_name.startswith('category_'):
218
                 category = pd_name[9:]
232
                 category = pd_name[9:]
219
                 if not category in categories:
233
                 if not category in categories:
220
                     continue
234
                     continue
221
                 selected_categories.append(category)
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
         if selected_categories:
248
         if selected_categories:
223
-            resp = make_response(redirect('/'))
224
             # cookie max age: 4 weeks
249
             # cookie max age: 4 weeks
225
             resp.set_cookie(
250
             resp.set_cookie(
226
                 'categories', ','.join(selected_categories),
251
                 'categories', ','.join(selected_categories),
227
                 max_age=60 * 60 * 24 * 7 * 4
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
 @app.route('/stats', methods=['GET'])
260
 @app.route('/stats', methods=['GET'])

+ 3
- 2
setup.py 查看文件

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