【发布时间】:2016-05-02 13:23:36
【问题描述】:
我尝试使用我的 json_file 在我的 html 中构建一些导航栏:
我的 json_file 示例:
{
"_comment":"example auto build navbar",
"TOP" :[
{
"name" : "name1",
"id" : "MLO",
"title" : "Title than i want translate"
}]
}
在我看来.py:
def view(request):
'''
'''
with open('IHMWEB/json_file.json') as data_file:
data = json.load(data_file)
c = {'user_username': request.session['user_username'],
"data" : data}
context = Context(c)
template = get_template('view.html')
translation.activate(settings.LANGUAGE_CODE)
html = template.render(context)
return HttpResponse(html)
在我的模板中:
{% for menu in data.TOP %}
<a href="#" id={{menu.id}} title="{{menu.title}}" class="navbar-brand"> {{menu.name}}</a>
{% endfor %}
如何使用 gettext 翻译“标题”并将翻译发送到我的 template.html?有可能吗?
【问题讨论】:
-
啊,好吧。那么如何改用 Python 文件并将对象导入到模板上下文中呢?这样你就可以使用标准的
ugettext()函数了。 -
正如@C14L 建议的那样,我将从您在设置中定义的
dict/list加载导航结构。这允许您只使用常见的(惰性)django 翻译功能。有关使用这种方法的示例,请参阅 django-oscar e-commerce-framework。 -
"attranslate" 是一个现代工具,可以在 JSON 和 PO/POT 文件之间进行转换:github.com/fkirc/attranslate
标签: python json django translation