在ASP.NET Core和.NET Core控制台项目中使用相同的`.resx`文件
我已根据此方法在ASP.NET Core中实现了本地化: https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/localization
I've implemented localization in ASP.NET Core according to this: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization
我有一个公共图书馆项目( MyProject.Common
)保存资源文件(因为它被多个ASP.NET Core应用程序使用)。
I have a common library project (MyProject.Common
) where I keep the resource file (since it is used by multiple ASP.NET Core applications).
所以我在 MyProject.Common\Resources\Localization\SharedResources.sv.resx
下有一个 .resx
文件:
So I have a .resx
file under MyProject.Common\Resources\Localization\SharedResources.sv.resx
:
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="English string" xml:space="preserve">
<value>Swedish string</value>
</data>
</root>
我有一个空的 .cs
文件在 MyProject.Common\Localization\SharedResources.cs
下,如下所示:
And I have an empty .cs
file under MyProject.Common\Localization\SharedResources.cs
that looks like this:
namespace MyProject.Common.Localization
{
/// <summary>
/// This is just a placeholder so that we can have all our resources in the same .resx file
/// </summary>
public class SharedResources
{
}
}
我有一个名为 Localizer
的类,可用于获取翻译后的字符串:
I have a class called Localizer
that I use to get translated strings:
using System;
using Microsoft.Extensions.Localization;
namespace MyProject.Common.Localization
{
public class Localizer
{
private readonly IStringLocalizer _localizer;
public Localizer(IStringLocalizer<SharedResources> localizer)
{
_localizer = localizer;
}
public virtual LocalizedString this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name];
}
}
public virtual LocalizedString this[string name, params object[] arguments]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name, arguments];
}
}
}
}
就像这样使用(如果请求的语言是瑞典语,将返回瑞典语翻译):
And it is used liked this (will return swedish translation if culture of the request is swedish):
_localizer["English string"];
在 Startup.cs
中,每个ASP.NET Core项目我按如下方式配置本地化:
In Startup.cs
for each of the ASP.NET Core projects I configure localization like this:
services
// Add the localization services to the services container
.AddLocalization(options => options.ResourcesPath = "Resources")
// Configure supported cultures and localization options
.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("sv")
};
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
});
我想弄清楚如何在(.NET Core)控制台应用程序中重用它吗?如果尝试通过依赖项注入使用自定义的 Localizer
,则会得到:'无法解析类型为'Microsoft.AspNetCore.Hosting.IHostingEnvironment的服务'尝试激活 Microsoft.Extensions.Localization.ResourceManagerStringLocalizerFactory
时。因此,这是因为 Microsoft.Extensions.Localization
依赖于ASP.NET Core,这在我的控制台项目中不存在。
I'm trying to figure out how to reuse this in a (.NET Core) console application? If I try to use my custom Localizer
via dependency injection, I get: 'Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' while attempting to activate 'Microsoft.Extensions.Localization.ResourceManagerStringLocalizerFactory
. So this is because Microsoft.Extensions.Localization
is dependent on ASP.NET Core, which doesn't exist in my console project.
如何在控制台项目中使用相同的 .resx
文件?
How can I use the same .resx
file in my console project?
我知道我在这里参加聚会有点晚了,但是您应该可以直接访问您的翻译。当然,在控制台应用程序中没有请求本地化这样的概念,因此您将需要手动处理区域性查找/存储/任何操作,但是一旦您了解区域性,就可以像这样访问您的翻译(无需依赖注入) !):
I know I'm a bit late to the party here but you should be able to access your translations directly. Of course in a console app there is no such concept as request localization, so you'll need to manually handle culture lookup/storage/whatever, but once you know the culture it is possible to access your translations like this (no dependency injection required!):
var german = new System.Globalization.CultureInfo("de-DE");
var english = new System.Globalization.CultureInfo("en-AU");
// result "Guten tag"
var greeting1 = Shared.Resources.Lang.ResourceManager.GetString("GREETING", german);
// result "Gidday mate"
var greeting2 = Shared.Resources.Lang.ResourceManager.GetString("GREETING", english);
您可能会想出一种处理语言代码和资源令牌的好方法,但这应该足够了来翻译公共资源文件。
You can probably figure out a nice way to handle language codes and resource tokens, but this should be enough to do your translations common resource files.
注意:这在.Net Core 2.0中有效,未在其他版本中进行过测试。
NOTE: this works in .Net Core 2.0, haven't tested in other versions.