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 :) .