Skip to content

使用本地化提供程序-Using the Localization Providers

Orchard uses a simple API to allow for localization that takes the default language string (en-us) as its input (that string will be used as the key into the localization data) and returns the most accurate localized version available for the current culture.

Orchard使用一个简单的API来允许本地化,它将默认语言字符串(en-us)作为其输入(该字符串将用作本地化数据的关键字),并返回当前文化可用的最准确的本地化版本。

Using T() with the Razor View Engine (.cshtml)

在Razor View Engine(.cshtml)中使用T()

Simple usage - translated string is returned and output

简单用法 - 返回并输出翻译后的字符串

@T("This was a triumph!")

Use this for simple strings.

用于简单的字符串。

Formatting user data

格式化用户数据

Sometimes, data needs to be injected into a localizable string.

有时,需要将数据注入可本地化的字符串中。

Do not use concatenation as the position of the data might vary per language:

不要使用连接,因为数据的位置可能因语言而异:

// BAD:

@T("You have ") + Model.SmsCredits + @T(" credits left.")

Instead, use a parameterized format string:

而是使用参数化格式字符串:

// GOOD:

@T("You have {0} credits left", Model.SmsCredits)

Encoding data

编码数据

Please note that the arguments will be encoded before being added. For example, if noteText in the following example contains <b>huge</b> success:

请注意,参数将在添加之前进行编码。例如,如果以下示例中的noteText包含<b> huge </ b> success

@T("I'm writing a note here: {0}.", noteText)

Then the output in the default culture will be with the markup visible to the end user:

然后默认区域中的输出将使最终用户可以看到标记:

I'm writing a note here: <b>huge</b> success.

我在这里写一个注释:&lt; b&gt; huge&lt; / b&gt;成功。

This is what you want in 99% of cases. This automatic encoding is protecting you from hackers using injection attacks.

在99%的情况下,这是你想要的。这种自动编码可以保护您免受使用注入攻击的黑客攻击。

In the rare cases where you absolutely know what you're doing and you want the unencoded string to be injected, you can do the following:

在极少数情况下,您绝对知道自己在做什么,并且希望注入未编码的字符串,您可以执行以下操作:

@T("I'm writing a note here: {0}.", new HtmlString(noteText))

This will result in:

这将导致:

I'm writing a note here: huge success.

我在这里写一个注释:巨大的成功。

Note

!注意

any object of a type implementing `IHtmlString` will be injected unencoded as we assume it to already be properly encoded.

This is the trick you are using when writing `new HtmlString(noteText)`.

For example, if you do the following:

例如,如果您执行以下操作:

@T("{0}: We do what we must because {1}",

    Html.ItemDisplayLink(apertureScienceContentItem),

    justification)

Then the action link will not be encoded and will work as expected, while the justification string will be encoded.

然后,动作链接将不会被编码并将按预期工作,而对齐字符串将被编码。

It should also be noted that the format string itself is considered safe as it is provided by the module author, so the following will work as expected:

还应注意,格式字符串本身被认为是安全的,因为它是由模块作者提供的,因此以下内容将按预期工作:

@T("It's <em>hard</em> to overstate my <strong>{0}</strong>",

    emotion)

If emotion contains "<satisfaction>", the resulting string will be "It's hard to overstate my <satisfaction>".

如果情感包含“&lt; satisfaction&gt;”,则结果字符串将是“它很难夸大我的&lt; satisfaction&gt; ”。

Injecting non-string values

注入非字符串值

Basic value types are not html encoded before formatting, and the current culture will be used to format them:

在格式化之前,基本值类型不是html编码的,并且当前文化将用于格式化它们:

@T("when {0} qty {1:#,##0.00} unit price {2:C}", _clock.UtcNow, 5.782, 87)

Pluralization

多元化

Pluralization of resource strings (such as {0} comment or {0} comments) can be tricky as the rules for pluralization or even how many strings you need for all cases wildly varies across languages. While Orchard does not yet implement all possible cases, the API is ready to support them in the future.

资源字符串的多元化(例如“{0}注释”或“{0}注释”)可能很棘手,因为复数规则甚至所有情况下所需的字符串数量会因语言而异。虽然Orchard尚未实现所有可能的情况,但API已准备好在将来支持它们。

If a string needs to be pluralized, provide two strings for the default language and put the pluralization parameter first:

如果字符串需要复数,请为默认语言提供两个字符串,并将复数参数放在第一位:

@T.Plural("1 Comment", "{0} Comments", commentCount)

@T.Plural("Deleted 1 item of type {1}", "Deleted {0} items of type {1}",

    deleteCount, contentType)

Use 1 literally in the singular string to provide better context to translators (like in the example above).

在单数字符串中使用“1”来为翻译提供更好的上下文(如上例所示)。

The pluralization parameter must be an integer.

复数参数必须是整数。

Do not use custom logic in the views to decide between strings, as that would put in the view logic that may vary by culture.

不要在视图中使用自定义逻辑来决定字符串,因为这会放在视图逻辑中,这可能因文化而异。

Using T() via the code

通过代码使用T()

Setting it up with dependency injection

使用依赖注入进行设置

You can use the T() localization helper within your code such as drivers, services and other classes.

您可以在代码中使用T()本地化帮助程序,例如驱动程序,服务和其他类。

By adding a public property to your class Orchard will detect it and use the property injection features of Autofac to configure your method with current Localizer.

通过向您的类添加公共属性,Orchard将检测它并使用Autofac的属性注入功能来使用当前的“Localizer”配置您的方法。

The process is as follows:

过程如下:

  1. Add a public property to your class:

1.向您的班级添加公共财产:

     public Localizer T { get; set; }

 You can technically use any variable name for the `T` property but the convention is to use `T`.
  1. In your constructor, assign a default value to the T property:

1.在构造函数中,为T属性指定一个默认值:

     protected YourClassName() {

       T = NullLocalizer.Instance;

     }



  This is to prevent the localizer from ending up without a default localizer before Autofac has configured it for you.

Inherit IDependency classes from Component to get T() automatically

从Component继承IDependency类以自动获取T()

Orchard provides an abstract class, Component which is defined in the Orchard.Framework project within IDependency.cs:

Orchard提供了一个抽象类,Component,它在IDependency.cs中的Orchard.Framework项目中定义:

public abstract class Component : IDependency {

    protected Component() {

        Logger = NullLogger.Instance;

        T = NullLocalizer.Instance;

    }

    public ILogger Logger { get; set; }

    public Localizer T { get; set; }

}

If your class implements the IDependency interface then you can inherit from Component instead. This way your class will have the T() method automatically declared and initialized.

如果你的类实现了IDependency接口,那么你可以继承Component。这样你的类就会自动声明和初始化T()方法。

Using the T() property when it's configured

在配置时使用T()属性

You can review the examples for the Razor examples above to use it.

您可以查看上面Razor示例的示例以使用它。

The only difference is that when you're using the localizer via code you don't need to use the Razor @ symbol to signify the start of a code block.

唯一的区别是当你通过代码使用定位器时,你不需要使用Razor@符号来表示代码块的开始。

For example, in the Razor section it lists this example:

例如,在Razor部分中列出了此示例:

@T("You have {0} credits left", Model.SmsCredits)

In code that would look like this:

在代码中看起来像这样:

T("You have {0} credits left", Model.SmsCredits)

Using T() with the ASPX View Engine (.aspx)

将T()与ASPX视图引擎(.aspx)一起使用

Simple usage - translated string is returned and output

简单用法 - 返回并输出翻译后的字符串

<%: T("This was a triumph!") %>

Use this for simple strings.

用于简单的字符串。

Formatting user data

格式化用户数据

Sometimes, data needs to be injected into a localizable string.

有时,需要将数据注入可本地化的字符串中。

Do not use concatenation as the position of the data might vary per language:

不要使用连接,因为数据的位置可能因语言而异:

// BAD:

<%: T("You have ") + Model.SmsCredits + T(" credits left.") %>

Instead, use a parameterized format string:

而是使用参数化格式字符串:

// GOOD:

<%: T("You have {0} credits left.", Model.SmsCredits) %>

Encoding data

编码数据

Please note that the arguments will be encoded before being added.

请注意,参数将在添加之前进行编码。

For example, if noteText in the following example contains &lt;b&gt;huge&lt;/b&gt; success:

例如,如果以下示例中的noteText包含&lt; b&gt; huge&lt; / b&gt; success

<%: T("I'm writing a note here: {0}.", noteText) %>

Then the output in the default culture will be: "I'm writing a note here: <b>huge</b> success." with the markup visible to the end user.

然后默认文化中的输出将是:“我在这里写一个注释:&lt; b&gt;巨大&lt; / b&gt;成功。”标记对最终用户可见。

This is what you want in 99% of cases. This automatic encoding is protecting you from nasty injection attacks.

在99%的情况下,这是你想要的。这种自动编码可以保护您免受恶意注入攻击。

In the rare cases where you absolutely know what you're doing and you want the unencoded string to be injected, you can do the following:

在极少数情况下,您绝对知道自己在做什么,并且希望注入未编码的字符串,您可以执行以下操作:

<%: T("I'm writing a note here: {0}.", new HtmlString(noteText)) %>

This will result in "I'm writing a note here: huge success."

这将导致“我在这里写一个注释:巨大的成功。”

Note

!注意

Any object of a type implementing `IHtmlString` will be injected unencoded as we assume it to already be properly encoded. This is the trick you are using when writing `new HtmlString(noteText)`.

For example, if you do the following:

例如,如果您执行以下操作:

<%: T("{0}: We do what we must because {1}",

    Html.ItemDisplayLink(apertureScienceContentItem),

    justification) %>

Then the action link will not be encoded and will work as expected, while the justification string will be encoded.

然后,动作链接将不会被编码并将按预期工作,而对齐字符串将被编码。

It should also be noted that the format string itself is considered safe as it is provided by the module author, so the following will work as expected:

还应注意,格式字符串本身被认为是安全的,因为它是由模块作者提供的,因此以下内容将按预期工作:

<%: T("It's <em>hard</em> to overstate my <strong>{0}</strong>",

    emotion) %>

If emotion contains "<satisfaction>", the resulting string will be:

如果情感包含“&lt; satisfaction&gt;”,则结果字符串将为:

It's hard to overstate my <satisfaction>

很难夸大我的&lt; satisfaction&gt;

Injecting non-string values

注入非字符串值

Basic value types are not HTML-encoded before formatting, and the current culture will be used to format them:

格式化之前,基本值类型不是HTML编码的,当前文化将用于格式化它们:

<%: T("when {0} qty {1:#,##0.00} unit price {2:C}", _clock.UtcNow, 5.782, 87 ) %>

Pluralization

多元化

Pluralization of resource strings (such as {0} comment or {0} comments) can be tricky as the rules for pluralization or even how many strings you need for all cases varies widely across languages.

资源字符串的多元化(例如“{0}注释”或“{0}注释”)可能很棘手,因为复数规则甚至所有情况下所需的字符串数量因语言而异。

While Orchard does not yet implement all possible cases, the API is ready to support them in the future.

虽然Orchard尚未实现所有可能的情况,但API已准备好在将来支持它们。

If a string needs to be pluralized, provide two strings for the default language and put the pluralization parameter first:

如果字符串需要复数,请为默认语言提供两个字符串,并将复数参数放在第一位:

<%: T.Plural("1 Comment", "{0} Comments", commentCount) %>

<%: T.Plural("Deleted 1 item of type {1}", "Deleted {0} items of type {1}",

    deleteCount, contentType) %>

Use 1 literally in the singular string to provide better context to translators (like in the example above).

在单数字符串中使用“1”来为翻译提供更好的上下文(如上例所示)。

The pluralization parameter must be an integer.

复数参数必须是整数。

Do not use custom logic in the views to decide between strings, as that would put logic that may vary by culture in the view.

不要在视图中使用自定义逻辑来决定字符串,因为这会使逻辑在视图中因文化而异。

<%= %> vs. <%: %>

&lt;%=%&gt;与&lt;%:%&gt;

<%: %> is to be used in all cases because it handles encoding automatically. Never use <%= %>. If you are sure you need unencoded strings injected, still use <%: %> with an HtmlString.

&lt;%:%&gt;将在所有情况下使用,因为它自动处理编码。切勿使用&lt;%=%&gt;。如果您确定需要注入未编码的字符串,仍然使用&lt;%:%&gt;用'HtmlString`。