ID English原文 中文翻译 最近翻译记录 状态 操作
0#翻译 Chapter 19: 第十九章: 翻译
1#翻译 Internationalization 国际化 翻译
4#翻译 Django was originally developed right in the middle of the United States quite literally, as Lawrence, Kansas, is less than 40 miles from the geographic center of the continental United States. Django诞生于美国中部堪萨斯的劳伦斯,距美国的地理中心不到40英里。 翻译
5#翻译 Like most open source projects, though, Django's community grew to include people from all over the globe. 像大多数开源项目一样,Djano社区逐渐开始包括来自全球各地的许多参与者。 翻译
6#翻译 As Django's community became increasingly diverse, <emphasis>internationalization</emphasis> and <emphasis>localization</emphasis> became increasingly important. 鉴于Django社区逐渐变的多样性,<emphasis>国际化</emphasis>和<emphasis>本地化</emphasis>逐渐变得很重要。 翻译
7#翻译 Because many developers have at best a fuzzy understanding of these terms, we'll define them briefly. 由于很多开发者对这些措辞比较困惑,所以我们将简明的定义一下它们。 翻译
9#翻译 <emphasis>Internationalization</emphasis> refers to the process of designing programs for the potential use of any locale. <emphasis> 国际化</emphasis> 是指为了该软件在任何地区的潜在使用而进行程序设计的过程。 翻译
10#翻译 This includes marking text (such as UI elements and error messages) for future translation, abstracting the display of dates and times so that different local standards may be observed, providing support for differing time zones, and generally making sure that the code contains no assumptions about the location of its users. 它包括了为将来翻译而标记的文本(比如用户界面要素和错误信息等)、日期和时间的抽象显示以便保证不同地区的标准得到遵循、为不同时区提供支持,并且一般确保代码中不会存在关于使用者所在地区的假设。 翻译
11#翻译 You'll often see internationalization abbreviated <emphasis>I18N</emphasis> . (The 18 refers to the number of letters omitted between the initial I and the terminal N.) 您会经常看到国际化被缩写为“I18N”(18表示Internationlization这个单词首字母I和结尾字母N之间的字母有18个)。 翻译
13#翻译 <emphasis>Localization</emphasis> refers to the process of actually translating an internationalized program for use in a particular locale. <emphasis> 本地化</emphasis> 是指使一个国际化的程序为了在某个特定地区使用而进行实际翻译的过程。 翻译
14#翻译 You'll sometimes see localization abbreviated as <emphasis>L10N</emphasis> . 有时,本地化缩写为 <emphasis>L10N</emphasis> 。 翻译
16#翻译 Django itself is fully internationalized; all strings are marked for translation, and settings control the display of locale-dependent values like dates and times. Django本身是完全国际化了的,所有的字符串均因翻译所需而被标记,并且设定了与地域无关的显示控制值,如时间和日期。 翻译
17#翻译 Django also ships with more than 50 different localization files. Django是带着50个不同的本地化文件发行的。 翻译
18#翻译 If you're not a native English speaker, theres a good chance that Django is already translated into your primary language. 即使您的母语不是英语,Django也很有可能已经被翻译为您的母语了。 翻译
20#翻译 The same internationalization framework used for these localizations is available for you to use in your own code and templates. 这些本地化文件所使用的国际化框架同样也可以被用在您自己的代码和模板中。 翻译
22#翻译 To use this framework, you'll need to add a minimal number of hooks to your Python code and templates. 您只需要添加少量的挂接代码到您的Python代码和模板中。 翻译
23#翻译 These hooks are called <emphasis>translation strings</emphasis> . They tell Django, This text should be translated into the end user's language, if a translation for this text is available in that language. 这些挂接代码被称为<emphasis> 翻译字符串</emphasis> 。它们告诉Django:如果这段文本的译文可用的话,它应被翻译为终端用户指定的语言。 翻译
25#翻译 Django takes care of using these hooks to translate Web applications, on the fly, according to users' language preferences. Django会根据用户的语言偏好,在线地运用这些挂接指令去翻译Web应用程序。 翻译
27#翻译 Essentially, Django does two things: 本质上来说,Django做两件事情: 翻译
29#翻译 It lets developers and template authors specify which parts of their applications should be translatable. 它让开发者和模板的作者指定他们的应用程序的哪些部分应该被翻译。 翻译
31#翻译 It uses that information to translate Web applications for particular users according to their language preferences. Django根据用户的语言偏好来翻译Web应用程序。 翻译
33#翻译 Note 备注: 翻译
35#翻译 Django's translation machinery uses GNU <literal>gettext</literal> (<reference name="http://www.gnu.org/software/gettext/" refuri="http://www.gnu.org/software/gettext/">http://www.gnu.org/software/gettext/</reference>) via the standard <literal>gettext</literal> module that comes with Python. Django的翻译机制是使用 GNU <literal>gettext</literal> (<reference name="http://www.gnu.org/software/gettext/" refuri="http://www.gnu.org/software/gettext/">http://www.gnu.org/software/gettext/</reference>),具体为Python自带的标准模块 <literal>gettext</literal> 。 翻译
37#翻译 If You Don't Need Internationalization: 如果您不需要国际化: 翻译
39#翻译 Django's internationalization hooks are enabled by default, which incurs a small bit of overhead. Django的国际化挂接是默认开启的,这可能会给Django的运行增加一点点开销。 翻译
40#翻译 If you don't use internationalization, you should set <literal>USE_I18N = False</literal> in your settings file. 如果您不需要国际化支持,那么您可以在您的设置文件中设置 <literal>USE_I18N = False</literal> 。 翻译
41#翻译 If <literal>USE_I18N</literal> is set to <literal>False</literal> , then Django will make some optimizations so as not to load the internationalization machinery. 如果 <literal>USE_I18N</literal> 被设为 False ,那么Django会进行一些优化,而不加载国际化支持机制。 翻译
43#翻译 You'll probably also want to remove <literal>'django.core.context_processors.i18n'</literal> from your <literal>TEMPLATE_CONTEXT_PROCESSORS</literal> setting. 您也可以从您的 <literal>TEMPLATE_CONTEXT_PROCESSORS</literal> 设置中移除 <literal>'django.core.context_processors.i18n'</literal> 。 翻译
45#翻译 The three steps for internationalizing your Django application are: 对你的Django应用进行国际化的三个步骤: 翻译
47#翻译 Embed translation strings in your Python code and templates. 第一步:在你的Python代码和模板中嵌入待翻译的字符串。 翻译
49#翻译 Get translations for those strings, in whichever languages you want to support. 第二步:把那些字符串翻译成你要支持的语言。 翻译
51#翻译 Activate the locale middleware in your Django settings. 第三步:在你的Django settings文件中激活本地中间件。 翻译
53#翻译 We'll cover each one of these steps in detail. 我们将详细地对以上步骤逐一进行描述。 翻译
55#翻译 1. How to Specify Translation Strings 1、如何指定待翻译字符串 翻译
57#翻译 Translation strings specify This text should be translated. 翻译字符串指定这段需要被翻译的文本。 翻译
58#翻译 These strings can appear in your Python code and templates. 这些字符串可以出现在您的Python代码和模板中。 翻译
59#翻译 It's your responsibility to mark translatable strings; the system can only translate strings it knows about. 而标记出这些翻译字符串则是您的责任;系统仅能翻译出它所知道的东西。 翻译
61#翻译 In Python Code 在Python 代码中 翻译
63#翻译 Standard Translation 标准翻译 翻译
65#翻译 Specify a translation string by using the function <literal>ugettext()</literal> . It's convention to import this as a shorter alias, <literal>_</literal> , to save typing. 使用函数 <literal>ugettext()</literal> 来指定一个翻译字符串。 作为惯例,使用短别名 <literal>_</literal> 来引入这个函数以节省键入时间. 翻译
67#翻译 In this example, the text <literal>&quot;Welcome to my site.&quot;</literal> 在下面这个例子中,文本 <literal>"Welcome to my site"</literal> 翻译
68#翻译 is marked as a translation string: 被标记为待翻译字符串: 翻译
71#翻译 Obviously, you could code this without using the alias. 显然,你也可以不使用别名来编码。 翻译
72#翻译 This example is identical to the previous one: 下面这个例子和前面两个例子相同: 翻译
75#翻译 Translation works on computed values. 翻译字符串对于计算出来的值同样有效。 翻译
76#翻译 This example is identical to the previous two: 下面这个例子等同前面一种: 翻译
79#翻译 Translation works on variables. 翻译对变量也同样有效。 翻译
80#翻译 Again, here's an identical example: 这里是一个同样的例子: 翻译
83#翻译 (The caveat with using variables or computed values, as in the previous two examples, is that Django's translation-string-detecting utility, <literal>django-admin.py makemessages</literal> , won't be able to find these strings. (以上两个例子中,对于使用变量或计算值,需要注意的一点是Django的待翻译字符串检测工具, <literal>make-messages.py</literal> ,将不能找到这些字符串。 翻译
84#翻译 More on <literal>makemessages</literal> later.) 稍后,在 <literal>makemessages</literal> 中会有更多讨论。) 翻译
86#翻译 The strings you pass to <literal>_()</literal> or <literal>ugettext()</literal> can take placeholders, specified with Python's standard named-string interpolation syntax. 你传递给 <literal>_()</literal> 或 <literal>gettext()</literal> 的字符串可以接受占位符,由Python标准命名字符串插入句法指定的。 翻译
87#翻译 Example: 例如: 翻译
90#翻译 This technique lets language-specific translations reorder the placeholder text. 这项技术使得特定语言的译文可以对这段文本进行重新排序。 翻译
91#翻译 For example, an English translation may be <literal>&quot;Today is November 26.&quot;</literal> 比如,一段英语译文可能是 <literal>"Today is November 26."</literal> 翻译
92#翻译 , while a Spanish translation may be <literal>&quot;Hoy es 26 de Noviembre.&quot;</literal> ,而一段西班牙语译文会是 <literal>"Hoy es 26 de Noviembre."</literal> 翻译
93#翻译 with the placeholders (the month and the day) with their positions swapped. 使用占位符(月份和日期)交换它们的位置。 翻译
95#翻译 For this reason, you should use named-string interpolation (e.g., <literal>%(day)s</literal> ) instead of positional interpolation (e.g., <literal>%s</literal> or <literal>%d</literal> ) whenever you have more than a single parameter. 由于这个原因,无论何时当你有多于一个单一参数时,你应当使用命名字符串插入(例如: <literal>%(day)s</literal> )来替代位置插入(例如: <literal>%s</literal> or <literal>%d</literal> )。 翻译
96#翻译 If you used positional interpolation, translations wouldn't be able to reorder placeholder text. 如果你使用位置插入的话,翻译动作将不能重新排序占位符文本。 翻译
98#翻译 Marking Strings as No-Op 标记字符串为不操作 翻译
100#翻译 Use the function <literal>django.utils.translation.ugettext_noop()</literal> to mark a string as a translation string without translating it. 使用 <literal>django.utils.translation.gettext_noop()</literal> 函数来标记一个不需要立即翻译的字符串。 翻译
101#翻译 The string is later translated from a variable. 这个串会稍后从变量翻译。 翻译
103#翻译 Use this if you have constant strings that should be stored in the source language because they are exchanged over systems or users such as strings in a database but should be translated at the last possible point in time, such as when the string is presented to the user. 使用这种方法的环境是,有字符串必须以原始语言的形式存储(如储存在数据库中的字符串)而在最后需要被翻译出来(如显示给用户时)。 翻译
105#翻译 Lazy Translation 惰性翻译 翻译
107#翻译 Use the function <literal>django.utils.translation.ugettext_lazy()</literal> to translate strings lazily when the value is accessed rather than when the <literal>ugettext_lazy()</literal> function is called. 使用 <literal>django.utils.translation.gettext_lazy()</literal> 函数,使得其中的值只有在访问时才会被翻译,而不是在 <literal>gettext_lazy()</literal> 被调用时翻译。 翻译
109#翻译 For example, to translate a model's <literal>help_text</literal> , do the following: 例如:要翻译一个模型的 <literal>help_text</literal>,按以下进行: 翻译
112#翻译 In this example, <literal>ugettext_lazy()</literal> stores a lazy reference to the string not the actual translation. 在这个例子中, <literal>ugettext_lazy()</literal> 将字符串作为惰性参照存储,而不是实际翻译。 翻译
113#翻译 The translation itself will be done when the string is used in a string context, such as template rendering on the Django admin site. 翻译工作将在字符串在字符串上下文中被用到时进行,比如在Django管理页面提交模板时。 翻译
115#翻译 The result of a <literal>ugettext_lazy()</literal> call can be used wherever you would use a unicode string (an object with type <literal>unicode</literal> ) in Python. 在Python中,无论何处你要使用一个unicode 字符串(一个<literal>unicode</literal> 类型的对象),您都可以使用一个 <literal>ugettext_lazy()</literal> 调用的结果。 翻译
116#翻译 If you try to use it where a bytestring (a <literal>str</literal> object) is expected, things will not work as expected, since a <literal>ugettext_lazy()</literal> object doesn't know how to convert itself to a bytestring. 一个<literal>ugettext_lazy()</literal>对象并不知道如何把它自己转换成一个字节串。如果你尝试在一个需要字节串的地方使用它,事情将不会如你期待的那样。 翻译
117#翻译 You can't use a unicode string inside a bytestring, either, so this is consistent with normal Python behavior. 同样,你也不能在一个字节串中使用一个 unicode 字符串。所以,这同常规的Python行为是一致的。 翻译
118#翻译 For example: 例如: 翻译
121#翻译 If you ever see output that looks like <literal>&quot;hello <django.utils.functional...>&quot;</literal> , you have tried to insert the result of <literal>ugettext_lazy()</literal> into a bytestring. 如果你曾经见到到像<literal>"hello"</literal>这样的输出,你就可能在一个字节串中插入了<literal>ugettext_lazy()</literal>的结果。 翻译
122#翻译 That's a bug in your code. 在您的代码中,那是一个漏洞。 翻译
124#翻译 If you don't like the verbose name <literal>ugettext_lazy</literal> , you can just alias it as <literal>_</literal> (underscore), like so: 如果觉得 <literal>gettext_lazy</literal> 太过冗长,可以用 <literal>_</literal> (下划线)作为别名,就像这样: 翻译
127#翻译 Always use lazy translations in Django models. 在Django模型中总是无一例外的使用惰性翻译。 翻译
128#翻译 Field names and table names should be marked for translation (otherwise, they won't be translated in the admin interface). 为了翻译,字段名和表名应该被标记。(否则的话,在管理界面中它们将不会被翻译) 翻译
129#翻译 This means writing explicit <literal>verbose_name</literal> and <literal>verbose_name_plural</literal> options in the <literal>Meta</literal> class, though, rather than relying on Django's default determination of <literal>verbose_name</literal> and <literal>verbose_name_plural</literal> by looking at the model's class name: 这意味着在<literal>Meta</literal>类中显式地编写<literal>verbose_nane</literal>和<literal>verbose_name_plural</literal>选项,而不是依赖于Django默认的<literal>verbose_name</literal>和<literal>verbose_name_plural</literal>(通过检查model的类名得到)。 翻译
132#翻译 Pluralization 复数的处理 翻译
134#翻译 Use the function <literal>django.utils.translation.ungettext()</literal> to specify pluralized messages. 使用<literal>django.utils.translation.ungettext()</literal>来指定以复数形式表示的消息。 翻译
135#翻译 Example: 例如: 翻译
138#翻译 <literal>ungettext</literal> takes three arguments: <literal>ngettext</literal> 函数包括三个参数: 翻译
139#翻译 the singular translation string, the plural translation string and the number of objects (which is passed to the translation languages as the <literal>count</literal> variable). 单数形式的翻译字符串,复数形式的翻译字符串,和对象的个数(将以 <literal>count</literal> 变量传递给需要翻译的语言)。 翻译
141#翻译 In Template Code 模板代码 翻译
143#翻译 Translation in Django templates uses two template tags and a slightly different syntax than in Python code. Django模板使用两种模板标签,且语法格式与Python代码有些许不同。 翻译
144#翻译 To give your template access to these tags, put <literal>{% load i18n %}</literal> toward the top of your template. 为了使得模板访问到标签,需要将 <literal>{% load i18n %}</literal> 放在模板最前面。 翻译
146#翻译 The <literal>{% trans %}</literal> template tag translates either a constant string (enclosed in single or double quotes) or variable content: 这个<literal>{% trans %}</literal>模板标记翻译一个常量字符串 (括以单或双引号) 或 可变内容: 翻译
149#翻译 If the <literal>noop</literal> option is present, variable lookup still takes place but the translation is skipped. 如果有<literal>noop</literal> 选项,变量查询还是有效但翻译会跳过。 翻译
150#翻译 This is useful when stubbing out content that will require translation in the future: 当空缺内容要求将来再翻译时,这很有用。 翻译
153#翻译 It's not possible to mix a template variable inside a string within <literal>{% trans %}</literal> . If your translations require strings with variables (placeholders), use <literal>{% blocktrans %}</literal> : 在一个带 <literal>{% trans %}</literal> 的字符串中,混进一个模板变量是不可能的。如果你的译文要求字符串带有变量(占位符placeholders),请使用 <literal>{% blocktrans %}</literal> : 翻译
156#翻译 To translate a template expression say, using template filters you need to bind the expression to a local variable for use within the translation block: 使用模板过滤器来翻译一个模板表达式,需要在翻译的这段文本中将表达式绑定到一个本地变量中: 翻译
159#翻译 If you need to bind more than one expression inside a <literal>blocktrans</literal> tag, separate the pieces with <literal>and</literal> : 如果需要在 <literal>blocktrans</literal> 标签内绑定多个表达式,可以用 <literal>and</literal> 来分隔: 翻译
162#翻译 To pluralize, specify both the singular and plural forms with the <literal>{% plural %}</literal> tag, which appears within <literal>{% blocktrans %}</literal> and <literal>{% endblocktrans %}</literal> . Example: 为了表示单复数相关的内容,需要在 <literal>{% blocktrans %}</literal> 和 <literal>{% endblocktrans %}</literal> 之间使用 <literal>{% plural %}</literal> 标签来指定单复数形式,例如: 翻译
165#翻译 Internally, all block and inline translations use the appropriate <literal>ugettext</literal> / <literal>ungettext</literal> call. 其内在机制是,所有的块和内嵌翻译调用相应的 <literal>gettext</literal> 或 <literal>ngettext</literal> 。 翻译
167#翻译 Each <literal>RequestContext</literal> has access to three translation-specific variables: 每一个<literal>RequestContext</literal>可以访问三个指定翻译变量: 翻译
169#翻译 <literal>LANGUAGES</literal> is a list of tuples in which the first element is the language code and the second is the language name (translated into the currently active locale). <literal>{{ LANGUAGES }}</literal> 是一系列元组组成的列表,每个元组的第一个元素是语言代码,第二个元素是用该语言表示的语言名称。 翻译
171#翻译 <literal>LANGUAGE_CODE</literal> is the current user's preferred language, as a string. 作为一二字符串,<literal>LANGUAGE_CODE</literal>是当前用户的优先语言。 翻译
172#翻译 Example: 例如: 翻译
173#翻译 <literal>en-us</literal> . (See How Django discovers language preference, below.) <literal>en-us</literal>。(请参见下面的Django如何发现语言偏好) 翻译
175#翻译 <literal>LANGUAGE_BIDI</literal> is the current locale's direction. <literal>LANGUAGE_BIDI</literal>就是当前地域的说明。 翻译
176#翻译 If True, it's a right-to-left language, e.g.: 如果为真(True),它就是从右向左书写的语言,例如: 翻译
177#翻译 Hebrew, Arabic. 希伯来语,阿拉伯语。 翻译
178#翻译 If False it's a left-to-right language, e.g.: 如果为假(False),它就是从左到右书写的语言,如: 翻译
179#翻译 English, French, German etc. 英语,法语,德语等。 翻译
181#翻译 If you don't use the <literal>RequestContext</literal> extension, you can get those values with three tags: 如果你不用这个<literal>RequestContext</literal>扩展,你可以用3个标记到那些值: 翻译
184#翻译 These tags also require a <literal>{% load i18n %}</literal> . 这些标记亦要求一个 <literal>{% load i18n %}</literal> 。   翻译
186#翻译 Translation hooks are also available within any template block tag that accepts constant strings. 翻译的hook在任何接受常量字符串的模板块标签内也是可以使用的。 翻译
187#翻译 In those cases, just use <literal>_()</literal> syntax to specify a translation string: 此时,使用 <literal>_()</literal> 表达式来指定翻译字符串,例如: 翻译
190#翻译 In this case, both the tag and the filter will see the already-translated string, so they don't need to be aware of translations. 在这种情况下,标记和过滤器两个都会看到已经翻译的字符串,所有它们并不需要提防翻译操作。 翻译
192#翻译 Note 备注: 翻译
194#翻译 In this example, the translation infrastructure will be passed the string <literal>&quot;yes,no&quot;</literal> , not the individual strings <literal>&quot;yes&quot;</literal> and <literal>&quot;no&quot;</literal> . The translated string will need to contain the comma so that the filter parsing code knows how to split up the arguments. 在这个例子中,翻译结构将放过字符串<literal>"yes,no"</literal>,而不是单独的字符串<literal>"yes"</literal>和<literal>"no"</literal>。翻译的字符串将需要包括逗号以便过滤器解析代码明白如何分割参数。 翻译
195#翻译 For example, a German translator might translate the string <literal>&quot;yes,no&quot;</literal> as <literal>&quot;ja,nein&quot;</literal> (keeping the comma intact). 例如, 一个德语翻译器可能会翻译字符串 <literal>"yes,no"</literal> 为 <literal>"ja,nein"</literal> (保持逗号原封不动)。 翻译
197#翻译 Working With Lazy Translation Objects 与惰性翻译对象一道工作 翻译
199#翻译 Using <literal>ugettext_lazy()</literal> and <literal>ungettext_lazy()</literal> to mark strings in models and utility functions is a common operation. 在模型和公用函数中,使用<literal>ugettext_lazy()</literal>和<literal>ungettext_lazy()</literal>来标记字符串是很普遍的操作。 翻译
200#翻译 When you're working with these objects elsewhere in your code, you should ensure that you don't accidentally convert them to strings, because they should be converted as late as possible (so that the correct locale is in effect). 当你在你的代码中其它地方使用这些对象时,你应当确定你不会意外地转换它们成一个字符串,因为它们应被尽量晚地转换(以便正确的地域生效) 翻译
201#翻译 This necessitates the use of a couple of helper functions. 这需要使用及个帮助函数。 翻译
203#翻译 Joining Strings: 拼接字符串: 翻译
204#翻译 string_concat() string_concat() 翻译
206#翻译 Standard Python string joins (<literal>''.join([...])</literal> ) will not work on lists containing lazy translation objects. 标准Python字符串拼接(<literal>''.join([...])</literal> ) 将不会工作在包括惰性翻译对象的列表上。 翻译
207#翻译 Instead, you can use <literal>django.utils.translation.string_concat()</literal> , which creates a lazy object that concatenates its contents <emphasis>and</emphasis> converts them to strings only when the result is included in a string. 作为替代,你可以使用<literal>django.utils.translation.string_concat()</literal>, 这个函数创建了一个惰性对象,其连接起它的内容 <emphasis>并且</emphasis> 仅当结果被包括在一个字符串中时转换它们为字符串 。 翻译
208#翻译 For example: 例如: 翻译
211#翻译 In this case, the lazy translations in <literal>result</literal> will only be converted to strings when <literal>result</literal> itself is used in a string (usually at template rendering time). 在这种情况下,当 <literal>result</literal> 自己被用与一个字符串时, <literal>result</literal> 中的惰性翻译将仅被转换为字符串(通常在模板渲染时间)。  翻译
213#翻译 The allow_lazy() Decorator allow_lazy() 修饰符 翻译
215#翻译 Django offers many utility functions (particularly in <literal>django.utils</literal> ) that take a string as their first argument and do something to that string. Django提供很多功能函数(如:取一个字符串作为他们的第一个参数并且对那个字符串做些什么)。(尤其在 <literal>django.utils</literal> 中) 翻译
216#翻译 These functions are used by template filters as well as directly in other code. 这些函数被模板过滤器像在其他代码中一样直接使用。 翻译
218#翻译 If you write your own similar functions and deal with translations, you'll face the problem of what to do when the first argument is a lazy translation object. 如果你写你自己的类似函数并且与翻译打交道,当第一个参数是惰性翻译对象时,你会面临“做什么”的难题。 翻译
219#翻译 You don't want to convert it to a string immediately, because you might be using this function outside of a view (and hence the current thread's locale setting will not be correct). 因为你可能在视图之外使用这个函数(并且因此当前线程的本地设置将会不正确),所以你不想立即转换其为一个字符串。 翻译
221#翻译 For cases like this, use the <literal>django.utils.functional.allow_lazy()</literal> decorator. 象这种情况,请使用 <literal>django.utils.functional.allow_lazy()</literal> 修饰符。 翻译
222#翻译 It modifies the function so that <emphasis>if</emphasis> it's called with a lazy translation as the first argument, the function evaluation is delayed until it needs to be converted to a string. 它修改这个函数以便 <emphasis>假如</emphasis>第一个参数是一个惰性翻译, 这个函数的赋值会被延后直到它需要被转化为一个字符串为止。 翻译
224#翻译 For example: 例如: 翻译
227#翻译 The <literal>allow_lazy()</literal> decorator takes, in addition to the function to decorate, a number of extra arguments (<literal>*args</literal> ) specifying the type(s) that the original function can return. <literal>allow_lazy()</literal> 装饰符 采用了另外的函数来装饰,以及一定量的,原始函数可以返回的特定类型的额外参数 (<literal>*args</literal> ) 。 翻译
228#翻译 Usually, it's enough to include <literal>unicode</literal> here and ensure that your function returns only Unicode strings. 通常,在这里包括 <literal>unicode</literal> 就足够了并且确定你的函数将仅返回Unicode字符串。 翻译
230#翻译 Using this decorator means you can write your function and assume that the input is a proper string, then add support for lazy translation objects at the end. 使用这个修饰符意味着你能写你的函数并且假设输入是合适的字符串,然后在末尾添加对惰性翻译对象的支持。 翻译
232#翻译 2. How to Create Language Files 2、如何创建语言文件 翻译
234#翻译 Once you've tagged your strings for later translation, you need to write (or obtain) the language translations themselves. 当你标记了翻译字符串,你就需要写出(或获取已有的)对应的语言翻译信息。 翻译
235#翻译 Here's how that works. 这里就是它如何工作的。 翻译
237#翻译 Locale restrictions 地域限制 翻译
239#翻译 Django does not support localizing your application into a locale for which Django itself has not been translated. Django不支持把你的应用本地化到一个连它自己都还没被翻译的地域。 翻译
240#翻译 In this case, it will ignore your translation files. 在这种情况下,它将忽略你的翻译文件。 翻译
241#翻译 If you were to try this and Django supported it, you would inevitably see a mixture of translated strings (from your application) and English strings (from Django itself). 如果你想尝试这个并且Django支持它,你会不可避免地见到这样一个混合体------参杂着你的译文和来自Django自己的英文。 翻译
242#翻译 If you want to support a locale for your application that is not already part of Django, you'll need to make at least a minimal translation of the Django core. 如果你的应用需要你支持一个Django中没有的地域,你将至少需要做一个Django core的最小翻译。 翻译
244#翻译 Message Files 消息文件 翻译
246#翻译 The first step is to create a <emphasis>message file</emphasis> for a new language. 第一步,就是为一种语言创建一个信息文件。 翻译
247#翻译 A message file is a plain-text file, representing a single language, that contains all available translation strings and how they should be represented in the given language. 信息文件是包含了某一语言翻译字符串和对这些字符串的翻译的一个文本文件。 翻译
248#翻译 Message files have a <literal>.po</literal> file extension. 信息文件以 <literal>.po</literal> 为后缀名。 翻译
250#翻译 Django comes with a tool, <literal>django-admin.py makemessages</literal> , that automates the creation and upkeep of these files. Django中带有一个工具, <literal>bin/make-messages.py</literal> ,它完成了这些文件的创建和维护工作。 翻译
251#翻译 To create or update a message file, run this command: 运行以下命令来创建或更新一个信息文件: 翻译
254#翻译 where <literal>de</literal> is the language code for the message file you want to create. 其中 <literal>de</literal> 是所创建的信息文件的语言代码。 翻译
255#翻译 The language code, in this case, is in locale format. 在这里,语言代码是以本地格式给出的。 翻译
256#翻译 For example, it's <literal>pt_BR</literal> for Brazilian Portuguese and <literal>de_AT</literal> for Austrian German. 例如,巴西地区的葡萄牙语为 <literal>pt_BR</literal> ,澳大利亚地区的德语为 <literal>de_AT</literal> 。 翻译
258#翻译 The script should be run from one of three places: 这段脚本应该在三处之一运行: 翻译
260#翻译 The root directory of your Django project. Django项目根目录。 翻译
262#翻译 The root directory of your Django app. 您Django应用的根目录。 翻译
264#翻译 The root <literal>django</literal> directory (not a Subversion checkout, but the one that is linked-to via <literal>$PYTHONPATH</literal> or is located somewhere on that path). <literal>django</literal> 根目录(不是Subversion检出目录,而是通过 <literal>$PYTHONPATH</literal> 链接或位于该路径的某处)。 翻译
265#翻译 This is only relevant when you are creating a translation for Django itself. 这仅和你为Django自己创建一个翻译时有关。 翻译
267#翻译 This script runs over your project source tree or your application source tree and pulls out all strings marked for translation. 这段脚本遍历你的项目源树或你的应用程序源树并且提取出所有为翻译而被标记的字符串。 翻译
268#翻译 It creates (or updates) a message file in the directory <literal>locale/LANG/LC_MESSAGES</literal> . In the <literal>de</literal> example, the file will be <literal>locale/de/LC_MESSAGES/django.po</literal> . 它在 <literal>locale/LANG/LC_MESSAGES</literal> 目录下创建(或更新)了一个信息文件。针对上面的<literal>de</literal>,应该是<literal>locale/de/LC_MESSAGES/django.po</literal>。 翻译
270#翻译 By default <literal>django-admin.py makemessages</literal> examines every file that has the <literal>.html</literal> file extension. 作为默认, <literal>django-admin.py makemessages</literal> 检测每一个有 <literal>.html</literal> 扩展名的文件。  翻译
271#翻译 In case you want to override that default, use the <literal>--extension</literal> or <literal>-e</literal> option to specify the file extensions to examine: 以备你要重载缺省值,使用 <literal>--extension</literal> 或 <literal>-e</literal> 选项指定文件扩展名来检测。 翻译
274#翻译 Separate multiple extensions with commas and/or use <literal>-e</literal> or <literal>--extension</literal> multiple times: 用逗号和(或)使用<literal>-e</literal>或<literal>--extension</literal>来分隔多项扩展名: 翻译
277#翻译 When creating JavaScript translation catalogs (which we'll cover later in this chapter,) you need to use the special djangojs' domain, <strong>not</strong> 当创建JavaScript翻译目录时,你需要使用特殊的Django域:<strong>not</strong> 翻译
278#翻译 <literal>-e js</literal> . <literal>-e js</literal> 。 翻译
280#翻译 No gettext? 没有gettext? 翻译
282#翻译 If you don't have the <literal>gettext</literal> utilities installed, <literal>django-admin.py makemessages</literal> will create empty files. 如果没有安装 <literal>gettext</literal> 组件, <literal>make-messages.py</literal> 将会创建空白文件。 翻译
283#翻译 If that's the case, either install the <literal>gettext</literal> utilities or just copy the English message file (<literal>locale/en/LC_MESSAGES/django.po</literal> ) if available and use it as a starting point; it's just an empty translation file. 这种情况下,安装 <literal>gettext</literal> 组件或只是复制英语信息文件( <literal>conf/locale/en/LC_MESSAGES/django.po</literal> )来作为一个起点;只是一个空白的翻译信息文件而已。 翻译
285#翻译 Working on Windows? 工作在Windows上么? 翻译
287#翻译 If you're using Windows and need to install the GNU gettext utilities so <literal>django-admin makemessages</literal> works, see the gettext on Windows section below for more information. 如果你正在使用Windows,且需要安装GNU gettext共用程序以便 <literal>django-admin makemessages</literal> 可以工作,请参看下面Windows小节中gettext部分以获得更多信息。 翻译
289#翻译 The format of <literal>.po</literal> files is straightforward. <literal>.po</literal> 文件格式很直观。 翻译
290#翻译 Each <literal>.po</literal> file contains a small bit of metadata, such as the translation maintainer's contact information, but the bulk of the file is a list of <emphasis>messages</emphasis> simple mappings between translation strings and the actual translated text for the particular language. 每个 <literal>.po</literal> 文件包含一小部分的元数据,比如翻译维护人员的联系信息,而文件的大部分内容是简单的翻译字符串和对应语言翻译结果的映射关系的列表。 翻译
292#翻译 For example, if your Django app contained a translation string for the text <literal>&quot;Welcome to my site.&quot;</literal> 举个例子,如果Django应用程序包括一个 <literal>"Welcome to my site."</literal> 的待翻译字符串 翻译
293#翻译 , like so: ,像这样: 翻译
296#翻译 then <literal>django-admin.py makemessages</literal> will have created a <literal>.po</literal> file containing the following snippet a message: 则<literal>django-admin.py makemessages</literal>将创建一个 <literal>.po</literal> 文件来包含以下片段的消息: 翻译
299#翻译 A quick explanation: 快速解释: 翻译
301#翻译 <literal>msgid</literal> is the translation string, which appears in the source. <literal>msgid</literal> 是在源文件中出现的翻译字符串。 翻译
302#翻译 Don't change it. 不要做改动。 翻译
304#翻译 <literal>msgstr</literal> is where you put the language-specific translation. <literal>msgstr</literal> 是相应语言的翻译结果。 翻译
305#翻译 It starts out empty, so it's your responsibility to change it. 刚创建时它只是空字符串,此时就需要你来完成它。 翻译
306#翻译 Make sure you keep the quotes around your translation. 注意不要丢掉语句前后的引号。 翻译
308#翻译 As a convenience, each message includes, in the form of a comment line prefixed with <literal>#</literal> and located above the <literal>msgid</literal> line, the filename and line number from which the translation string was gleaned. 作为方便之处,每一个消息都包括:以 <literal>#</literal> 为前缀的一个注释行并且定位上边的<literal>msgid</literal> 行,文件名和行号。 翻译
310#翻译 Long messages are a special case. 对于比较长的信息也有其处理方法。 翻译
311#翻译 There, the first string directly after the <literal>msgstr</literal> (or <literal>msgid</literal> ) is an empty string. <literal>msgstr</literal> (或 <literal>msgid</literal> )后紧跟着的字符串为一个空字符串。 翻译
312#翻译 Then the content itself will be written over the next few lines as one string per line. 然后真正的内容在其下面的几行。 翻译
313#翻译 Those strings are directly concatenated. 这些字符串会被直接连在一起。 翻译
314#翻译 Don't forget trailing spaces within the strings; otherwise, they'll be tacked together without whitespace! 同时,不要忘了字符串末尾的空格,因为它们会不加空格地连到一起。 翻译
316#翻译 To reexamine all source code and templates for new translation strings and update all message files for <emphasis>all</emphasis> languages, run this: 若要对新创建的翻译字符串校验所有的源代码和模板,并且更新所有语言的信息文件,可以运行以下命令: 翻译
319#翻译 Compiling Message Files 编译信息文件 翻译
321#翻译 After you create your message file and each time you make changes to it you'll need to compile it into a more efficient form, for use by <literal>gettext</literal> . Do this with the <literal>django-admin.py compilemessages</literal> utility. 创建信息文件之后,每次对其做了修改,都需要将它重新编译成一种更有效率的形式,供 <literal>gettext</literal> 使用。可以使用<literal>django-admin.py compilemessages</literal>完成。 翻译
323#翻译 This tool runs over all available <literal>.po</literal> files and creates <literal>.mo</literal> files, which are binary files optimized for use by <literal>gettext</literal> . In the same directory from which you ran <literal>django-admin.py makemessages</literal> , run <literal>django-admin.py compilemessages</literal> like this: 这个工具作用于所有有效的 <literal>.po</literal> 文件,创建优化过的二进制 <literal>.mo</literal> 文件供 <literal>gettext</literal> 使用。在你可以运行<literal>django-admin.py makemessages</literal>的目录下,运行<literal>django-admin.py compilemessages</literal>: 翻译
326#翻译 That's it. 就是这样了。 翻译
327#翻译 Your translations are ready for use. 你的翻译成果已经可以使用了。 翻译
329#翻译 3. How Django Discovers Language Preference Django如何处理语言偏好 翻译
331#翻译 Once you've prepared your translations or, if you just want to use the translations that come with Django you'll just need to activate translation for your app. 一旦你准备好了翻译,如果希望在Django中使用,那么只需要激活这些翻译即可。 翻译
333#翻译 Behind the scenes, Django has a very flexible model of deciding which language should be used installation-wide, for a particular user, or both. 在这些功能背后,Django拥有一个灵活的模型来确定在安装和使用应用程序的过程中选择使用的语言。 翻译
335#翻译 To set an installation-wide language preference, set <literal>LANGUAGE_CODE</literal> . Django uses this language as the default translation the final attempt if no other translator finds a translation. 要设定一个安装阶段的语种偏好,请设定<literal>LANGUAGE_CODE</literal>。如果其他翻译器没有找到一个译文,Django将使用这个语种作为缺省的翻译最终尝试。 翻译
337#翻译 If all you want to do is run Django with your native language, and a language file is available for your language, all you need to do is set <literal>LANGUAGE_CODE</literal> . 如果你只是想要用本地语言来运行Django,并且该语言的语言文件存在,只需要简单地设置 <literal>LANGUAGE_CODE</literal> 即可。 翻译
339#翻译 If you want to let each individual user specify which language he or she prefers, use <literal>LocaleMiddleware</literal> . <literal>LocaleMiddleware</literal> enables language selection based on data from the request. 如果要让每一个使用者各自指定语言偏好,就需要使用 <literal>LocaleMiddleware</literal> 。 <literal>LocaleMiddleware</literal> 使得Django基于请求的数据进行语言选择,从而为每一位用户定制内容。 翻译
340#翻译 It customizes content for each user. 它为每一个用户定制内容。 翻译
342#翻译 To use <literal>LocaleMiddleware</literal> , add <literal>'django.middleware.locale.LocaleMiddleware'</literal> to your <literal>MIDDLEWARE_CLASSES</literal> setting. 使用 <literal>LocaleMiddleware</literal> 需要在 <literal>MIDDLEWARE_CLASSES</literal> 设置中增加 <literal>'django.middleware.locale.LocaleMiddleware'</literal> 。 翻译
343#翻译 Because middleware order matters, you should follow these guidelines: 中间件的顺序是有影响的,最好按照依照以下要求: 翻译
345#翻译 Make sure it's one of the first middlewares installed. 保证它是第一批安装的中间件类。 翻译
347#翻译 It should come after <literal>SessionMiddleware</literal> , because <literal>LocaleMiddleware</literal> makes use of session data. 因为 <literal>LocalMiddleware</literal> 要用到session数据,所以需要放在 <literal>SessionMiddleware</literal> 之后。 翻译
349#翻译 If you use <literal>CacheMiddleware</literal> , put <literal>LocaleMiddleware</literal> after it. 如果你使用<literal>CacheMiddleware</literal>,把<literal>LocaleMiddleware</literal>放在它后面。 翻译
351#翻译 For example, your <literal>MIDDLEWARE_CLASSES</literal> might look like this: 例如, <literal>MIDDLE_CLASSES</literal> 可能会是如此: 翻译
354#翻译 (For more on middleware, see Chapter 17.) (更多关于中间件的内容,请参阅第17章) 翻译
356#翻译 <literal>LocaleMiddleware</literal> tries to determine the user's language preference by following this algorithm: <literal>LocaleMiddleware</literal> 按照如下算法确定用户的语言: 翻译
358#翻译 First, it looks for a <literal>django_language</literal> key in the current user's session. 首先,在当前用户的 session 的中查找<literal>django_language</literal>键; 翻译
360#翻译 Failing that, it looks for a cookie. 如未找到,它会找寻一个cookie 翻译
362#翻译 Failing that, it looks at the <literal>Accept-Language</literal> HTTP header. 还找不到的话,它会在 HTTP 请求头部里查找<literal>Accept-Language</literal>, 翻译
363#翻译 This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. 该头部是你的浏览器发送的,并且按优先顺序告诉服务器你的语言偏好。 翻译
364#翻译 Django tries each language in the header until it finds one with available translations. Django会尝试头部中的每一个语种直到它发现一个可用的翻译。 翻译
366#翻译 Failing that, it uses the global <literal>LANGUAGE_CODE</literal> setting. 以上都失败了的话, 就使用全局的 <literal>LANGUAGE_CODE</literal> 设定值。 翻译
368#翻译 Notes: 备注: 翻译
370#翻译 In each of these places, the language preference is expected to be in the standard language format, as a string. 在上述每一处,语种偏好应作为字符串,以标准的语种格式出现。 翻译
371#翻译 For example, Brazilian Portuguese is <literal>pt-br</literal> . 例如,巴西葡萄牙语是<literal>pt-br</literal> 翻译
373#翻译 If a base language is available but the sublanguage specified is not, Django uses the base language. 如果一个基本语种存在而亚语种没有指定,Django将使用基本语种。 翻译
374#翻译 For example, if a user specifies <literal>de-at</literal> (Austrian German) but Django only has <literal>de</literal> available, Django uses <literal>de</literal> . 比如,如果用户指定了 <literal>de-at</literal> (澳式德语)但Django只有针对 <literal>de</literal> 的翻译,那么 <literal>de</literal> 会被选用。 翻译
376#翻译 Only languages listed in the <literal>LANGUAGES</literal> setting can be selected. 只有在 <literal>LANGUAGES</literal> 设置中列出的语言才能被选用。 翻译
377#翻译 If you want to restrict the language selection to a subset of provided languages (because your application doesn't provide all those languages), set <literal>LANGUAGES</literal> to a list of languages. 若希望将语言限制为所提供语言中的某些(因为应用程序并不提供所有语言的表示),则将 <literal>LANGUAGES</literal> 设置为所希望提供语言的列表,例如: 翻译
378#翻译 For example: 例如: 翻译
381#翻译 This example restricts languages that are available for automatic selection to German and English (and any sublanguage, like <literal>de-ch</literal> or <literal>en-us</literal> ). 上面这个例子限制了语言偏好只能是德语和英语(包括它们的子语言,如 <literal>de-ch</literal> 和 <literal>en-us</literal> )。 翻译
383#翻译 If you define a custom <literal>LANGUAGES</literal> setting, as explained in the previous bullet, it's OK to mark the languages as translation strings but use a dummy <literal>ugettext()</literal> function, not the one in <literal>django.utils.translation</literal> . You should <emphasis>never</emphasis> import <literal>django.utils.translation</literal> from within your settings file, because that module in itself depends on the settings, and that would cause a circular import. 如果自定义了 <literal>LANGUAGES</literal> ,将语言标记为翻译字符串是可以的,但是,请不要使用 <literal>django.utils.translation</literal> 中的 <literal>gettext()</literal> (决不要在settings文件中导入 <literal>django.utils.translation</literal> ,因为这个模块本身是依赖于settings,这样做会导致无限循环),而是使用一个“虚构的” <literal>gettext()</literal> 。 翻译
385#翻译 The solution is to use a dummy <literal>ugettext()</literal> function. 解决方案就是使用一个“虚假的” <literal>gettext()</literal> 。以 翻译
386#翻译 Here's a sample settings file: 下是一个settings文件的例子: 翻译
389#翻译 With this arrangement, <literal>django-admin.py makemessages</literal> will still find and mark these strings for translation, but the translation won't happen at runtime so you'll have to remember to wrap the languages in the <emphasis>real</emphasis> 这样做的话, <literal>make-messages.py</literal> 仍会寻找并标记出将要被翻译的这些字符串,但翻译不会在运行时进行,故而需要在任何使用 <emphasis>LANGUAGES</emphasis> 的代码中用“真实的” 翻译
390#翻译 <literal>ugettext()</literal> in any code that uses <literal>LANGUAGES</literal> at runtime. <literal>ugettext()</literal>。 翻译
392#翻译 The <literal>LocaleMiddleware</literal> can only select languages for which there is a Django-provided base translation. <literal>LocaleMiddleware</literal> 只能选择那些Django已经提供了基础翻译的语言。 翻译
393#翻译 If you want to provide translations for your application that aren't already in the set of translations in Django's source tree, youll want to provide at least basic translations for that language. 如果想要在应用程序中对Django中还没有基础翻译的语言提供翻译,那么必须至少先提供该语言的基本的翻译。 翻译
394#翻译 For example, Django uses technical message IDs to translate date formats and time formats so you will need at least those translations for the system to work correctly. 例如,Django使用特定的信息ID来翻译日期和时间格式,故要让系统正常工作,至少要提供这些基本的翻译。 翻译
396#翻译 A good starting point is to copy the English <literal>.po</literal> file and to translate at least the technical messages maybe the validation messages, too. 以英语的 <literal>.po</literal> 文件为基础,翻译其中的技术相关的信息,可能还包括一些使之生效的信息。 翻译
398#翻译 Technical message IDs are easily recognized; they're all upper case. 技术相关的信息ID很容易被认出来:它们都是大写的。 翻译
399#翻译 You dont translate the message ID as with other messages, you provide the correct local variant on the provided English value. 这些信息ID的翻译与其他信息不同:你需要提供其对应的本地化内容。 翻译
400#翻译 For example, with <literal>DATETIME_FORMAT</literal> (or <literal>DATE_FORMAT</literal> or <literal>TIME_FORMAT</literal> ), this would be the format string that you want to use in your language. 例如,对于 DATETIME_FORMAT (或 DATE_FORMAT 、 TIME_FORMAT ),应该提供希望在该语言中使用的格式化字符串。 翻译
401#翻译 The format is identical to the format strings used by the <literal>now</literal> template tag. 格式被模板标签<literal>now</literal>用来识别格式字符串。 翻译
403#翻译 Once <literal>LocaleMiddleware</literal> determines the user's preference, it makes this preference available as <literal>request.LANGUAGE_CODE</literal> for each <literal>HttpRequest</literal> . Feel free to read this value in your view code. 一旦<literal>LocaleMiddleware</literal>决定用户的偏好,它会让这个偏好作为<literal>request.LANGUAGE_CODE</literal>对每一个<literal>HttpRequest</literal>有效。请随意在你的视图代码中读一读这个值。 翻译
404#翻译 Here's a simple example: 以下是一个简单的例子: 翻译
407#翻译 Note that, with static (middleware-less) translation, the language is in <literal>settings.LANGUAGE_CODE</literal> , while with dynamic (middleware) translation, it's in <literal>request.LANGUAGE_CODE</literal> . 注意,对于静态翻译(无中间件)而言,此语言在<literal>settings.LANGUAGE_CODE</literal>中,而对于动态翻译(中间件),它在<literal>request.LANGUAGE_CODE</literal>中。 翻译
409#翻译 Using Translations in Your Own Projects 在你自己的项目中使用翻译 翻译
411#翻译 Django looks for translations by following this algorithm: Django使用以下算法寻找翻译: 翻译
413#翻译 First, it looks for a <literal>locale</literal> directory in the application directory of the view that's being called. 首先,Django在该视图所在的应用程序文件夹中寻找 <literal>locale</literal> 目录。 翻译
414#翻译 If it finds a translation for the selected language, the translation will be installed. 若找到所选语言的翻译,则加载该翻译。 翻译
416#翻译 Next, it looks for a <literal>locale</literal> directory in the project directory. 第二步,Django在项目目录中寻找 <literal>locale</literal> 目录。 翻译
417#翻译 If it finds a translation, the translation will be installed. 若找到翻译,则加载该翻译。 翻译
419#翻译 Finally, it checks the Django-provided base translation in <literal>django/conf/locale</literal> . 最后,Django使用 <literal>django/conf/locale</literal> 目录中的基本翻译。 翻译
421#翻译 This way, you can write applications that include their own translations, and you can override base translations in your project path. 以这种方式,你可以创建包含独立翻译的应用程序,可以覆盖项目中的基本翻译。 翻译
422#翻译 Or, you can just build a big project out of several apps and put all translations into one big project message file. 或者,你可以创建一个包含几个应用程序的大项目,并将所有需要的翻译放在一个大的项目信息文件中。 翻译
423#翻译 The choice is yours. 决定权在你手中。 翻译
425#翻译 All message file repositories are structured the same way. 所有的信息文件库都是以同样方式组织的: 翻译
426#翻译 They are: 它们是: 翻译
428#翻译 <literal>$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)</literal> <literal>$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)</literal> 翻译
430#翻译 <literal>$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)</literal> <literal>$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)</literal> 翻译
432#翻译 All paths listed in <literal>LOCALE_PATHS</literal> in your settings file are searched in that order for <literal><language>/LC_MESSAGES/django.(po|mo)</literal> 所有在settings文件中 <literal>LOCALE_PATHS</literal> 中列出的路径以其列出的顺序搜索 <literal><language>/LC_MESSAGES/django.(po|mo)</literal> 翻译
434#翻译 <literal>$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)</literal> <literal>$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)</literal> 翻译
436#翻译 To create message files, you use the same <literal>django-admin.py makemessages</literal> tool as with the Django message files. 要创建信息文件,也是使用 <literal>django-admin.py makemessages.py</literal> 工具,和Django信息文件一样。 翻译
437#翻译 You only need to be in the right place in the directory where either the <literal>conf/locale</literal> (in case of the source tree) or the <literal>locale/</literal> (in case of app messages or project messages) directory are located. 需要做的就是进入正确的目录—— <literal>conf/locale</literal> (在源码树的情况下)或者 <literal>locale/</literal> (在应用程序信息或项目信息的情况下)所在的目录下。 翻译
438#翻译 And you use the same <literal>django-admin.py compilemessages</literal> to produce the binary <literal>django.mo</literal> files that are used by <literal>gettext</literal> . 同样地,使用 <literal>compile-messages.py</literal> 生成 <literal>gettext</literal> 需要使用的二进制 <literal>django.mo</literal> 文件。 翻译
440#翻译 You can also run <literal>django-admin.py compilemessages --settings=path.to.settings</literal> to make the compiler process all the directories in your <literal>LOCALE_PATHS</literal> setting. 您亦可运行<literal>django-admin.py compilemessages --settings=path.to.settings</literal> 来使编译器处理所有存在于您 <literal>LOCALE_PATHS</literal> 设置中的目录。 翻译
442#翻译 Application message files are a bit complicated to discover they need the <literal>LocaleMiddleware</literal> . If you don't use the middleware, only the Django message files and project message files will be processed. 应用程序信息文件稍微难以发现——因为它们需要 <literal>LocaleMiddle</literal> 。如果不使用中间件,Django只会处理Django的信息文件和项目的信息文件。 翻译
444#翻译 Finally, you should give some thought to the structure of your translation files. 最后,需要考虑一下翻译文件的结构。 翻译
445#翻译 If your applications need to be delivered to other users and will be used in other projects, you might want to use app-specific translations. 若应用程序要发放给其他用户,应用到其它项目中,可能需要使用应用程序相关的翻译。 翻译
446#翻译 But using app-specific translations and project translations could produce weird problems with <literal>makemessages</literal> : <literal>makemessages</literal> will traverse all directories below the current path and so might put message IDs into the project message file that are already in application message files. 但是,使用应用程序相关的翻译和项目翻译在使用 <literal>make-messages</literal> 时会产生古怪的问题。它会遍历当前路径下所有的文件夹,这样可能会把应用消息文件里存在的消息ID重复放入项目消息文件中。 翻译
448#翻译 The easiest way out is to store applications that are not part of the project (and so carry their own translations) outside the project tree. 最容易的解决方法就是将不属于项目的应用程序(因此附带着本身的翻译)存储在项目树之外。 翻译
449#翻译 That way, <literal>django-admin.py makemessages</literal> on the project level will only translate strings that are connected to your explicit project and not strings that are distributed independently. 这样做的话,项目级的 <literal>make-messages</literal> 将只会翻译与项目精确相关的,而不包括那些独立发布的应用程序中的字符串。 翻译
451#翻译 The <literal>set_language</literal> Redirect View <literal>set_language</literal> 重定向视图 翻译
453#翻译 As a convenience, Django comes with a view, <literal>django.views.i18n.set_language</literal> , that sets a user's language preference and redirects back to the previous page. 方便起见,Django自带了一个 <literal>django.views.i18n.set_language</literal> 视图,作用是设置用户语言偏好并重定向返回到前一页面。 翻译
455#翻译 Activate this view by adding the following line to your URLconf: 在URLconf中加入下面这行代码来激活这个视图: 翻译
458#翻译 (Note that this example makes the view available at <literal>/i18n/setlang/</literal> .) (注意这个例子使得这个视图在 <literal>/i18n/setlang/</literal> 中有效。) 翻译
460#翻译 The view expects to be called via the <literal>POST</literal> method, with a <literal>language</literal> parameter set in request. 这个视图是通过 <literal>GET</literal> 方法调用的,在请求中包含了 <literal>language</literal> 参数。 翻译
461#翻译 If session support is enabled, the view saves the language choice in the user's session. 如果session已启用,这个视图会将语言选择保存在用户的session中。 翻译
462#翻译 Otherwise, it saves the language choice in a cookie that is by default named <literal>django_language</literal> . (The name can be changed through the <literal>LANGUAGE_COOKIE_NAME</literal> setting.) 否则,它会以缺省名<literal>django_language</literal>在cookie中保存这个语言选择。(这个名字可以通过<literal>LANGUAGE_COOKIE_NAME</literal>设置来改变) 翻译
464#翻译 After setting the language choice, Django redirects the user, following this algorithm: 保存了语言选择后,Django根据以下算法来重定向页面: 翻译
466#翻译 Django looks for a <literal>next</literal> parameter in the <literal>POST</literal> data. Django 在 <literal>POST</literal> 数据中寻找一个 <literal>下一个</literal> 参数。 翻译
468#翻译 If that doesn't exist, or is empty, Django tries the URL in the <literal>Referrer</literal> header. 如果 <literal>next</literal> 参数不存在或为空,Django尝试重定向页面为HTML头部信息中 <literal>Referer</literal> 的值。 翻译
470#翻译 If that's empty say, if a user's browser suppresses that header then the user will be redirected to <literal>/</literal> (the site root) as a fallback. 如果 <literal>Referer</literal> 也是空的,即该用户的浏览器并不发送 <literal>Referer</literal> 头信息,则页面将重定向到 <literal>/</literal> (页面根目录)。 翻译
472#翻译 Here's example HTML template code: 这是一个HTML模板代码的例子: 翻译
475#翻译 Translations and JavaScript 翻译与JavaScript 翻译
477#翻译 Adding translations to JavaScript poses some problems: 将翻译添加到JavaScript会引起一些问题: 翻译
479#翻译 JavaScript code doesn't have access to a <literal>gettext</literal> implementation. JavaScript代码无法访问一个 <literal>gettext</literal> 的实现。 翻译
481#翻译 JavaScript code doesn't have access to .po or .mo files; they need to be delivered by the server. JavaScript 代码并不访问 .po或 .mo 文件;它们需要由服务器分发。 翻译
483#翻译 The translation catalogs for JavaScript should be kept as small as possible. 针对JavaScript的翻译目录应尽量小。 翻译
485#翻译 Django provides an integrated solution for these problems: Django已经提供了一个集成解决方案: 翻译
486#翻译 It passes the translations into JavaScript, so you can call <literal>gettext</literal> , etc., from within JavaScript. 它会将翻译传递给JavaScript,因此就可以在JavaScript中调用 <literal>gettext</literal> 之类的代码。 翻译
488#翻译 The <literal>javascript_catalog</literal> View <literal>javascript_catalog</literal>视图 翻译
490#翻译 The main solution to these problems is the <literal>javascript_catalog</literal> view, which sends out a JavaScript code library with functions that mimic the <literal>gettext</literal> interface, plus an array of translation strings. 这些问题的主要解决方案就是 <literal>javascript_catalog</literal> 视图。该视图生成一个JavaScript代码库,包括模仿 gettext 接口的函数,和翻译字符串的数组。 翻译
491#翻译 Those translation strings are taken from the application, project or Django core, according to what you specify in either the info_dict or the URL. 这些翻译字符串来自于你在info_dict或URl中指定的应用,工程或Django内核。 翻译
493#翻译 You hook it up like this: 像这样使用: 翻译
496#翻译 Each string in <literal>packages</literal> should be in Python dotted-package syntax (the same format as the strings in <literal>INSTALLED_APPS</literal> ) and should refer to a package that contains a <literal>locale</literal> directory. <literal>packages</literal> 里的每个字符串应该是Python中的点分割的包的表达式形式(和在 <literal>INSTALLED_APPS</literal> 中的字符串相同的格式),而且应指向包含 <literal>locale</literal> 目录的包。 翻译
497#翻译 If you specify multiple packages, all those catalogs are merged into one catalog. 如果指定了多个包,所有的目录会合并成一个目录。 翻译
498#翻译 This is useful if you have JavaScript that uses strings from different applications. 如果有用到来自不同应用程序的字符串的JavaScript,这种机制会很有帮助。 翻译
500#翻译 You can make the view dynamic by putting the packages into the URL pattern: 你可以动态使用视图,将包放在urlpatterns里: 翻译
503#翻译 With this, you specify the packages as a list of package names delimited by +' signs in the URL. 这样的话,就可以在URL中指定由加号( <literal>+</literal> )分隔包名的包了。 翻译
504#翻译 This is especially useful if your pages use code from different apps and this changes often and you don't want to pull in one big catalog file. 如果页面使用来自不同应用程序的代码,且经常改变,还不想将其放在一个大的目录文件中,对于这些情况,显然这是很有用的。 翻译
505#翻译 As a security measure, these values can only be either <literal>django.conf</literal> or any package from the <literal>INSTALLED_APPS</literal> setting. 出于安全考虑,这些值只能是 <literal>django.conf</literal> 或 <literal>INSTALLED_APPS</literal> 设置中的包。 翻译
507#翻译 Using the JavaScript Translation Catalog 使用JavaScript翻译目录 翻译
509#翻译 To use the catalog, just pull in the dynamically generated script like this: 要使用这个目录,只要这样引入动态生成的脚本: 翻译
512#翻译 This is how the admin fetches the translation catalog from the server. 这就是管理页面如何从服务器获取翻译目录。 翻译
513#翻译 When the catalog is loaded, your JavaScript code can use the standard <literal>gettext</literal> interface to access it: 当目录加载后,JavaScript代码就能通过标准的 <literal>gettext</literal> 接口进行访问: 翻译
516#翻译 There is also an <literal>ngettext</literal> interface: 也有一个<literal>ngettext</literal>接口: 翻译
519#翻译 and even a string interpolation function: 甚至有一个字符串插入函数: 翻译
522#翻译 The interpolation syntax is borrowed from Python, so the <literal>interpolate</literal> function supports both positional and named interpolation: 插入句法是从Python借用的,所以<literal>interpolate</literal> 函数对位置和命名插入均提供支持: 翻译
524#翻译 Positional interpolation: 位置插入 翻译
525#翻译 <literal>obj</literal> contains a JavaScript Array object whose elements values are then sequentially interpolated in their corresponding <literal>fmt</literal> placeholders in the same order they appear. <literal>obj</literal>包括一个JavaScript数组对象,元素值在它们对应于<literal>fmt</literal>的占位符中以它们出现的相同次序顺序插值 。 翻译
526#翻译 For example: 例如: 翻译
529#翻译 Named interpolation: 命名插入 翻译
530#翻译 This mode is selected by passing the optional boolean <literal>named</literal> parameter as true. 通过传送为真(TRUE)的布尔参数<literal>name</literal>来选择这个模式。 翻译
531#翻译 <literal>obj</literal> contains a JavaScript object or associative array. <literal>obj</literal>包括一个 JavaScript 对象或相关数组。 翻译
532#翻译 For example: 例如: 翻译
535#翻译 You shouldn't go over the top with string interpolation, though: 但是,你不应重复编写字符串插值: 翻译
536#翻译 this is still JavaScript, so the code has to make repeated regular-expression substitutions. 这还是JavaScript,所以这段代码不得不重复做正则表达式置换。 翻译
537#翻译 This isn't as fast as string interpolation in Python, so keep it to those cases where you really need it (for example, in conjunction with <literal>ngettext</literal> to produce proper pluralizations). 它不会和Python中的字符串插补一样快,因此只有真正需要的时候再使用它(例如,利用 <literal>ngettext</literal> 生成合适的复数形式)。 翻译
539#翻译 Creating JavaScript Translation Catalogs 创建JavaScript翻译目录 翻译
541#翻译 You create and update the translation catalogs the same way as the other 你可以创建和更改翻译目录,就像其他 翻译
543#翻译 Django translation catalogs with the django-admin.py makemessages tool. Django翻译目录一样,使用django-admin.py makemessages 工具。 翻译
544#翻译 The only difference is you need to provide a <literal>-d djangojs</literal> parameter, like this: 唯一的差别是需要提供一个 <literal>-d djangojs</literal> 的参数,就像这样: 翻译
547#翻译 This would create or update the translation catalog for JavaScript for German. 这样来创建或更新JavaScript的德语翻译目录。 翻译
548#翻译 After updating translation catalogs, just run <literal>django-admin.py compilemessages</literal> the same way as you do with normal Django translation catalogs. 和普通的Django翻译目录一样,更新了翻译目录后,运行 <literal>compile-messages.py</literal> 即可。 翻译
550#翻译 Notes for Users Familiar with <literal>gettext</literal> 熟悉 <literal>gettext</literal> 用户的注意事项 翻译
552#翻译 If you know <literal>gettext</literal> , you might note these specialties in the way Django does translation: 如果你了解 <literal>gettext</literal> ,你可能会发现Django进行翻译时的一些特殊的东西: 翻译
554#翻译 The string domain is <literal>django</literal> or <literal>djangojs</literal> . This string domain is used to differentiate between different programs that store their data in a common message-file library (usually <literal>/usr/share/locale/</literal> ). The <literal>django</literal> domain is used for python and template translation strings and is loaded into the global translation catalogs. 字符串域为 <literal>django</literal> 或 <literal>djangojs</literal> 。字符串域是用来区别将数据存储在同一信息文件库(一般是 <literal>/usr/share/locale/</literal> )的不同程序。django 域是为Python和模板翻译字符串服务的,被加载到全局翻译目录。 翻译
555#翻译 The <literal>djangojs</literal> domain is only used for JavaScript translation catalogs to make sure that those are as small as possible. <literal>djangojs</literal> 域只是用来尽可能缩小JavaScript翻译的体积。 翻译
557#翻译 Django doesn't use <literal>xgettext</literal> alone. Django不单独使用 <literal>xgettext</literal> , 翻译
558#翻译 It uses Python wrappers around <literal>xgettext</literal> and <literal>msgfmt</literal> . This is mostly for convenience. 而是经过Python包装后的<literal>xgettext</literal>和msgfmt。这主要是为了方便。 翻译
560#翻译 <literal>gettext</literal> on Windows Windows下的<literal>gettext</literal>  翻译
562#翻译 This is only needed for people who either want to extract message IDs or compile message files (<literal>.po</literal> ). Translation work itself just involves editing existing files of this type, but if you want to create your own message files, or want to test or compile a changed message file, you will need the <literal>gettext</literal> utilities: 对于那些要提取消息或编译消息文件的人们来说,需要的只有这么多。翻译工作本身仅仅包含编辑这个类型的现存文件,但如果你要创建你自己的消息文件,或想要测试或编译一个更改过的消息文件,你将需要这个<literal>gettext</literal>公用程序。 翻译
564#翻译 Download the following zip files from <reference name="http://sourceforge.net/projects/gettext" refuri="http://sourceforge.net/projects/gettext">http://sourceforge.net/projects/gettext</reference> 从<reference name="http://sourceforge.net/projects/gettext" refuri="http://sourceforge.net/projects/gettext">http://sourceforge.net/projects/gettext</reference>下载以下zip文件 翻译
566#翻译 <literal>gettext-runtime-X.bin.woe32.zip</literal> <literal>gettext-runtime-X.bin.woe32.zip</literal> 翻译
568#翻译 <literal>gettext-tools-X.bin.woe32.zip</literal> <literal>gettext-tools-X.bin.woe32.zip</literal> 翻译
570#翻译 <literal>libiconv-X.bin.woe32.zip</literal> <literal>libiconv-X.bin.woe32.zip</literal> 翻译
572#翻译 Extract the 3 files in the same folder (i.e. 在同一文件夹下展开这3个文件。(也就是 翻译
573#翻译 <literal>C:\Program Files\gettext-utils</literal> ) <literal>C:\Program Files\gettext-utils</literal> ) 翻译
575#翻译 Update the system PATH: 更新系统路径: 翻译
577#翻译 <literal>Control Panel > System > Advanced > Environment Variables</literal> <literal>控制面板 > 系统> 高级 > 环境变量</literal> 翻译
579#翻译 In the <literal>System variables</literal> list, click <literal>Path</literal> , click <literal>Edit</literal> 在<literal>系统变量</literal>列表中,点击<literal>Path</literal>,点击<literal>Edit</literal> 翻译
581#翻译 Add <literal>;C:\Program Files\gettext-utils\bin</literal> at the end of the <literal>Variable value</literal> field 把<literal>;C:\Program Files\gettext-utils\bin</literal>加到<literal>变量值</literal>字段的末尾。 翻译
583#翻译 You may also use <literal>gettext</literal> binaries you have obtained elsewhere, so long as the <literal>xgettext --version</literal> command works properly. 只要<literal>xgettext --version</literal>命令正常工作,你亦可使用从别处获得的<literal>gettext</literal>的二进制代码。 翻译
584#翻译 Some version 0.14.4 binaries have been found to not support this command. 有些版本的0.14.4二进制代码被发现不支持这个命令。 翻译
585#翻译 Do not attempt to use Django translation utilities with a <literal>gettext</literal> package if the command <literal>xgettext --version</literal> entered at a Windows command prompt causes a popup window saying xgettext.exe has generated errors and will be closed by Windows. 不要试图与Django公用程序一起使用一个<literal>gettext</literal>。在一个windows命令提示窗口输入命令 <literal> xgettext --version </literal>将导致出现一个错误弹出窗口---“xgettext.exe产生错误并且将被windows关闭”。 翻译
587#翻译 What's Next? 下一章 翻译
589#翻译 The <reference name="final chapter" refuri="../chapter20/">final chapter</reference> focuses on security how you can help secure your sites and your users from malicious attackers. <reference name="final chapter" refuri="../chapter20/">未章</reference> 将关注于安全,如何帮助你的用户和网站远离恶意软件的攻击。 翻译