.netのタイプセーフi18n
slang.netは、新しい機能(文字列形式など)を備えたDART/Flutterコミュニティのスラングの.NETポートです。
Generated Files General、En、およびDe Lookがどのように表示されるかを表示できます
ライブラリをヌゲットパッケージとしてインストールします。
Install-Package dotnet add package Slang.Net重要なファイルは「.i18n.json」で終了する必要があります。これは、SourceGeneratorが他の追加ファイルの変更を追跡しないようにするために必要です。
i18n/strings_en.i18n.jsonまたはi18n/strings_en-US.i18n.jsonまたはi18n/strings.i18n.json (基本文化用)
{
"screen" : {
"locale1" : " Locale 1 "
}
} i18n/strings_ru.i18n.jsonまたはi18n/strings_ru-RU.i18n.json
{
"screen" : {
"locale1" : " Локаль 1 "
}
} slang.json
{
"base_culture" : " en " // or "en-EN"
}推奨文字列から文化のリストを取得する場合は、文字列をフォーマットするときの適切な機能のために、「en-us」などの国コードを指定することをお勧めします。
< ItemGroup >
< AdditionalFiles Include = " i18n*.i18n.json " />
< AdditionalFiles Include = " slang.json " />
</ ItemGroup > [ Translations ( InputFileName = "strings" ) ]
public partial class Strings ; Strings . SetCulture ( new CultureInfo ( "ru-RU" ) ) ;
Console . WriteLine ( Strings . Instance . Root . Screen . Locale1 ) ; // Локаль 1
Strings . SetCulture ( new CultureInfo ( "en-US" ) ) ;
Console . WriteLine ( Strings . Instance . Root . Screen . Locale1 ) ; // Locale 1または
< MenuItem Header = " {Binding Root.Screen.Locale1, Source={x:Static localization:Strings.Instance}} " />実行時に渡されるパラメーターを指定できます。
{
"Hello" : " Hello {name} "
}生成されたコードは次のようになります。
/// In en, this message translates to:
/// **"Hello {name}"**
public virtual string Hello ( object name ) => $ "Hello { name } " ;パラメーターはデフォルトでobjectとして入力されます。これは、最大の柔軟性を提供するため便利です。
2つの構文オプションを使用してタイプを指定できます。1-シンプル:
{
"greet" : " Hello {name: string}, you are {age: int} years old "
}2-プレースホルダーを使用して、タイプまたはフォーマットの文字列を指定できます(文字列形式を参照)。
{
"greet2" : " Hello {name}, you are {age} years old " ,
"@greet2" : {
"placeholders" : {
"name" : {
"type" : " string "
},
"age" : {
"type" : " int "
}
}
}
}生成されたコードは次のようになります:
/// In ru, this message translates to:
/// **"Hello {name}, you are {age} years old"**
public virtual string Greet ( string name , int age ) => $ "Hello { name } , you are { age } years old" ;翻訳ファイルにコメントを追加できます。
{
"@@locale" : " en " , // fully ignored
"mainScreen" : {
"button" : " Submit " ,
// ignored as translation but rendered as a comment
"@button" : " The submit button shown at the bottom " ,
// or use
"button2" : " Submit " ,
"@button2" : {
"description" : " The submit button shown at the bottom "
}
}
}生成されたコードは次のようになります。
/// The submit button shown at the bottom
///
/// In ru, this message translates to:
/// **"Submit"**
public virtual string Button => "Submit" ;このライブラリは、次のタイプのToString(format)を介した埋め込み形式をサポートしています: int 、 long 、 double 、 decimal 、 float 、 DateTime 、 DateOnly 、 TimeOnly 、 TimeSpan 。他のタイプの場合、形式の文字列はstring.Format(format, locale)に渡されます。
{
"dateExample" : " Date {date} " ,
"@dateExample" : {
"placeholders" : {
"date" : {
"type" : " DateTime " ,
"format" : " dd MMMM HH:mm "
}
}
}
} String s = Strings . Instance . Root . DateExample ( DateTime . Now ) ; // Date 17 October 22:25生成されたコードは次のようになります。
/// In ru, this message translates to:
/// **"Date {date}"**
public virtual string DateExample ( DateTime date )
{
string dateString = date . ToString ( "dd MMMM HH:mm" ) ;
return $ "Date { dateString } " ;
}このライブラリは、ここで定義されている概念を使用します。
一部の言語には、箱から出してサポートがあります。こちらをご覧ください。
複数形はtwo次のキーワードで検出されます: zero 、 one 、 few 、 many 、 other 。
{
"someKey" : {
"apple" : {
"one" : " I have {n} apple. " ,
"other" : " I have {n} apples. "
}
}
} String a = Strings . Instance . Root . SomeKey . Apple ( n : 1 ) ; // I have 1 apple.
String b = Strings . Instance . Root . SomeKey . Apple ( n : 2 ) ; // I have 2 apples. 生成されたコードは次のようになります。
public virtual string Apple ( int n ) => PluralResolvers . Cardinal ( "en" ) ( n ,
one : $ "I have { n } apple." ,
other : $ "I have { n } apples." ) ;検出された複製は、デフォルトでは枢機sです。
順序を指定するには、 (ordinal)修飾子を追加する必要があります。
{
"someKey" : {
"apple(cardinal)" : {
"one" : " I have {n} apple. " ,
"other" : " I have {n} apples. "
},
"place(ordinal)" : {
"one" : " {n}st place. " ,
"two" : " {n}nd place. " ,
"few" : " {n}rd place. " ,
"other" : " {n}th place. "
}
}
}デフォルトでは、パラメーター名はnです。修飾子を追加することで、それを変更できます。
{
"someKey" : {
"apple(param=appleCount)" : {
"one" : " I have one apple. " ,
"other" : " I have multiple apples. "
}
}
} String a = Strings . Instance . Root . SomeKey . Apple ( appleCount : 1 ) ; // notice 'appleCount' instead of 'n' PluralParameterを使用して、デフォルトのパラメーターをグローバルに設定できます。
[ Translations (
InputFileName = "strings" ,
PluralParameter = "count" ) ]
internal partial class Strings ;ある翻訳を別の翻訳にリンクできます。プレフィックス@:その後、目的の変換への絶対パスが続きます。
{
"fields" : {
"name" : " my name is {firstName} " ,
"age" : " I am {age} years old "
},
"introduce" : " Hello, @:fields.name and @:fields.age "
} String s = Strings . Instance . Root . Introduce (firstName : "Tom" , age : 27 ); // Hello, my name is Tom and I am 27 years old.生成されたコードは次のようになります。
/// In ru, this message translates to:
/// **"Hello, {_root.Fields.Name(firstName: firstName)} and {_root.Fields.Age(age: age)}"**
public virtual string Introduce ( object firstName , object age ) => $ "Hello, { _root . Fields . Name ( firstName : firstName ) } and { _root . Fields . Age ( age : age ) } " ;オプションで、 {}でパスを囲むことにより、リンクされた翻訳を逃れることができます。
{
"fields" : {
"name" : " my name is {firstName} "
},
"introduce" : " Hello, @:{fields.name}inator "
}リスト内のリストを配置することもできます!
{
"niceList" : [
" hello " ,
" nice " ,
[
" first item in nested list " ,
" second item in nested list "
],
{
"wow" : " WOW! " ,
"ok" : " OK! "
},
{
"aMapEntry" : " access via key " ,
"anotherEntry" : " access via second key "
}
]
} String a = Strings . Instance . Root . NiceList [ 1 ] ; // "nice"
String b = Strings . Instance . Root . NiceList [ 2 ] [ 0 ] ; // "first item in nested list"
String c = Strings . Instance . Root . NiceList [ 3 ] . Ok ; // "OK!"
String d = Strings . Instance . Root . NiceList [ 4 ] . AMapEntry ; // "access via key"生成されたコードは次のようになります。
public virtual List < dynamic > NiceList => [
"hello" ,
"nice" ,
new [ ] {
"first item in nested list" ,
"second item in nested list" ,
} ,
new Feature1NiceList0i3Ru ( _root ) ,
new Feature1NiceList0i4Ru ( _root ) ,
] ;文字列キーを使用して各翻訳にアクセスできます。
(map)修飾子を追加します。
{
"a(map)" : {
"helloWorld" : " hello "
},
"b" : {
"b0" : " hey " ,
"b1(map)" : {
"hiThere" : " hi "
}
}
}これで、キーを使用して翻訳にアクセスできます。
String a = Strings . Instance . Root . A [ "helloWorld" ] ; // "hello"
String b = Strings . Instance . Root . B . B0 ; // "hey"
String c = Strings . Instance . Root . B . B1 [ "hiThere" ] ; // "hi"生成されたコードは次のようになります:
/// In ru, this message translates to:
/// **"hey"**
public virtual string B0 => "hey" ;
public virtual IReadOnlyDictionary < string , string > B1 => new Dictionary < string , string > {
{ "hiThere" , " hi "},
} ; GPTを活用して、コンテキストを意識した翻訳でアプリを国際化します。
Slang CLIをインストール:
dotnet tool install --global Slang.CLI次に、slang.jsonに次の構成を追加します。
{
"base_culture" : " en " ,
"gpt" : {
"base_culture" : " ru " ,
"model" : " gpt-4o-mini " ,
"description" : " Showcase for Slang.Net.Gpt "
}
}次に、slang-gptを使用します。
slang gpt --target=en --api-key= < api-key >詳細:ドキュメント