Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:01:38+00:00 2026-06-09T16:01:38+00:00

I am utterly confused with trying to use babel with flask for language translation.

  • 0

I am utterly confused with trying to use babel with flask for language translation.

Lets assume the following. A user has a preferance of spanish over english. I have a variable that marks this:

g.user.default_language='sp'

Lets say I have to messages, one on english and the other in spanish that I want to display.
‘Please’ vs ‘por fovor’

<html>
<h1>INSERT TRANSLATION HERE</h1>
</html>

Here is my base babel config file

[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_

So, where does ‘Please’ go and how is it mapped to ‘por fovor’ if user pref is spanish?

Then, how to I call from a template based on language profile?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:01:39+00:00Added an answer on June 9, 2026 at 4:01 pm

    Flask-Babel is really great project. If you looking for jinja, i18n and Flask on google, you will find some useful examples. Please see docs for more info. Here I’ll provide a little example:

    1. Create the translations dir:

    my_website/translations/pt/LC_MESSAGES/
    my_website/translations/en/LC_MESSAGES/
    

    Assuming that your site is in Portuguese and English. It is a manual way to do that. You’d better use pybabel init command.

    2. Create a file named messages.po (these files will contain translated strings):

    $ touch my_website/translations/pt/LC_MESSAGES/messages.po 
    $ printf "msgid \"Hello world\"\nmsgstr \"Olá mundo\"" > my_website/translations/pt/LC_MESSAGES/messages.po
    $ cat my_website/translations/pt/LC_MESSAGES/messages.po
    

    It will create a file with the follow content:

    msgid "HELLO WORLD"
    msgstr "Olá mundo"
    

    3. Compile translation

     $ pybabel compile -d translations
    

    4. Added line code this to your flask’s main app file.

    app.config['BABEL_DEFAULT_LOCALE'] = 'pt_BR' #(context locale to load language).
    

    5. Use _() function in your jinja’s template file in order to see Olá mundo string.

    <h1>{{ _('HELLO WORLD') }}</h1>
    

    I hope it’ll be useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could use some help. I am utterly stuck, been trying to fix this one
I'm utterly bamboozled by an app I'm trying to write which has suddenly stopped
I'm trying to use OSMdroid for a project, and i'm utterly unable to make
I've literally been trying to debug this for a full day and it's utterly
I'm utterly confused. There's an 'assets' folder in my project, but the AssetManager does
Until now answers from SO has been utterly satisfying for my problems. I'm learning
WPF has the Typography.Variants attached property that lets you do superscript and subscript. However,
Is there any java virtual machine implemented in Java language utterly? Thanks
I am trying to do the following: Obtain the address of a member function
There has to be something utterly simple I'm overlooking here, but for the life

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.