I18n.Avalonia.Generator
1.0.2
dotnet add package I18n.Avalonia.Generator --version 1.0.2
NuGet\Install-Package I18n.Avalonia.Generator -Version 1.0.2
<PackageReference Include="I18n.Avalonia.Generator" Version="1.0.2" />
<PackageVersion Include="I18n.Avalonia.Generator" Version="1.0.2" />
<PackageReference Include="I18n.Avalonia.Generator" />
paket add I18n.Avalonia.Generator --version 1.0.2
#r "nuget: I18n.Avalonia.Generator, 1.0.2"
#addin nuget:?package=I18n.Avalonia.Generator&version=1.0.2
#tool nuget:?package=I18n.Avalonia.Generator&version=1.0.2
I18n.Avalonia
简体中文 | English
一个给 Avalonia 做的 I18n 的类库. 包括一个 SourceGenerator 从 resx 文件生成的 Designer 类中生成一个封装器。
内容
I18n.Avalonia
一个给 Avalonia 做的 I18n 的类库
特点
- 支持动态切换语言
- 随时切换当前语言,无须重新启动程序
- 支持插值
- 支持一个 format 和多种类参数混合,
- Binding 参数
- I18n 参数
- 固定参数
- 支持一个 format 和多种类参数混合,
这是一个混用 I18n 和 Binding 作为参数的例子,Key 获取到的文本可以接收两个参数
<TextBlock Name='textBlock'>
<TextBlock.Text>
<i18n:I18n Key="{x:Static sample:LangKeys.Current_language_is}">
<i18n:I18n.Args>
<i18n:I18n Key="{x:Static sample:LangKeys.Language}" />
<Binding Path="((gb:CultureInfo)Culture).NativeName" />
</i18n:I18n.Args>
</i18n:I18n>
</TextBlock.Text>
</TextBlock>
由于在 I18nExtension.cs 里 很难从 Binding 值 计算出 I18nUnit 作为 key 所以又新加了一个附加属性 I18nAttached.cs 的用法,本意只是为了 Key 可以 绑定自 ViewModel
<TextBlock Name='textBlock'
i18n:I18nAttached.Key="{Binding Current_language_is}"
i18n:I18nAttached.Args="{Binding Args}"
i18n:I18nAttached.TextProperty="{x:Static TextBlock.TextProperty}" />
附加属性 I18nAttached.cs 的用法 参数如果是希望 在 Axaml 内设置的话 需要额外多写一句 才能有正常效果,也许 Avalonia 应该优化一下 不过这里本就是为了解决 Binding ViewModel 的 I18nUnit 值,所以正常情况下 参数 也应该来自 Binding 这里只是记录一下支持的特殊用法
i18n:I18nAttached.Args="{Binding (i18n:I18nAttached.Args),RelativeSource={RelativeSource Mode=Self}}"
完整内容
<TextBlock Name='textBlock'
i18n:I18nAttached.Key="{Binding Addition_formula_2}"
i18n:I18nAttached.Args="{Binding (i18n:I18nAttached.Args),RelativeSource={RelativeSource Mode=Self}}"
i18n:I18nAttached.TextProperty="{x:Static TextBlock.TextProperty}">
<i18n:I18nAttached.Args>
<sys:Int32>1</sys:Int32>
<sys:Int32>1</sys:Int32>
<sys:String>2</sys:String>
</i18n:I18nAttached.Args>
</TextBlock>
I18n.Avalonia.Generator
使用 public static partial 类 和 ResxKeysOfAttribute 标识 生成类的 命名空间 和 对应的 类名
要看生成代码的话 需要您先在 IDE 里 生成本项目
- ResxKeysGenerator.cs: 本项目中生效的 SourceGenerator 首先会生成 ResxKeysOfAttribute 供项目使用,用法可参考 LangKeys.cs
I18n.Avalonia.Sample
一个 引用 I18n.Avalonia.Generator 的项目. 需要注意项目文件 I18n.Avalonia.Sample.csproj 里的 ProjectReference
, 确保是作为SourceGenerator 引入的 关键点 就在设置 OutputItemType="Analyzer" ReferenceOutputAssembly="false"
I18n.Avalonia.Tests
单元测试,目前比较简单
I18nExtensionTest.cs 是给 I18nExtension.cs 做的简单单元测试,也展示了可行的用法
I18nAttachedTest.cs 是给 I18nAttached.cs 做的简单单元测试,也展示了可行的用法
SourceGenerator 深入
怎么调试?
- 使用 launchSettings.json 文件.
- 调试测试.
如何确定我应该使用哪些语法节点?
考虑安装 Roslyn syntax tree viewer
插件 Rossynt.
如何进一步了解怎么编写 SourceGenerator ?
观看教程 视频: Let’s Build an Incremental Source Generator With Roslyn, by Stefan Pölz 完整可用的信息在 Source Generators Cookbook.
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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 | 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. |
-
.NETStandard 2.0
- I18n.Avaloniα (>= 1.0.2)
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- First Release