Django 多语言 - 如何预览任何语言的内容?

发布于 2024-09-11 05:39:34 字数 273 浏览 5 评论 0原文

我正在使用 django-multilingual 作为基于 Django 的网站。当我为模型定义 __unicode__ 函数以这种方式返回时:

def __unicode__(self):
        return unicode(self.title)

但是我的默认语言是英语,并且我有一些仅以荷兰语插入的项目。当我预览完整列表时,我得到“无”作为标题。

有没有一种简单的方法可以尝试获取英文标题,然后检查任何其他语言的标题以进行预览?

I'm using django-multilingual for a Django based website. When I define the __unicode__ function for a model to return this way:

def __unicode__(self):
        return unicode(self.title)

However my default language is English and I have some items that are inserted in Dutch only. When I preview the full list, I get "None" as a title.

Is there an easy way to try to get the English title and then check for a title in any other language just for preview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红玫瑰 2024-09-18 05:39:34

迭代所有翻译可以轻松完成,如下所示:

>>> translations = [getattr(obj, "name_" + lang[0].replace("-","_")) for lang in
settings.LANGUAGES]

其中 obj 是模型对象,lang 将表示您设置中的元组('bg'、'保加利亚语')文件。

如果您有“uk-gb”等语言,则需要 lang[0].replace("-","_") ,因为这些值会放入 name_uk_gb强>

Iterating over all translations can be easily done like this:

>>> translations = [getattr(obj, "name_" + lang[0].replace("-","_")) for lang in
settings.LANGUAGES]

Where obj is the model object, and lang will represent a tuple ('bg', 'Bulgarian') from your settings file.

lang[0].replace("-","_") is required, in case that you have languages like "uk-gb", because those values are placed into name_uk_gb

黎夕旧梦 2024-09-18 05:39:34

好吧,假设您的模型将荷兰语版本存储在名为 dutch_title 的属性中,您可以执行此操作。

def __unicode__(self):
  # If we've got an English title, use that
  if self.title:
    return self.title

  # Otherwise, default to the Dutch title
  return self.dutch_title

如果不知道“某些项目仅以荷兰语插入”是什么意思,则很难更有帮助地回答您的问题。

Well, assuming your model stores the Dutch version in an attribute called dutch_title, you could do this.

def __unicode__(self):
  # If we've got an English title, use that
  if self.title:
    return self.title

  # Otherwise, default to the Dutch title
  return self.dutch_title

Without knowing what you mean by "some items are inserted in Dutch only", it's a bit difficult to answer your question more helpfully.

一杯敬自由 2024-09-18 05:39:34

根据您使用的应用程序(例如 - django-multilingual),您可以使用:

<td>{{ object.name_en|escape }}</td>

(示例来自:http://code.google.com/p/django-multilingual/source/browse/trunk/testproject/templates/articles/category_list .html)

我记得这个应用程序还有一些其他分支,它们曾经用来改变这种行为,如下所示:

Objec title is: {{object.en.name}}

如果您使用其他应用程序,您可以随时运行manage.py shell并测试使用 dir(MyModel) 或 dir(MyModel.fields) 来检查那里定义了哪些字段:)

如果这些字段名称有神奇的 getter 并且你在 dir(...) 中看不到任何有趣的东西,你可以参考您喜欢的本地化应用程序的代码,看看幕后发生了什么:)

Depending on the app you are using (for example - django-multilingual), you can use:

<td>{{ object.name_en|escape }}</td>

(example from: http://code.google.com/p/django-multilingual/source/browse/trunk/testproject/templates/articles/category_list.html)

As i remember there were some other forks of this app, that used to change that behavior like this:

Objec title is: {{object.en.name}}

In case you use other app, you can always run the manage.py shell and test with dir(MyModel), or dir(MyModel.fields) to check what fields are defined there :)

In case there are magic getters for those field names and you cant see anything interesting in dir(...), you can refer to the code of your prefered l18n app and see what going under the hood :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文