gpt4 book ai didi

c# - 在 ASP.NET Core 和 .NET Core 控制台项目中使用相同的 `.resx` 文件

转载 作者:太空狗 更新时间:2023-10-29 23:43:34 25 4
gpt4 key购买 nike

我已经根据以下内容在 ASP.NET Core 中实现了本地化:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization

我有一个公共(public)库项目 ( MyProject.Common ),我在其中保存资源文件(因为它被多个 ASP.NET Core 应用程序使用)。

所以我有一个 .resx 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 下的文件看起来像这样:

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我用来获取翻译后的字符串:

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];
}
}
}
}

它是这样使用的(如果请求的文化是瑞典语,将返回瑞典语翻译):

_localizer["English string"];

Startup.cs对于每个 ASP.NET Core 项目,我都像这样配置本地化:

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通过依赖注入(inject),我得到:'Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' while attempting to activate 'Microsoft.Extensions.Localization.ResourceManagerStringLocalizerFactory .所以这是因为 Microsoft.Extensions.Localization依赖于 ASP.NET Core,它在我的控制台项目中不存在。

我怎样才能使用相同的 .resx我的控制台项目中的文件?

最佳答案

我知道我来晚了一点,但您应该可以直接访问您的翻译。当然在控制台应用程序中没有请求本地化这样的概念,所以你需要手动处理文化查找/存储/任何东西,但是一旦你知道文化就可以像这样访问你的翻译(不需要依赖注入(inject)!):

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);

您可能会想出一种处理语言代码和资源标记的好方法,但这应该足以翻译公共(public)资源文件。

注意:这适用于 .Net Core 2.0,尚未在其他版本中测试。

关于c# - 在 ASP.NET Core 和 .NET Core 控制台项目中使用相同的 `.resx` 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42928486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com