{% trans “string” %} not working on templates but {% trans variable %} does

岁酱吖の 提交于 2020-01-01 09:14:11

问题


I'm quite new to Django and I'm working on a project with i18n, the thing is that I have translated some variables using .manage.py makemessages / compilemessages on my template file, but when I use {% trans "my string" %} I got the same "my string" for all the languages.

What am I doing wrong? Here's the code for the views.py and the idioma.html


views.py:

#some code here...

def idioma(request):
    output = _("Mensaje en espanol")
    return render_to_response( 'idioma/idioma.html', { 'idioma' : output }, context_instance = RequestContext(request) )


idioma.html
{% load i18n %}

< form action="/i18n/setlang/" method="post">

        {% csrf_token %}

        < input name="next" type="hidden" value="{{ redirect_to }}" />

        < select name="language" >

        {% get_language_info_list for LANGUAGES as languages %}

        {% for language in languages %}

            < option value="{{ language.code }}">
                {{ language.name_local }} ({{ language.code }})
            < /option>
        {% endfor %}

        </select>

        < input type="submit" value="Go" />

    < /form>

    La cadena es: {% trans idioma  %}

    {% trans "carro" %}


The application translates the idioma variable from the .po and .mo files in locale/path/to/language/

But it doesn't translate the {% trans "carro" %} string.

What's going on?

Thanks for your help!!!!


回答1:


Have you manually translated the string in the .po ?

makemessages just adds "carro" to the .po, generating something like this in the .po file

#: idioma.html:45
msgid "carro"
msgstr ""

and then you have to edit the .po manually adding the translation for that string, in this way:

#: idioma.html:45
msgid "carro"
msgstr "car"

Then, when you are done translating all the .po strings, you can run compilemessages: it will compile your translations.

Note: always remember to look for ,fuzzy translations. If you have something like this in your .po

#: idioma.html:45
#, fuzzy
msgid "carro"
msgstr "car"

That means that django, for some reason, tried to translate the string by itself (it usually happens when you already used that string in a piece of code that you aren't using anymore).

You have to review the translation and delete the #, fuzzy line: any translation tagged with #, fuzzy won't be translated in your pages.




回答2:


I ran into a similar problem and was able to resolve it by setting LOCALE_PATHS in my settings file. LOCALE_PATHS is a tuple of directory paths where django looks for .mo and .po files. Here's an example:

# settings.py
LOCALE_PATHS = (
    '/path/to/your/project/locale',
)

Read django's official documentation on LOCALE_PATHS for more information.




回答3:


see

In some cases {% trans "your string " %} doesn't work

so i will recommend you to use blocktrans instead of trans for strings

how to use blocktrans

{% blocktrans %}  your strnig {% endblocktrans %}



回答4:


I tracked the code whole night and found some clues. Mainly in trans_real.py and gettext.py. You can put a breakpoint in "translation" function.

  1. The translation code are executed only once when app starts. So you need to reload the uwsgi to load the new version.
  2. Po file is never used. gettext.py use "mo" file only.(in my laptop). So you have to compile messages after changing.
  3. The "-" in the language code will be converted to "_" in locale path. For example "zh-CN" will be converted to "zh_CN". That's why the translation file cannot be found. I think it will be better if just using two characters as the language code. And the case sensitive should be considered in Linux system.



回答5:


I had kind of the same issue. I explained it with details in this link under another similar question.

Briefly, my problem has been solved by reloading the server with this code:

sudo /etc/init.d/uwsgi reload

Now, everytime I change the phrases and after compiling language files, I reload the server too to see the changes. I hope this solves some one's problem.



来源:https://stackoverflow.com/questions/12101811/trans-string-not-working-on-templates-but-trans-variable-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!