[PYTHON] Let's make a multilingual site using flask-babel

I made it with another language site using flask-babel, so I documented how to make it

I referred to this site, but some of them were written about 5 years ago and do not match the current situation. So I modified it a little.

The sample code is posted on github here. https://github.com/shibacow/flask_babel_sample (Actually, this alone may have achieved the intent of this blog). I think it works if I clone the above repository and put Flask and Flask-Babel in it.

Installation

flask-babel is a library for realizing i18n with flask. The site of flask-babel is here

Install using pip as follows

pip install Flask-Babel 

You can go with the original site.

Preparing for automatic language discrimination

https://github.com/shibacow/flask_babel_sample/blob/master/srv.py

of

@babel.localeselector
def get_locale():
    return request.accept_languages.best_match(['ko','zh','ja', 'ja_JP', 'en'])

Can be automatically determined with.

Preparation of translation source html

https://github.com/shibacow/flask_babel_sample/blob/master/templates/hello.html

Like,

<p>
  message: {{ gettext('Hello world!') }}
</p>

Prepare a sentence enclosed in gettext like. You may use `_ ()` like message: {{_ ('Hello world!')}}` `. This'Hello world!'` `` Is the target of the replacement wording.

translation

After that, prepare babel.cfg as shown in this article,

$ pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot .
$ pybabel init -i messages.pot -d translations -l ja

You can create a ja folder under translations.

With a similar feeling

$ pybabel init -i messages.pot -d translations -l en
$ pybabel init -i messages.pot -d translations -l zh
$ pybabel init -i messages.pot -d translations -l ko

And so on. Language locale names such as ko and zh seem to be determined by the code ISO-639-1 (https://ja.wikipedia.org/wiki/ISO_639-1%E3%82%B3%E3%83% BC% E3% 83% 89% E4% B8% 80% E8% A6% A7)

Then

https://github.com/shibacow/flask_babel_sample/blob/master/translations/ja/LC_MESSAGES/messages.po

Can be done. Open messages.po and put the translated wording in msgid under msgid. msgid is included, but msgid is not included, so fill in the wording you translated yourself.

After translating all the languages

$ pybabel compile -d translations

Compile with. If the word fuzzy is included in /messages.po, it will not be compiled (it is considered to be in the process of translation), so remove the word fuzzy from messages.po.

After compiling, you will have messages.mo.

https://github.com/shibacow/flask_babel_sample/blob/master/translations/ja/LC_MESSAGES/messages.mo

It looks like a binary file.

Confirmation of translation results

If you execute python srv.py in this state, the part that originally contains Hello World will be changed depending on the language selection of the browser.

中国語

And

日本語

It will be replaced with the translated language.

Recommended Posts

Let's make a multilingual site using flask-babel
Let's make a module for Python using SWIG
Let's make a Discord Bot.
Let's make a rock-paper-scissors game
Let's easily make a math gif using Google Colaboratory
Let's make a LINE bot using various services [ngrok edition]
Let's make a remote rumba [Hardware]
Let's make a remote rumba [Software]
Make a face recognizer using TensorFlow
Let's make a breakout with wxPython
Let's make a spot sale service 1
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Let's make a spot sale service 3
Let's make a web chat using WebSocket with AWS serverless (Python)!
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
How to make a Pelican site map
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's make a Backend plugin for Errbot
Let's create a REST API using SpringBoot + MongoDB
Let's replace UWSC with Python (5) Let's make a Robot
[Chat De Tornado] Make a chat using WebSocket with Tornado
I tried to make a ○ ✕ game using TensorFlow
Anyway, I'm at home, so let's make a (site) screenshot function bot on Discord!
Let's make a spot sale service 9 (Task Queue edition)
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Let's make a Makefile and build it (super beginner)
[Let's play with Python] Make a household account book
Let's make dependency management with pip a little easier
How to make a Python package using VS Code
[For play] Let's make Yubaba a LINE Bot (Python)
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Let's make a spot sale service 8 (image uploader edition)
[Super easy] Let's make a LINE BOT with Python.
Make a Sato Yohei discriminator using OpenCV and TensorFlow
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
Let's make a cron program in Java! !! (Task Scheduler)
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Make a squash game
Make a function decorator
I'll make a password!
Make a Nyan button
I want to make a voice changer using Python and SPTK with reference to a famous site
Make a Tetris-style game!
Make a Base64 decoder
Let's make a websocket client with Python. (Access token authentication)
Let's create a function for parametrized test using frame object
Yes, let's make a Minecraft server (Oracle Linux + Spigot + Geyser)
Let's make a robot that solves the Rubik's Cube! 2 Algorithm
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
Let's make a robot that solves the Rubik's Cube! 3 Software
Let's make a robot that solves the Rubik's Cube! 1 Overview