ประเภทที่ปลอดภัย I18N สำหรับ. NET
Slang.net เป็นพอร์ต. NET ของสแลงจากชุมชน Dart/Flutter ที่มีคุณสมบัติใหม่ (เช่นรูปแบบสตริง)
คุณสามารถดูว่าไฟล์ที่สร้างขึ้นทั่วไป en และ de look ได้อย่างไร
ติดตั้งไลบรารีเป็นแพ็คเกจ NUGET:
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 โดยค่าเริ่มต้น สะดวกเพราะมีความยืดหยุ่นสูงสุด
คุณสามารถระบุประเภทโดยใช้สองตัวเลือกไวยากรณ์: 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 } " ;
}ห้องสมุดนี้ใช้แนวคิดที่กำหนดไว้ที่นี่
บางภาษามีการสนับสนุนนอกกรอบ ดูที่นี่
Plurals ถูกตรวจพบโดยคำหลักต่อไปนี้: 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จากนั้นเพิ่มการกำหนดค่าต่อไปนี้ในคำสแลงของคุณ json:
{
"base_culture" : " en " ,
"gpt" : {
"base_culture" : " ru " ,
"model" : " gpt-4o-mini " ,
"description" : " Showcase for Slang.Net.Gpt "
}
}จากนั้นใช้คำสแลง-gpt:
slang gpt --target=en --api-key= < api-key >ดูเพิ่มเติม: เอกสาร