{%trans“string” %}不适用于模板,但{%trans variable%}没有 [英] {% trans "string" %} not working on templates but {% trans variable %} does

查看:1664
本文介绍了{%trans“string” %}不适用于模板,但{%trans variable%}没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django很新,我正在使用i18n进行一个项目,事实是我已经使用 .manage.py makemessages / compilemessages 我的模板文件,但是当我使用 {%trans我的字符串%} 我得到相同的我的字符串所有语言。



我做错了什么?以下是 views.py idioma.html





views.py:



#some code here ...


$ b $

返回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 =nexttype =hiddenvalue ={{redirect_to}}/>

<选择name =language>

{%get_language_info_list为LANGUAGES作为语言%}

{%为语言%}

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

< / select>

< input type =submitvalue =Go/>

< /形式>

La cadena es:{%trans idioma%}

{%transcarro%}



该应用程序将locioma变量从locale / path /中的.po和.mo文件翻译成/ language /



但是它不会翻译{%transcarro%}字符串。



发生了什么?



感谢您的帮助!!!!

解决方案

您是否手动翻译了.po?



makemessages 刚刚添加carro到.po,在.po中生成这样的东西文件

 #:idioma.html:45 
msgidcarro
msgstr

然后您必须手动编辑.po 添加该字符串的翻译,以这种方式:

 #:idioma.html:45 
msgidcarro
msgstrcar

然后,当你完成翻译所有的。 po string,你可以运行 compilemessages :它会编译你的翻译。



注意:,模糊的翻译。
如果您的.po中有这样的东西,那么您可以在

 #:idioma.html:45 
# ,fuzzy
msgidcarro
msgstrcar

这意味着django,由于某些原因,尝试自己翻译字符串(通常会发生在您已经在该代码中使用该字符串,而不再使用该代码)



您必须查看翻译并删除#,fuzzy 行:任何标有#的翻译,模糊不会在您的网页中翻译。


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!!!!

解决方案

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.

这篇关于{%trans“string” %}不适用于模板,但{%trans variable%}没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆