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

@@ -43,8 +43,11 @@ production: bin/buildout production.cfg setup.py
 minimal: bin/buildout minimal.cfg setup.py
 minimal: bin/buildout minimal.cfg setup.py
 	bin/buildout -c minimal.cfg $(options)
 	bin/buildout -c minimal.cfg $(options)
 
 
+locales:
+	@pybabel compile -d searx/translations
+
 clean:
 clean:
 	@rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \
 	@rm -rf .installed.cfg .mr.developer.cfg bin parts develop-eggs \
 		searx.egg-info lib include .coverage coverage
 		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

+ 1 - 1
searx/settings.yml

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

+ 2 - 0
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; }
 input[type="checkbox"] { visibility: hidden; }
 
 
+fieldset { margin: 8px; }
+
 #categories { margin: 0 10px; }
 #categories { margin: 0 10px; }
 
 
 .checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; }
 .checkbox_container { display: inline-block; position: relative; margin: 0 3px; padding: 0px; }

+ 1 - 1
searx/templates/categories.html

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

+ 13 - 3
searx/templates/preferences.html

@@ -5,15 +5,25 @@
     <h2>{{ _('Preferences') }}</h2>
     <h2>{{ _('Preferences') }}</h2>
 
 
 
 
+    <form method="post" action="/preferences" id="search_form">
     <fieldset>
     <fieldset>
         <legend>{{ _('Default categories') }}</legend>
         <legend>{{ _('Default categories') }}</legend>
-        <form method="post" action="/preferences" id="search_form">
         <p>
         <p>
         {% include 'categories.html' %}
         {% include 'categories.html' %}
         </p>
         </p>
-        <input type="submit" value="{{ _('save') }}" />
-        </form>
     </fieldset>
     </fieldset>
+    <fieldset>
+        <legend>{{ _('Interface language') }}</legend>
+        <p>
+        <select name='locale'>
+            {% for locale_id,locale_name in locales.items() %}
+            <option value={{ locale_id }} {% if locale_id == current_locale %}selected="selected"{% endif %}>{{ locale_name}}</option>
+            {% endfor %}
+        </select>
+        </p>
+    </fieldset>
+    <input type="submit" value="{{ _('save') }}" />
+    </form>
     <div class="right"><a href="/">{{ _('back') }}</a></div>
     <div class="right"><a href="/">{{ _('back') }}</a></div>
 </div>
 </div>
 {% endblock %}
 {% endblock %}

+ 31 - 4
searx/webapp.py

@@ -63,7 +63,20 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
 
 
 @babel.localeselector
 @babel.localeselector
 def get_locale():
 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():
 def get_base_url():
@@ -213,21 +226,35 @@ def preferences():
 
 
     if request.method == 'POST':
     if request.method == 'POST':
         selected_categories = []
         selected_categories = []
+        locale = None
         for pd_name, pd in request.form.items():
         for pd_name, pd in request.form.items():
             if pd_name.startswith('category_'):
             if pd_name.startswith('category_'):
                 category = pd_name[9:]
                 category = pd_name[9:]
                 if not category in categories:
                 if not category in categories:
                     continue
                     continue
                 selected_categories.append(category)
                 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:
         if selected_categories:
-            resp = make_response(redirect('/'))
             # cookie max age: 4 weeks
             # cookie max age: 4 weeks
             resp.set_cookie(
             resp.set_cookie(
                 'categories', ','.join(selected_categories),
                 'categories', ','.join(selected_categories),
                 max_age=60 * 60 * 24 * 7 * 4
                 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'])
 @app.route('/stats', methods=['GET'])

+ 3 - 2
setup.py

@@ -15,8 +15,8 @@ long_description = read('README.rst')
 
 
 setup(
 setup(
     name='searx',
     name='searx',
-    version="0.1.1",
-    description="",
+    version="0.1.2",
+    description="A privacy-respecting, hackable metasearch engine",
     long_description=long_description,
     long_description=long_description,
     classifiers=[
     classifiers=[
         "Programming Language :: Python",
         "Programming Language :: Python",
@@ -60,6 +60,7 @@ setup(
             'settings.yml',
             'settings.yml',
             '../README.rst',
             '../README.rst',
             'static/*/*',
             'static/*/*',
+            'translations/*/*',
             'templates/*.html',
             'templates/*.html',
             'templates/result_templates/*.html',
             'templates/result_templates/*.html',
         ],
         ],