Yamloc 1.0.0

dotnet add package Yamloc --version 1.0.0
                    
NuGet\Install-Package Yamloc -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Yamloc" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Yamloc" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Yamloc" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Yamloc --version 1.0.0
                    
#r "nuget: Yamloc, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Yamloc@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Yamloc&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Yamloc&version=1.0.0
                    
Install as a Cake Tool

Yamloc

Yamloc は、YAML をベースとした軽量な .NET ローカライゼーション(多言語化)ライブラリです。

このライブラリは CheapLoc を参考にして作られました。CheapLoc の「導入が簡単で、専用のローカライゼーション用データベースを必要としない」というコンセプトを引き継ぎつつ、翻訳ファイルの形式として JSON の代わりに、人間が読み書きしやすい YAML を採用しています。 English README is here

特徴

  • シンプルな APILoc.Setup() を呼び出すだけで導入可能
  • YAML ベース — コメントを書きやすく、手編集にも向いたフォーマット
  • アセンブリ単位の管理 — 呼び出し元のアセンブリを自動的に判別し、アセンブリごとにローカライズデータを分離
  • フォールバック対応 — キーが見つからない場合は指定したフォールバック文字列を表示
  • フォーマット引数対応string.Format 互換の引数を使った文字列の組み立てが可能
  • 翻訳者向けコンテキスト — 各エントリに description(使用箇所の説明)を付与可能
  • スレッドセーフSetup() によるデータの差し替えと Localize() の並行呼び出しに対応

なお、本パッケージ (Yamloc) には実行時のローカライズ用 API のみが含まれており、依存ライブラリは YamlDotNet のみです。ビルド時に翻訳対象文字列を抽出するエクスポートツール(ExportLocalizable / ExportLocalizableForAssembly 相当の機能)は、Mono.Cecil に依存する別パッケージ Yamloc.Export で提供される予定です。

インストール

dotnet add package Yamloc

使い方

1. 翻訳データ(YAML)を用意する

message は必須、description は翻訳者向けの補足情報(任意)です。

Greeting:
  message: "こんにちは、世界!"
  description: "アプリ起動時に表示されるあいさつ文"
HelloName:
  message: "こんにちは、{0} さん。"
  description: "ユーザー名を含むあいさつ(引数1つ)"

2. 初期化する

アプリ起動時などに Loc.Setup() / Loc.SetupFromFile() を呼び出します。

using Yamloc;

var allowedLang = new[] { "de", "ja", "fr", "it", "es" };
var currentUiLang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

if (allowedLang.Contains(currentUiLang))
{
    Loc.SetupFromFile($"loc_{currentUiLang}.yaml");
}
else
{
    // 該当する言語データがない場合は、フォールバック文字列のみを使用する
    Loc.SetupWithFallbacks();
}

YAML 文字列を直接渡すことも可能です。

Loc.Setup(File.ReadAllText("loc_ja.yaml"));

3. 文字列をローカライズする

// キーが見つからない場合は "Hello, World." が表示される
var text = Loc.Localize("Greeting", "Hello, World.");

// フォーマット引数付き(T() は Localize() のショートハンド)
var greeting = Loc.T("HelloName", "Hello, {0}.", userName);

API 概要

メソッド 説明
Loc.Setup(string locData) 呼び出し元アセンブリ向けに、YAML 文字列からローカライズデータを設定する
Loc.Setup(string locData, Assembly assembly) 指定したアセンブリ向けに、YAML 文字列からローカライズデータを設定する
Loc.SetupFromFile(string path) 呼び出し元アセンブリ向けに、YAML ファイルからローカライズデータを設定する
Loc.SetupFromFile(string path, Assembly assembly) 指定したアセンブリ向けに、YAML ファイルからローカライズデータを設定する
Loc.SetupWithFallbacks() / Loc.SetupWithFallbacks(Assembly assembly) 空のデータを設定し、常にフォールバック文字列を表示させる
Loc.Localize(string key, string fallBack) キーに対応する文字列を取得(見つからない場合はフォールバック)
Loc.Localize(string key, string fallBack, params object[] args) 上記に加え、string.Format 互換の引数でフォーマットする
Loc.T(string key, string fallBack) / Loc.T(string key, string fallBack, params object[] args) Localize() のショートハンドエイリアス

いずれのメソッドにも、対象アセンブリを明示的に指定できるオーバーロードが用意されています。

翻訳データのスキーマ

各エントリは LocEntry 型に対応します。

public class LocEntry
{
    public string Message { get; set; }     // message: 実際の翻訳文
    public string Description { get; set; }  // description: 翻訳者向けの補足説明
}

ライセンス

Yamloc は MIT License の下で提供されます。

謝辞

本ライブラリは goaaats/CheapLoc を参考に作られました。CheapLoc の作者およびコントリビューターに感謝します。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 76 8/17/2026