Skip to content

Adrien-Luxey/multilingual-flask-site

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multilingual Flask site

A minimal installation of Flask and Flask-Babel, so as to propose multiple languages depending on the request URL (e.g. /en/index vs /fr/index).

Installation

Install dependencies through pipenv:

pipenv install 

Everytime you want your Shell session to use the project's dependencies, run:

pipenv shell

Before running any flask command, do setup the required environment variables, e.g. :

export FLASK_APP=flaskr # name of the python module containing our codebase
export FLASK_ENV=development 

Translations

Flask-Babel takes care of business---go read their docs for info.

Only note that, instead of calling pybabel as written in Flask-Babel's documentation, a CLI module is installed on the site to ease things (courtesy of @hazzillrodriguez's flask-multi-language):

  • To generate the translation .po files files for a language, run:

     flask translate init <language-code>
  • Putting fr as the <language-code>, the above would generate the file flaskr/translations/fr/LC_MESSAGES/messages.po. It will look like:

     #: flaskr/templates/base.html:10
     msgid "Multilingual flask site"
     msgstr ""
    
     #: flaskr/templates/base.html:22
     msgid "Home"
     msgstr ""
    
     #: flaskr/templates/base.html:81
     msgid "No rights reserved"
     msgstr ""
    
     #: flaskr/templates/base.html:87
     msgid "Source code"
     msgstr ""
    

    Now fill-in the French translations!

  • Once you're done translating, you can generate Babel's binary output file (.mo):

     flask translate compile
  • Next time you edit your site, only update translations (your existing translations will be kept):

     flask translate update
     # Then recompile
     flask translate compile

Running locally

Simply run:

# If not already done:
pipenv shell
export FLASK_ENV=development
export FLASK_APP=flaskr
# The righteous command:
flask run

Then go check it out on http://127.0.0.1:5000 !

Your site is so beautiful, Adrien!

Thanks, I know, I know... But really, credits go to the Foundation HTML framework.

Buy me a coffee

No thanks! I already had too much.

Paying your taxes is a healthier way to contribute to my salary.

License

Public domain, aka "do whatever you please with this code, it ain't mine".

About

A simple site skeleton demonstrating multilingual Flask + Flask-Babel site through URL prefixes (/en/stuffs, /fr/stuffs ...)

Topics

Resources

License

Stars

Watchers

Forks