.net的類型安全i18n
slang.net是帶有新功能(如字符串格式)的飛鏢/顫音社區的lang語端口。
您可以查看生成的文件一般,en和de的方式
將庫作為Nuget軟件包安裝:
Install-Package dotnet add package Slang.Net重要文件必須以“ .i18n.json”結尾。這是必要的,以便源機器人不會跟踪其他額外費用的更改。
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 。這很方便,因為它提供了最大的靈活性。
您可以使用兩個語法選項指定類型: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, 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 } " ;
}該庫使用此處定義的概念。
有些語言開箱即用。請參閱此處。
複數由以下關鍵字檢測到: zero , one , two , 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." ) ;默認情況下,檢測到的複數是紅衣主教。
要指定序數,您需要添加(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通過上下文感知的翻譯來國際化您的應用程序。
安裝語CLI:
dotnet tool install --global Slang.CLI然後在s語中添加以下配置:json:
{
"base_culture" : " en " ,
"gpt" : {
"base_culture" : " ru " ,
"model" : " gpt-4o-mini " ,
"description" : " Showcase for Slang.Net.Gpt "
}
}然後使用lang-gpt:
slang gpt --target=en --api-key= < api-key >查看更多:文檔