[파이썬] Flask로 다국어 지원

Flask is a popular web framework for building web applications using the Python programming language. One of the key features of Flask is its ability to support multiple languages, allowing developers to create websites that can be easily translated into different languages.

In this blog post, we will explore how to implement multilingual support in a Flask application.

Getting Started

Before we begin, make sure you have Flask installed. You can install it using pip:

$ pip install flask

Setting Up Flask-Babel

To add multilingual support to our Flask application, we will use Flask-Babel, a Flask extension that adds i18n and l10n support to the application. To install Flask-Babel, run the following command:

$ pip install flask-babel

Initializing Flask-Babel

To initialize Flask-Babel in our Flask application, we need to import the necessary modules and create an instance of the Babel class. We also need to specify the supported languages and the default language.

Here’s an example code snippet to initialize Flask-Babel:

from flask import Flask
from flask_babel import Babel

app = Flask(__name__)
babel = Babel(app)

@babel.localeselector
def get_locale():
    return request.accept_languages.best_match(app.config['LANGUAGES'])

app.config['LANGUAGES'] = {
    'en': 'English',
    'fr': 'French',
    'ko': 'Korean',
}
app.config['BABEL_DEFAULT_LOCALE'] = 'en'

Adding Translation Strings

Once Flask-Babel is set up, we can start adding translation strings to our templates and code. Flask-Babel uses gettext to handle translations.

Here’s an example of translating a string in a Flask template:


<h1>{{ _('Welcome to my website') }}</h1>

And here’s an example of translating a string in Python code:

from flask_babel import gettext

message = gettext('Hello, world!')

Generating Translation Files

To generate translation files, we need to use the pybabel command-line tool. First, let’s create a directory to store the translation files:

$ mkdir translations

Now, we can use the following command to initialize the translation files:

$ pybabel init -i messages.pot -d translations -l fr

The above command creates a translation file for the French language. You can repeat this command for each language you want to support.

Translating the Messages

To translate the messages, open the translation files (*.po) in a text editor and add the translated strings for each corresponding message.

Here’s an example of a translated message in a French translation file (fr.po):

msgid "Welcome to my website"
msgstr "Bienvenue sur mon site"

Switching Languages

To allow users to switch languages, we can provide language links in our templates. Here’s an example of switching languages using Flask’s url_for function:


<a href="{{ url_for('set_language', lang='en') }}">English</a>
<a href="{{ url_for('set_language', lang='fr') }}">Français</a>
<a href="{{ url_for('set_language', lang='ko') }}">한국어</a>

And here’s an example of the set_language route in Flask:

@app.route('/set-language/<lang>')
def set_language(lang):
    session['language'] = lang
    return redirect(request.referrer)

Conclusion

In this blog post, we learned how to implement multilingual support in a Flask application using Flask-Babel. We explored how to initialize Flask-Babel, add translation strings, generate translation files, and switch languages.

Adding multilingual support to your Flask application can greatly enhance the user experience and make your website more accessible to a global audience. With Flask-Babel, translating your website into multiple languages becomes a much simpler task.

I hope you found this tutorial helpful! Happy coding!