.NET 용 타입 안전 I18N
slang.net은 새로운 기능 (문자열 형식)을 갖춘 다트/플러터 커뮤니티의 속어의 .NET 포트입니다.
생성 된 파일 General, EN 및 DE Look을 볼 수 있습니다.
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" ; TimeSpan DateOnly ToString(format) int decimal long TimeOnly double DateTime float 다른 유형의 경우 형식 문자열은 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 ; 한 번역을 다른 번역에 연결할 수 있습니다. Prefix @: 다음과 같은 번역으로의 절대 경로를 추가하십시오.
{
"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그런 다음 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 >자세히보기 : 문서화