Antelcat.I18N.SourceGenerators
1.0.0
dotnet add package Antelcat.I18N.SourceGenerators --version 1.0.0
NuGet\Install-Package Antelcat.I18N.SourceGenerators -Version 1.0.0
<PackageReference Include="Antelcat.I18N.SourceGenerators" Version="1.0.0" />
paket add Antelcat.I18N.SourceGenerators --version 1.0.0
#r "nuget: Antelcat.I18N.SourceGenerators, 1.0.0"
// Install Antelcat.I18N.SourceGenerators as a Cake Addin #addin nuget:?package=Antelcat.I18N.SourceGenerators&version=1.0.0 // Install Antelcat.I18N.SourceGenerators as a Cake Tool #tool nuget:?package=Antelcat.I18N.SourceGenerators&version=1.0.0
<div align="center">
<h1>
Antelcat.{I18N}
</h1>
给.NET应用程序提供响应式的多语言支持。
</div>
<p align="center"> <img alt="dotnet-version" src="https://img.shields.io/badge/WPF-%3E%3D4.0-2C896A.svg"/> <img alt="dotnet-version" src="https://img.shields.io/badge/Avalonia-%3E%3D11.0-AE42F8.svg"/> <img alt="csharp-version" src="https://img.shields.io/badge/C%23-preview-3BA93F.svg"/> <img alt="nuget" src="https://img.shields.io/badge/Nuget-v1.0.0-blue.svg"/> </p>
🇬🇧 English
🗔 受支持的平台
📖 示例
<div float="right"> <img src="docs/demo.zh.png" width="45%"/> <img src="docs/demo.en.png" width="45%"/> </div>
静态使用
当你在项目中使用.resx
文件作为语言文件时,你可以使用Antelcat.I18N.Attributes.ResourceKeysOfAttribute
来自动生成资源键:
using Antelcat.I18N.Attributes;
namespace MyProject
//Auto generated class should be partial
[ResourceKeysOf(typeof(My.Resource.Designer.Type))]
public partial class LangKeys
{
}
然后在你的.xaml
文件中使用x:Static
来为你的控件提供资源键
如果你已经在你的.resx
文件中有
<data name="Language" xml:space="preserve">
<value>语言</value>
</data>
你可以像这样使用:
<TextBolck Text="{x:Static myProject:LangKeys.Language}"/>
然后你可以使用这个键来绑定语言源
<TextBlock Text="{I18N {x:Static myProject:LangKeys.Language}}"/>
当你想要改变语言时,只需要调用
using System.Windows;
I18NExtension.Culture = new CultureInfo("language code");
你可以看到文本在语言之间变化。
动态使用
有时你的源文本并不是在你的应用程序中定义的,而是从其他来源(如网络)接收到的,你可以使用I18N
直接绑定文本。
如果你收到了一个像这样的json:
{
"message": "This is a message"
}
并且你已经在.resx
中将他翻译成了另一种语言
<data name="This is a message" xml:space="preserve">
<value>这是一条消息</value>
</data>
你肯定会设计一个ViewModel
并且将他设置到属性Message
中,你可以像这样绑定:
<TextBlock Text="{I18N {Binding Message}}"/>
每当Message
属性被改变或者语言源被改变时,文本都会自动更新。
多个文本组合和格式化
有些情况下,你需要将多个文本组合起来,或者对文本进行格式化,你可以使用I18N
和LanguageBinding
来实现。
如果你已经有了如下翻译的.resx
文件:
<data name="Current_is" xml:space="preserve">
<value>当前的 {0} 是 {1}</value>
</data>
<data name="Language" xml:space="preserve">
<value>语言</value>
</data>
<data name="Chinese" xml:space="preserve">
<value>中文</value>
</data>
并且在.xaml
中
<TextBlock>
<TextBlock.Text>
<I18N Key="{x:Static myProject:LangKeys.Current_is}">
<LanguageBinding Key="{x:Static myProject:LangKeys.Language}"/>
<Binding Path="Language"/>
</I18N>
</TextBlock.Text>
</TextBlock>
此时 I18N.Key
是字符串的模板,其中的 LanguageBinding
和 Binding
会提供模板的参数,他们会被按顺序格式化成最终的文本。同时保持整体的响应性。
Product | Versions 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. |
.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 | net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. 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. |
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Antelcat.I18N.SourceGenerators:
Package | Downloads |
---|---|
Antelcat.I18N.Avalonia
Reactive language support for Avalonia applications when using .resx translate file. |
|
Antelcat.I18N.WPF
Reactive language support for WPF applications when using .resx translate file. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 2,184 | 12/2/2023 |
Initial release