python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

How to Change Languages with URL PREFIX ? #143

Closed bekab95 closed 4 years ago

bekab95 commented 5 years ago

With Flask Can I change translations with url prefix ? for example mysite.com/en and mysite.com/ka

tvuotila commented 5 years ago

You can always write:

@babel.localeselector
def get_locale():
    return request.url.split('/', 2)[1]
bekab95 commented 5 years ago

I want to make url_prefix logic correctly https://stackoverflow.com/questions/49496761/two-language-flask-site-with-or-without-blueprints this is my example works without babel but I think there is cleaner solution for multiple lang prefixes with cookie and babel

tvuotila commented 5 years ago

Use {% trans %} tags in templates http://jinja.pocoo.org/docs/2.10/templates/#i18n

Do the setup https://pythonhosted.org/Flask-Babel/#flask.ext.babel.Babel Remember to do the "Translating Applications" part also.

You probably want a locale selector like:

@babel.localeselector
def get_locale():
    return getattr(g, 'lang_code', None)
bekab95 commented 5 years ago

I will try but if you have a working example please refer

tvuotila commented 5 years ago

I will try but if you have a working example please refer

I cannot share any of the ones I have worked on, but at the end of this blog, there is a working example of application with i18n.

bekab95 commented 5 years ago

I have seen many examples but it was tricky for me to get fully clean solution for translating..

bekab95 commented 5 years ago

My main question was the function how to switch between Lang s