Ejemplo n.º 1
0
def get_locale():
	if (g.user is not None) and (not g.user.is_anonymous()):
		if g.user.lang_id is not None:
			app.logger.info(g.user.lang.short_name)
			return g.user.lang.short_name
		else:
			accept_lang = request.accept_languages.best_match(LANGUAGES.keys())
			if accept_lang is None or (accept_lang == ''):
				accept_lang = 'en'			
			g.user.lang = Language.query.filter_by(short_name = accept_lang).first()
			db.session.add(g.user)
			db.session.commit()
			return g.user.lang.short_name
	else:
		return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 2
0
def get_locale():
    print "hi"
    # for testing, hard code the language to show
    # return 'fr'
    print request.accept_languages
    # in production use the below to change language based on http 'accept-languages' header:
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 3
0
def get_locale():
    print "hi"
    # for testing, hard code the language to show
    # return 'fr'
    print request.accept_languages
    # in production use the below to change language based on http 'accept-languages' header:
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 4
0
def get_locale():
    if request.args.get('lang'):
        g.lang = request.args.get('lang')
    if g.get('lang'):
        return g.lang
    g.lang = request.accept_languages.best_match(LANGUAGES.keys())
    return g.lang
Ejemplo n.º 5
0
def get_locale():
    try:
        print('setted lang: '+session['lang'])
        return session['lang']
    except:
        print('default lang')
        return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 6
0
 def ensure_lang_support():
     lang_code = g.get('lang_code', None)
     if lang_code and lang_code not in LANGUAGES.keys():
         app.logger.info(
             'ensure_lang_support failed to find %s in LANGUAGES' %
             (lang_code))
         return abort(404)
Ejemplo n.º 7
0
def get_locale():
    try:
        language = getArgAsList(request, 'lang')[0]
    except:
        language = 'hi'
    print request.accept_languages.best_match(LANGUAGES.keys())
    return language
Ejemplo n.º 8
0
def get_locale():
    """Get current language."""
    try:
        language = session['language']
    except KeyError:
        language = None
    return language if language else request.accept_languages.best_match(
        LANGUAGES.keys())
Ejemplo n.º 9
0
def get_locale():
    # if language forced
    if 'LANGUAGE'in app.config:
        if app.config['LANGUAGE'] in LANGUAGES.keys():
	    return app.config['LANGUAGE']
	else:
            print 'Language not found in Config - LANGUAGES'
    # else if a user is logged in, use the locale from the user settings
    user = getattr(g, 'user', None)
    
    if user is not None:
	print user.locale
        return user.locale
    # otherwise try to guess the language from the user accept
    # header the browser transmits.  We support de/fr/en in this
    # example.  The best match wins.
    return request.accept_languages.best_match(LANGUAGES.keys()) # 'it'
Ejemplo n.º 10
0
def set_lang(lang):
    if lang not in LANGUAGES.keys():
        return redirect('page.index')

    rv = make_response(redirect(redirect_url()))
    rv.set_cookie('lang', lang)
    refresh()
    return rv
Ejemplo n.º 11
0
 def index_view(self):
     placeholder_images = DataGetter.get_event_default_images()
     pages = DataGetter.get_all_pages()
     custom_placeholder = DataGetter.get_custom_placeholders()
     subtopics = DataGetter.get_event_subtopics()
     settings = get_settings()
     LANGUAGES.pop("en")
     if request.method == 'POST':
         dic = dict(request.form.copy())
         for key, value in dic.items():
             settings[key] = value[0]
             set_settings(**settings)
     return self.render(
         '/gentelella/admin/super_admin/content/content.html', pages=pages, settings=settings,
         placeholder_images=placeholder_images, subtopics=subtopics, custom_placeholder=custom_placeholder,
         languages=LANGUAGES
     )
Ejemplo n.º 12
0
def get_locale():
    # if a user is logged in, use the locale from the user settings
    # user = getattr(g, 'user', None)
    # if user is not None:
    #     return user.locale
    # otherwise try to guess the language from the user accept
    # header the browser transmits.  We support de/fr/en in this
    # example.  The best match wins.
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 13
0
def guess_locale():
    req_locale = request.accept_languages.best_match(LANGUAGES.keys())
    expl_locale = g.get('lang_code')
    def_locale = g.get('lang_code', current_app.config['BABEL_DEFAULT_LOCALE'])
    if def_locale not in LANGUAGES.keys():
        def_locale = current_app.config[
            'BABEL_DEFAULT_LOCALE']  # fix recursive 404 error
    current_app.logger.debug("req_locale %s - expl_locale %s - def_locale %s" %
                             (req_locale, expl_locale, def_locale))
    # if they differ prefer the explicit
    if expl_locale and expl_locale in LANGUAGES.keys(
    ) and req_locale != expl_locale:
        locale = expl_locale
    elif req_locale in LANGUAGES.keys():
        locale = req_locale
    else:
        locale = def_locale
    current_app.logger.debug("using locale %s" % (locale))
    return locale
Ejemplo n.º 14
0
class UserForm(Form):
    nickname = StringField(lazy_gettext('Nickname'), [validators.data_required(),
                                                      validators.length(min=5, max=30)])
    email = EmailField(lazy_gettext('Email'), [validators.data_required(),
                                 validators.length(max=120)])
    delivery_mail_receive = BooleanField(lazy_gettext('Email notification of new goods arrival'), default=True)
    inv_lang = dict((v, k) for k, v in LANGUAGES.items())
    lang = [(v, k) for k, v in inv_lang.iteritems()]
    language = SelectField(lazy_gettext('Preferred language'), choices=lang)
    products_per_page = IntegerField(lazy_gettext('Number of products displayed per page'), [validators.NumberRange(min=3)],
                                     default=PRODUCTS_PER_PAGE)
Ejemplo n.º 15
0
class EditUserForm(Form):
    nickname = StringField(lazy_gettext('User nickname'), [validators.data_required(),
                                        validators.length(max=64)])
    email = EmailField(lazy_gettext('Email'), [validators.data_required(),
                                 validators.length(max=120)])

    role = [(str(v), k) for k, v in iter(sorted(USER_ROLES.iteritems(), key=operator.itemgetter(1)))]
    role = SelectField(lazy_gettext('User role'), choices=role)
    customer = SelectField('Customer', coerce=int)
    maker = SelectField('Maker', coerce=int)

    inv_lang = dict((v, k) for k, v in LANGUAGES.items())
    lang = [(v, k) for k, v in inv_lang.iteritems()]
    language = SelectField(lazy_gettext('User language'), choices=lang)
Ejemplo n.º 16
0
def before():
    g.languages = LANGUAGES
    g.locale = request.cookies.get('locale')
    if g.locale is None:
        g.locale = request.accept_languages.best_match(LANGUAGES.keys())

        @after_this_request
        def remember_language(response):
            response.set_cookie('locale', g.locale)
            return response

    g.search_form = SearchForm()
    if current_user and current_user.is_authenticated:
        current_user.see_you()
        g.roles = [role.role_name for role in current_user.roles]
Ejemplo n.º 17
0
def get_locale():
    supported_languages = LANGUAGES.keys()
    language_arg = request.args.get('l')
    if language_arg is not None:
        if language_arg in supported_languages:
            @after_this_request
            def remember_language(response):
                response.set_cookie('language', language_arg)

            return language_arg
    else:
        language_cookie = request.cookies.get('language')
        if language_cookie in supported_languages:
            return language_cookie

    return request.accept_languages.best_match(supported_languages)
Ejemplo n.º 18
0
def get_language():
    """
    Determinate language, by request, cookies and POST data.
    :return: language code.
    """
    result = "en"
    lang_cookies = request.cookies.get('lang')
    if lang_cookies is None:
        result = request.accept_languages.best_match(LANGUAGES.keys())
    else:
        post = request.form
        if len(post) is 0:
            result = lang_cookies
        else:
            lang_post = post.get('lang', default=None)
            if lang_post is not None:
                result = lang_post
    return result
Ejemplo n.º 19
0
def get_locale():
    """
    the function targeted by this selector above will be called before each request, to give us a chance to choose the
    language when producing its response.
    For now we will do something simple, we will just read the Accept-Lanugages header sent by the browser in the HTTP
    request and find the best-matching language from the list we support. This is simple, 'best_match' method does it
    for us.

    Accept-Languages header in most browsers is config'd by default with language selected at OS level, but all give
    users chance to select others. Users can provide a list of languages, each with a weight. Looks like:
    Accept-Language: da, en-gb;q=0.8, en;q=0.7
    Danish default weight=1 is preferred, followed by GB-English, finally with generic english.

    To translate text, use gettext(string to translate) within Python code, or {{ _('string')}} inside HTML
    :return:
    """
    #return 'es'
    return request.accept_languages.best_match(LANGUAGES.keys())  # temp set to spanish for testing
Ejemplo n.º 20
0
class AddUserForm(Form):
    nickname = StringField(lazy_gettext('User nickname'), [validators.data_required(),
                                        validators.length(max=64)])
    password = PasswordField(lazy_gettext('User password'), [validators.data_required(),
                                          validators.length(min=5, max=30),
                                          validators.EqualTo('confirm', message=lazy_gettext('Passwords must match'))])
    confirm = PasswordField(lazy_gettext('Password confirmation'))
    email = EmailField(lazy_gettext('Email'), [validators.data_required(),
                                 validators.length(max=120)])

    role = [(str(v), k) for k, v in iter(sorted(USER_ROLES.iteritems(), key=operator.itemgetter(1)))]
    role = SelectField(lazy_gettext('User role'), choices=role)
    customer = SelectField('Customer', coerce=int)
    maker = SelectField('Maker', coerce=int)

    inv_lang = dict((v, k) for k, v in LANGUAGES.items())
    lang = [(v, k) for k, v in inv_lang.iteritems()]
    language = SelectField(lazy_gettext('User language'), choices=lang)
Ejemplo n.º 21
0
def settings_general():
    """ Display general settings """
    if not logged_in():
        return redirect(url_for('general_routes.home'))

    misc = db_retrieve_table(
        current_app.config['MYCODO_DB_PATH'], Misc, entry='first')
    form_settings_general = flaskforms.SettingsGeneral()

    languages_sorted = sorted(LANGUAGES.items(), key=operator.itemgetter(1))

    if request.method == 'POST':
        form_name = request.form['form-name']
        if form_name == 'General':
            flaskutils.settings_general_mod(form_settings_general)
        return redirect('/settings/general')

    return render_template('settings/general.html',
                           misc=misc,
                           languages=languages_sorted,
                           form_settings_general=form_settings_general)
Ejemplo n.º 22
0
def get_locale():
    headers = request.headers.get('User-Agent')
    app.logger.info('Header info: ' + headers)
    return request.accept_languages.best_match(LANGUAGES.keys()) or 'en'
Ejemplo n.º 23
0
def get_locale():
    return request.accept_languages.best_match(LANGUAGES.keys())
    
        if query.duration >= DATABASE_QUERY_TIMEOUT:
            app.logger.warning("SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" % (query.statement, query.parameters, query.duration, query.context))
Ejemplo n.º 24
0
def get_locale():
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 25
0
def get_locale():
    """Return the best matched language supported."""
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 26
0
def get_locale():
    """Return the best matched language supported."""
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 27
0
def get_locale():
    # BUG: al invocarse justo before_request siempre se selecciona el preferido, no el seleccionado
    l = getattr(g, 'locale', None)
    if l is not None:
        return g.locale
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 28
0
def before():
    if request.view_args and 'lang_code' in request.view_args:
        if request.view_args['lang_code'] not in LANGUAGES.keys():
            return abort(404)
        g.current_lang = request.view_args['lang_code']
        request.view_args.pop('lang_code')
Ejemplo n.º 29
0
def get_locale():
    """Get the locale to use for lang."""
    locale = request.accept_languages.best_match(LANGUAGES.keys())
    # return locale
    # Temporary
    return 'fr'
Ejemplo n.º 30
0
def get_locale():
    try:
        return request.cookies["selected_lang"]
    except:
        return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 31
0
def get_locale():
    key = _validate('locale', request.args.get('locale'))
    return LANGUAGES.has_key(key) and LANGUAGES.get(key)[0] or DEFAUL_LANGUAGE
Ejemplo n.º 32
0
def get_locale():
    """Retrieve locale based on available languages"""
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 33
0
def get_locale():
    # if a user is logged in, use the locale from the user settings
    if current_user and current_user.locale is not None and \
            not current_user.is_anonymous():
        return current_user.locale
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 34
0
def get_locale():
    # 返回最匹配浏览器发送的request请求中指定的语言
    # print(request.accept_languages.best_match(LANGUAGES.keys()))
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 35
0
def get_locale():
    if current_user and current_user.is_authenticated():
        return current_user.lang
    return request.accept_languages.best_match(LANGUAGES.keys())
def get_locale():
    # We should get this from the config, but it fails, the same happens with timezone
    #return "es"
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 37
0
 def locale(self):
   if self.request.get('lang'):
     return self.request.get('lang')
   else:
     return LANGUAGES.get(self.request.headers.get('X-AppEngine-Country')) or 'en'
def get_locale():
    try:
        return request.cookies["selected_lang"]
    except:
        return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 39
0
def get_locale():
    best_math_lang = request.accept_languages.best_match(LANGUAGES.keys())
    # app.logger.debug('best_math_lang: %s', best_math_lang)
    return best_math_lang
Ejemplo n.º 40
0
def get_locale():
    lang = request.cookies.get('lang')
    if not lang:
        lang = request.accept_languages.best_match(list(LANGUAGES.keys()))
    return lang
Ejemplo n.º 41
0
def get_locale():
    # if a user is logged in, use the locale from the user settings
    user = getattr(g, 'user', None)
    if user is not None:
        return user.locale
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 42
0
def get_locale():
    lang = request.accept_languages.best_match(LANGUAGES.keys())
    if g.user.is_authenticated():
        lang = g.user.language
    return lang
Ejemplo n.º 43
0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

if sys.platform == 'win32':
    pybabel = r"C:\Python27\Scripts\pybabel.exe"
else:
    pybabel = "pybabel"

from config import LANGUAGES
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -k T -o messages.pot ..\\')

for lang in LANGUAGES.keys():
    os.system('%s init -i messages.pot -d . -l "%s"' % (pybabel, lang.replace("-","_")))

os.system("pause")
os.unlink('messages.pot')

Ejemplo n.º 44
0
def get_locale():
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 45
0
def get_locale():
  if 'locale' in session:
    return session['locale']
  return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 46
0
Archivo: lotg.py Proyecto: tojaku/lotg
def get_locale():
    if g.user is not None:
        return g.user.language
    return request.accept_languages.best_match(LANGUAGES.keys())
Ejemplo n.º 47
0
def set_language_code(language=None):
    session[LANGUAGE_KEY] = language if language in LANGUAGES.keys(
    ) else BABEL_DEFAULT_LOCALE
    return redirect('/')