1

Description of my usage:

This is my project structure(basically base on Flask Web Development):

├── README.md
├── app
│   ├── __init__.py
│   ├── admin
│   │   ├── __init__.py
│   │   ├── user_admin.py
│   ├── auth
│   │   ├── __init__.py
│   │   ├── forms.py
│   │   ├── views.py
│   ├── decorators.py
│   ├── main
│   │   ├── __init__.py
│   │   ├── errors.py
│   │   ├── forms.py
│   │   ├── views.py
│   ├── models.py
│   └── templates
│       ├── auth
│       │   ├── login.html
│       │   └── register.html
│       ├── base.html
│       ├── edit-profile.html
│       ├── index.html
│       ├── layout.html
│       └── user.html
├── babel.cfg
├── config.py
├── manage.py
├── migrations
│   ├── README
│   ├── alembic.ini
│   ├── env.py
│   ├── script.py.mako
│   └── versions
│       ├── 20c68396e6d8_.py
├── requirement.txt
├── test
└── translations
    └── zh_CN
        └── LC_MESSAGES
            ├── messages.mo
            └── messages.po

Configuration in /babel.cfg and /app/__init__.py

babel.cfg:

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

app/__init__.py:

# ...
from flask_babelex import Babel

babel = Babel()

@babel.localeselector
def get_locale():
    return 'zh_CN'

def create_app(config_name):
    #...
    babel.init_app(app)
    return app

Then I follow the Flask-Babel document

Run $ pybabel extract -F babel.cfg -o messages.pot .

Run $ pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot .

They do have found all the gettexts and lazy_gettexts.

Run $ pybabel init -i messages.pot -d translations -l zh_CN

This generate a /translations/zh_CN/LC_MESSAGES/messages.po for me. And I fixed some translations in it.(including delete the # .fuzzy)

Finally I run $ pybabel compile -d translations. This generate the /tranlations/zh_CN/LC_MESSAGES/messages.mo successfully.

But nothing has been translated......And I really don't know how to fix this bug.

I was really screwed up with this for several days.

For more information,I put this project on Github.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sinux
  • 1,728
  • 3
  • 15
  • 28
  • I don't understand why if you define your locale as `zh` you ended up with a `/translations/zh_CN/LC_MESSAGES` path. The language codes are different. – Miguel Grinberg Jan 26 '15 at 19:38
  • It's confused, I've unified them to 'zh_CN', but still no translation action occur.I'm flattered that you left a comment here, Miguel.Your book and blog helps me a lot. – Sinux Jan 28 '15 at 02:42
  • looks like the only translated texts are fed into Flask-Admin. Maybe you should try something simpler, just put one of these words you have translated directly in a template, to see if that works. – Miguel Grinberg Jan 28 '15 at 03:08
  • They don't work in template either.Wonder if there is something wrong with my local configurations or they goes into wrong 'gettext' method? – Sinux Jan 28 '15 at 03:28

2 Answers2

2

because the actual locale name parsed by babel is "zh_Hans_CN", name your translation directory to be "zh_Hans_CN", and it will be found.

xiechao06
  • 410
  • 6
  • 9
0

pybabel --list-locales

for a list with all valid locales. zh_CN is not part of the list but zh_Hans_CN is.

hanspeters205
  • 381
  • 2
  • 6