Django 应用程序布局:国际化
在 django 项目中存储语言常量的最佳方式是什么?
例如,我们知道,对于翻译,我们需要这样做:
from django.utils.translation import gettext_lazy as _
class MyThing(models.Model):
name = models.CharField(help_text=_('This is the help text'))
如果我们有很多变体,我们可以使用这样的字典:
from django.utils.translation import gettext_lazy as _
MYTRANSLATION = {
'term1':_('term1'),
'term2':_('term2'),
...
}
所以,我的问题是,在哪里存储带有语言常量的字典......直接在视图中,在模型,在应用程序文件夹中的单独文件中,在项目的根文件夹中等......什么是最好的方法?你在哪里存储你的语言常量?
What is the best way to store language constants in django project?
For example, we know, that for translation we need to do:
from django.utils.translation import gettext_lazy as _
class MyThing(models.Model):
name = models.CharField(help_text=_('This is the help text'))
If we have a lot of variants, we can use dictionary like this:
from django.utils.translation import gettext_lazy as _
MYTRANSLATION = {
'term1':_('term1'),
'term2':_('term2'),
...
}
So, my question is, where to store dictionary with language constants... Directly in view, in model, in separate file in app folder, in root folder of project, etc... What is the best way? And where do you store your language consts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这确实取决于。如果您有一个面向公众的大型网站,想要翻译成 N 种语言,那么它们就不是恒定的,只要内容发生变化,就需要专门的翻译人员进行更新。对于这样的项目,gettext 完全不适合,因为 gettext 对于非技术人员来说很难使用。该网站还需要重新编译和重新部署来更新翻译,如果内容经常更改,这将是主要的破坏因素。 gettext 适用于桌面应用程序(某种程度上,它仍然是翻译人员的主要皮塔),但不适用于网站。
相反,请使用 https://github.com/ojii/django-nani。创建一个表来保存模板中的所有可翻译字符串:
对于您需要的每段文本,将其添加为 I18NString 中的一行。例如,您可以添加带有“short-introduction-text”键的行,并在其中添加英语和德语版本的介绍文本。
然后,对于每个页面加载,加载当前语言的所有翻译:
将 trans 字典传递给模板并呈现翻译后的字符串:
I think it really depends. If you have a big public-facing site that you want to translate into N languages, then they aren't constant and will require updates from dedicated translators whenever the content changes. For a project like that, gettext is wholly unsuitable because gettext is to hard for non-technical persons to use. The site will also require a recompile and redeploy to update the translations which are major dealbreakers if the content is to change often. gettext works for desktop applications (kind of, it is still a major pita for translators) but it does not for web sites.
Instead, use https://github.com/ojii/django-nani. Create a table to hold all translatable strings in your templates:
For each piece of text you need, add it as a row in I18NString. For example, you could add a row with the key "short-introduction-text" and add the English and German versions of the introduction texts there.
Then for each page load, load all translations for the current language:
Pass the trans dictionary to the template and render the translated strings:
实际使用字符串的地方。不要制作一个包含翻译的单独字典 - 它很丑陋,会破坏上下文(xgettext 将无法指向使用字符串的文件)并且基本上无缘无故地重复 i18n 机器的功能。
Where the strings are actually used. Don't make a separate dict containing translations — it's ugly, destroys context (xgettext won't be able to point to the files where strings are used) and basically duplicates functionality of i18n machinery for no reason.