【Rust GUI开发入门】编写一个本地音乐播放器(12. 国际化应用-多语言支持)

本系列教程对应的代码已开源在 Github zeedle

本篇文章介绍Slint UI如何为播放器页面添加多语言支持,让你的应用走向国际!Translations | Slint Docs

1. 使用@tr修饰.slint中的字面量

就像这样:

2. 提取要翻译的字符串

打开git bash,输入:

bash 复制代码
find ui -name \*.slint | xargs slint-tr-extractor -o zeedle.pot

此命令会找出ui文件夹下所有的.slint文件,并把这些文件路径作为参数全部传给slint-tr-extractor,后者会提取所有被@tr修饰的字面量到.pot中。

3. 使用Poedit翻译.pot文件

翻译完成后,保存生成的.po文件到lang/语言代码/LC_MESSAGES/zeedle.po

语言代码:

  • 中文简体:zh_CN
  • 西班牙语:es
  • 德语:de

...其余请自行查阅。

4. 静态编译到可执行文件中

build.rs中指定语言目录,跟上面对应,也就是lang

rust 复制代码
fn main() {
    let cfg = slint_build::CompilerConfiguration::new()
        .with_style("fluent".into())
        .with_bundled_translations("lang");
    slint_build::compile_with_config("ui/app.slint", cfg).expect("slint build failed");
}

5. 使用Rust API动态选择语言

可以在用户callback中调用此函数完成UI语言更改:

rust 复制代码
slint::select_bundled_translation(语言代码字面量)
	.expect("failed to set language");
相关推荐
ServBay3 小时前
别再用初级写法写Rust了,8个写法你值得拥有
后端·rust
望眼欲穿的程序猿7 小时前
Hello World
嵌入式硬件·rust
望眼欲穿的程序猿10 小时前
按键控制 LED
嵌入式硬件·rust
ai安歌11 小时前
鸿蒙PC:Linux 搭建 Rust 开发环境并实现计算器项目
linux·rust·harmonyos
星栈11 小时前
一套 Rust 代码跑三端:为什么我开始关注 Dioxus
前端·rust·前端框架
不良使11 小时前
鸿蒙PC迁移_LocalSend 迁移到鸿蒙 PC:一次 Flutter + Rust + 三方库适配的完整记录
flutter·rust·harmonyos
红尘散仙1 天前
别再手动录屏了:用 VHS 给终端应用生成会动的文档素材
后端·rust
techdashen1 天前
从 Windows 的 ping.exe 入手:动态库、调用约定与 Rust FFI
开发语言·windows·rust
Venuslite1 天前
Mac系统安装Rust
rust
shimly1234561 天前
RUST Arc
rust