Skip to content

afunc233/I18n.Avalonia

Repository files navigation

I18n.Avalonia

简体中文 | English

一个给 Avalonia 做的 I18n 的类库. 包括一个 SourceGenerator 从 resx 文件生成的 Designer 类中生成一个封装器。

内容

I18n.Avalonia

一个给 Avalonia 做的 I18n 的类库

特点

  • 支持动态切换语言
    • 随时切换当前语言,无须重新启动程序
  • 支持插值
    • 支持一个 format 和多种类参数混合,
      • Binding 参数
      • I18n 参数
      • 固定参数

这是一个混用 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 里 生成本项目

I18n.Avalonia.Sample

一个 引用 I18n.Avalonia.Generator 的项目. 需要注意项目文件 I18n.Avalonia.Sample.csproj 里的 ProjectReference , 确保是作为SourceGenerator 引入的 关键点 就在设置 OutputItemType="Analyzer" ReferenceOutputAssembly="false"

I18n.Avalonia.Tests

单元测试,目前比较简单

SourceGenerator 深入

怎么调试?

如何确定我应该使用哪些语法节点?

考虑安装 Roslyn syntax tree viewer 插件 Rossynt.

如何进一步了解怎么编写 SourceGenerator ?

观看教程 视频: Let’s Build an Incremental Source Generator With Roslyn, by Stefan Pölz 完整可用的信息在 Source Generators Cookbook.

About

avalonia i18n

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages