Skip to content

Localization (OrchardCore.Localization)

本地化(OrchardCore.Localization)

This module provides the infrastructure necessary to support the PO (Portable Object) localization file format.

此模块提供支持PO(可移植对象)本地化文件格式所需的基础结构。

It also supports plural forms.

它还支持复数形式。

PO files locations

PO文件位置

PO files are found via the following steps:

PO文件可通过以下步骤找到:

  • For each module and theme all files matching [ModuleLocation]/App_Data/Localization/[CultureName].po

  • 对于每个模块和主题,所有文件都匹配[ModuleLocation] / App_Data / Localization / [CultureName] .po

  • Then all files matching /App_Data/Localization/[CultureName].po

  • 然后所有文件匹配/ App_Data / Localization / [CultureName] .po

  • For each tenant all files matching /App_Data/Sites/[TenantName]/Localization/[CultureName].po

  • 对于每个租户所有匹配/ App_Data / Sites / [TenantName] / Localization / [CultureName] .po的文件

File format

文件格式

This article explains how PO files are organized, including plural forms.

本文介绍了PO文件的组织方式,包括复数形式。

https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html

https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html

Translation contexts

翻译背景

To prevent different PO files entries from overriding each other, entries define a context for each translation string.

为了防止不同的PO文件条目相互覆盖,条目为每个翻译字符串定义上下文。

For instance two views could use the string named Hello but they might have different translation. It's then necessary to

例如,两个视图可以使用名为“Hello”的字符串,但它们可能具有不同的转换。然后有必要

provide two entries and specify which context is associated with each translation. In this case each view name is a context.

提供两个条目并指定哪个_context_与每个翻译相关联。在这种情况下,每个视图名称都是上下文。

From a View

从一个视图

The context string must match the view location up to the module folder.

上下文字符串必须与视图位置匹配,直到模块文件夹。

View

视图

Assuming the view's path is TheAdmin\Views\Layout.cshtml.

假设视图的路径是TheAdmin \\ Views \\ Layout.cshtml

PO File

PO文件


msgctxt "TheAdmin.Views.Layout"

 <font color=#0099ff size=4 face="黑体">msgctxt“TheAdmin.Views.Layout”</font> 


msgid "Hello"

 <font color=#0099ff size=4 face="黑体">msgstr“你好”</font> 


msgstr "Bonjour"

 <font color=#0099ff size=4 face="黑体">msgstr“Bonjour”</font> 


From a Service

来自服务

The context string must match the full name of the type the localizer is injecting in.

上下文字符串必须与本地化程序所注入的类型的全名相匹配。

Source

资源


namespace MyNamespace

 <font color=#0099ff size=4 face="黑体">名称空间MyNamespace</font> 


{

 <font color=#0099ff size=4 face="黑体">{</font> 


    public class MyService : IMyService

    {

        public IStringLocalizer T { get; set; }




 <font color=#0099ff size=4 face="黑体"></font> 


        public MyService(IStringLocalizer<MyService> localizer)

        {

            T = localizer;

        }




 <font color=#0099ff size=4 face="黑体"></font> 


        public void DoSomething()

        {

            Console.WriteLine(T["Hello"]);

        }

    }

}

 <font color=#0099ff size=4 face="黑体">}</font> 


PO file

PO文件


msgctxt "MyNamespace.MyService"

 <font color=#0099ff size=4 face="黑体">msgctxt“MyNamespace.MyService”</font> 


msgid "Hello"

 <font color=#0099ff size=4 face="黑体">msgstr“你好”</font> 


msgstr "Bonjour"

 <font color=#0099ff size=4 face="黑体">msgstr“Bonjour”</font> 


Pluralization

多元化

This module also provides support for pluralization.

该模块还支持多元化。

It is necessary to reference the OrchardCore.Localization.Abstractions package in order to be able to use it.

有必要引用OrchardCore.Localization.Abstractions包以便能够使用它。

Sample PO file

示例PO文件


msgctxt "TheAdmin.Views.Layout"

 <font color=#0099ff size=4 face="黑体">msgctxt“TheAdmin.Views.Layout”</font> 


msgid "1 book"

 <font color=#0099ff size=4 face="黑体">msgstr“1本书”</font> 


msgid_plural "{0} books"

 <font color=#0099ff size=4 face="黑体">msgid_plural“{0}书籍”</font> 


msgstr[0] "[1 livre]"

 <font color=#0099ff size=4 face="黑体">msgstr [0]“[1 livre]”</font> 


msgstr[1] "[{0} livres]"

 <font color=#0099ff size=4 face="黑体">msgstr [1]“[{0} livres]”</font> 


Usage

用法

  • Import the using Microsoft.Extensions.Localization namespace.

  • 导入using Microsoft.Extensions.Localization命名空间。

  • Inject an instance of IStringLocalizer or IViewLocalizer (represented as the T variable in the following example).

  • 注入一个IStringLocalizerIViewLocalizer的实例(在下面的例子中表示为T变量)。


T.Plural(count, "1 book", "{0} books")

 <font color=#0099ff size=4 face="黑体">T.Plural(伯爵,“1本书”,“{0}书”)</font>