0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MiniAPI(十一):ローカリゼーション

Posted at

.NET開発体系では、ローカリゼーション(地域化)の実装には、主にリソースファイル(.resx)が使用されます。ASP.NET Coreでの多言語Cultureは、地域性を示すオブジェクトを指し、UICultureは、実行時にリソースマネージャが地域特有のリソースを検索するために使用する現在のユーザーインターフェースの地域性を示します。
ASP.NET Coreでの実装も、ローカリゼーションサービスを注入し、ミドルウェアを追加することで行われます。例えば以下のようになります:

using Microsoft.Extensions.Localization;
var builder = WebApplication.CreateBuilder(args);
// ローカリゼーションミドルウェアの追加。Resources
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

var app = builder.Build();

// ローカリゼーションリソースの適用
var supportedCultures = new[] { "zh-CN", "ja-JP", "en-US" };
var localizationOptions = new RequestLocalizationOptions()
    .SetDefaultCulture(supportedCultures[0])
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures);
localizationOptions.ApplyCurrentCultureToResponseHeaders = true;
app.UseRequestLocalization(localizationOptions);

// サービスコンテナからStringLocalizerを取得して具体的なローカリゼーションデータを取得
app.MapGet("/demo", (IStringLocalizer<SharedResource> sharedLocalizer) => {
    return sharedLocalizer["ok"].Value;
});

app.Run();

// SharedResource型は実装不要
public class SharedResource
{
}

.resxファイルタイプ
alt 画像

.resx文書の可視化インターフェース
alt 画像

.resxファイルのXMLデータ。説明部分とデータ部分から成り立っています。

<?xml version="1.0" encoding="utf-8"?>
<root>
  <!--     Microsoft ResX Schema 
    Version 2.0
    The primary goals of this format is to allow a simple XML format     that is mostly human readable. The generation and parsing of the     various data types are done through the TypeConverter classes     associated with the data types.
    Example:
    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    ...
    <data name="cancel" xml:space="preserve">
      <value>Cancel</value>
    </data>
    <data name="no" xml:space="preserve">
      <value>No</value>
    </data>
    <data name="ok" xml:space="preserve">
      <value>Yes</value>
    </data>
</root>

(Translated by GPT)

元のリンク:https://mp.weixin.qq.com/s?__biz=MzA3NDM1MzIyMQ==&mid=2247484892&idx=1&sn=fd4bc3a34364ebc98958d466406f104d&chksm=9f005af6a877d3e0c268ef046451c89c783a1edb485b1175f5e3a871aec388968af531e90227&token=877361727&lang=zh_CN#rd

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?