ASH84

Software Engineer/Developer, co-founder of Payhere. Ex-Banksalad. Intereseted in iteroperability, bootstrap company, writting.

flask-babel 로 다국어 대응하기

created:2017-10-25
updated:2017-10-27
edit

flask 로 다국어 서비스를 만드는 일을 하고 있는데 일단 생각해 볼 부분이 API 와 WEB 이다. API는 모바일에서 요청이 들어오는 형태인데, 이 경우 모바일의 사용자 언어 설정을 파라미터로 받고 API 레벨에서 DB 내에 다국어 컬럼 혹은 행이 있다는 가정하에 select 를 해서 response 를 내보내면 된다. WEB 의 경우 AcceptLanguage 를 이용할 수도 있고, GET 요청의 파라미터로 언어코드가 전달 된다면 그것을 활용할 수도 있다.

일단 여기서는 WEB 상에서 언어별로 사전을 만들고 jinja 템플릿 안에서 어떻게 언어코드 별로 다른 문자를 보여줄것인지에 대해서 설명한다.

설치하기

pip install flask-babel  

babel.cfg

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

.pot 파일 만들기

$ pybabel extract -F babel.cfg -o messages.pot ./
extracting messages from app.py  
extracting messages from config.py  
extracting messages from templates/index.html (extensions="jinja2.ext.autoescape,jinja2.ext.with_")  
writing PO template file to messages.pot

$ cat messages.pot  
msgid ""  
msgstr ""  

언어별 디렉토리 생성

$ pybabel init -i messages.pot -d ./translations -l en
creating catalog ./translations/en/LC_MESSAGES/messages.po based on messages.pot  
$ pybabel init -i messages.pot -d ./translations -l ja
creating catalog ./translations/ja/LC_MESSAGES/messages.po based on messages.pot  
$ pybabel init -i messages.pot -d ./translations -l zh
creating catalog ./translations/zh/LC_MESSAGES/messages.po based on messages.pot

$ translations  ls -al 
합계 20
drwxrwxr-x 5 ash84 ash84 4096 2017-10-26 13:13 .  
drwxrwxr-x 6 ash84 ash84 4096 2017-10-26 13:13 ..  
drwxrwxr-x 3 ash84 ash84 4096 2017-10-26 13:13 en  
drwxrwxr-x 3 ash84 ash84 4096 2017-10-26 13:13 ja  
drwxrwxr-x 3 ash84 ash84 4096 2017-10-26 13:13 zh  

.po 파일에 번역본 넣기

# ko 
msgid "user_name_label"  
msgstr "사용자 이름 : "  
# zh 
msgid "user_name_label"  
msgstr "用户名 : "  

컴파일 하기

$ pybabel compile -f -d ./translations
compiling catalog ./translations/zh/LC_MESSAGES/messages.po to ./translations/zh/LC_MESSAGES/messages.mo  
compiling catalog ./translations/en/LC_MESSAGES/messages.po to ./translations/en/LC_MESSAGES/messages.mo  
compiling catalog ./translations/ko/LC_MESSAGES/messages.po to ./translations/ko/LC_MESSAGES/messages.mo  
compiling catalog ./translations/ja/LC_MESSAGES/messages.po to ./translations/ja/LC_MESSAGES/messages.mo  

사용하기

@babel.localeselector
def get_locale():  
    return str(request.accept_languages)
    @babel.localeselector
    def get_locale(): 
        if 'api' in request.url:
            return request.get_json().get('lang', 'ko')
        else:
            if request.method == 'GET':
                return request.args.get('lang', 'ko')
            else:
                return request.form.get('lang', 'ko')
<body>  
<H1>HELLO</H1>  
{{ _('user_name_label') }} {{user_name}}
</body>  
render_template('index.html', user_name_label=gettext('user_name_label'))  

flask-babel 에 대해서 간단하게 설명했는데, 더 많은 기능들이 있는것 같다. 아직 다 활용하진 않았는데, 새롭게 사용하는 기능이 있으면 더 올리도록 하겠다. 아래의 URL에 들어가면 위의 내용에 대한 소스를 볼 수가 있다.

https://github.com/AhnSeongHyun/flask-babel-example


#dev  #FLASK  #다국어  #flask-babel  #i18n