pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.73k stars 1.57k forks source link

Translating text OTHER THAN Flask-Admin's ? #1578

Closed vincentwhales closed 6 years ago

vincentwhales commented 6 years ago

I have been spending an entire afternoon trying to figure out why I am able to see the translation for Flask-Admin but not my own.

These are the commands that I am using:

- `pybabel extract -F translations/babel.cfg -o translations/messages.pot .`
- (Run this only for the first time) `pybabel init -i translations/messages.pot -d translations/translations -l zh`
- Edit the messages.po file
- `pybabel compile -d translations/` 

This is what I have so far in views:

  from flask_babelex import Babel
  ... 
  babel = Babel(app)

  @babel.localeselector
  def get_locale():
    if request.args.get('lang'):
      session['lang'] = request.args.get('lang')
    return session.get('lang', 'en')

I am trying to translate this jinja snippet:

      <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
              aria-haspopup="true" aria-expanded="false">{{ _('Language') }}<span class="caret"></span></a>
          <ul class="dropdown-menu">
              <li><a href="?lang=en">{{ _('English') }}</a></li>
              <li><a href="?lang=zh_CN">{{ _('Chinese') }}</a></li>
          </ul>
      </li>

This is my translation folder:

translations/
├── babel.cfg
├── messages.pot
└── zh
    └── LC_MESSAGES
        ├── messages.mo
        └── messages.po

My messages.pot:

#: portal/templates/admin/base.html:91
msgid "Language"
msgstr ""

#: portal/templates/admin/base.html:93
msgid "English"
msgstr ""

#: portal/templates/admin/base.html:94
msgid "Chinese"
msgstr ""

and my messages.po:

#: portal/templates/admin/base.html:93
msgid "English"
msgstr "英文"

#: portal/templates/admin/base.html:94
msgid "Chinese"
msgstr "中文"

And I manually verified that messages.mo is updated correctly (I viewed it in vim).

The strangest thing is, when I added ?lang=zh_CN to my homepage, I am seeing this:

enter image description here

From what I have provided so far:

  1. I know babel has successfully extracted my text and I've translated and compiled them.
  2. Locale setting must somewhat be working because Flask-Admin's text has been translated.

Does anyone know what my own texts are not being translated?

SBillion commented 6 years ago

This is not a Flask admin issue. I guess you forgot to create a babel.cfg file. Please have a look on flask babelex documentation.