1

I want to make my site in two different languages, am using Flask-Babel for this purpose .

Here is my settings.py configurations:

LANGUAGES = {
    'en': 'English',
    'ru': 'Russian'
}

Here you can see a dictionary including the two languages that am using in my app.

here is the views.py:

from settings import LANGUAGES

@app.before_request
@babel.localeselector
def before_request():
    if request.view_args and 'lang' in request.view_args:
        g.current_lang = request.view_args['lang']
        request.view_args.pop('lang')

@app.context_processor
def inject_url_for():
    return {
        'url_for': lambda endpoint, **kwargs: flask_url_for(
        endpoint, lang=g.current_lang, **kwargs
        )
    }
url_for = inject_url_for()['url_for']

The inject_url_for function means that all the url_for() calls in the application need to be modified to have an extra parameter called lang to be passed, and that's what i did exactly , here is the index function:

@app.route('/<lang>/')
def index(page=1):

    user = User.query.filter_by(username='Reznov').first_or_404()
    blogs = Blog.query.count()
    blog_data = Blog.query.first()
    blog = Blog.query.first()
    posts = Post.query.filter_by(live=True).order_by(Post.publish_date.desc()).paginate(page, app.config['POSTS_PER_PAGE'], False)
    scshot = ScreenShot.query.filter_by(live=True).order_by(ScreenShot.publish_date.desc()).limit(4)
    post = Post.query.first()

    context = {
        'user': user,
        'blogs': blogs,
        'blog_data':blog_data,
        'blog':blog,
        'posts':posts,
        'scshot':scshot,
        'post':post
    }

    return render_template('blog/index.html', **context)

Sounds good right ?

Now if i ran the application as it is, nothing changes, it just gives me the default language which is English, but won't work if i switch it to Russian .

Please any help would be very appreciated :) .

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Reznov
  • 175
  • 1
  • 3
  • 14
  • How are you expecting strings to be translated? It doesn't just happen automatically – OneCricketeer Jul 31 '16 at 20:07
  • no, i have compiled all the project contexts, i have the messages.po file and its just work perfectly if i let the @babel.localselector return one of the languages, for example: return 'ru' – Reznov Jul 31 '16 at 20:21
  • i just want to switch between them inside the Jinja templates, something like, RU | EN, so if someone clicked the RU it redirect him to something like {{ url_for('index', lang=ru) }} ! – Reznov Jul 31 '16 at 20:24
  • 1
    Sorry, can't help. All I would be able to do is search the various tutorials I've already found on Flask Babel and try to see how your code works with that. I trust you can find the same material. – OneCricketeer Jul 31 '16 at 21:04
  • @cricket_007, thanks alot dude for your interest, i've found the right place, now its work, here is the link: https://github.com/akavlie/flask-i18n-l10n-tutorial/blob/master/i18n.py – Reznov Jul 31 '16 at 21:54

0 Answers0