私はこれに応じてASP.NETコアに局在を実装しました:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localizationASP.NETコアで使用するのと同じ `.resx`ファイルおよび.NETのコアコンソールプロジェクト
私は、共通のライブラリプロジェクト(MyProject.Common
)どこを持っています私はリソースファイルを保持しています(複数のASP.NET Coreアプリケーションで使用されているため)。
だから私はMyProject.Common\Resources\Localization\SharedResources.sv.resx
下.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>
をそして、私はこのようになりますMyProject.Common\Localization\SharedResources.cs
下の空の.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];
}
}
}
}
私はこのような局在を設定するASP.NETコアプロジェクトごとに
Startup.cs
で
_localizer["English string"];
:
そして、それが使用されているが、これを(リクエストの文化は、スウェーデンの場合はスウェーデン語の翻訳を返します)言ってい
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
を使用しようとすると、私は'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
ファイルを使用するにはどうすればよいですか?
あなたがパーティーに遅れてうれしいです;)。私はそれをやろうとしているが、成功していない。私は2つのResXファイルを作成しました.1つは他の文化特有の中立ですが、私はいつも中立値を得ています。それはあなたによく知られているだろうか? 詳細はこちら:https://github.com/dotnet/core/issues/1036 –