【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");
相关推荐
wadesir14 小时前
深入理解Rust静态生命周期(从零开始掌握‘static的奥秘)
开发语言·后端·rust
脑极体18 小时前
蓝河入海:Rust先行者vivo的开源之志
开发语言·后端·rust·开源
badmonster019 小时前
实时代码库索引:用 CocoIndex 构建智能代码搜索的终极方案
python·rust
哈哈~haha21 小时前
ui5_Walkthrough_Step 8:Translatable Texts
i18n·ui5·walkthrough
黛色正浓1 天前
【React】极客园案例实践-项目搭建和登录模块
前端·react.js·rust
思密吗喽1 天前
npm install 报错,解决 node-sass@4.14.1 安装失败问题
rust·npm·node.js·毕业设计·sass·课程设计
青云交1 天前
深度实战:Rust交叉编译适配OpenHarmony PC——ansi_term完整适配案例
rust·交叉编译·命令行工具·openharmony pc·ansi_term·适配案例·终端颜色
星释1 天前
Rust 练习册 106:太空年龄计算器与宏的魔法
开发语言·后端·rust
ALex_zry1 天前
系统编程的基石:补码循环溢出与Rust变量绑定的深度探索
开发语言·后端·rust
ALex_zry1 天前
Rust语言基础分析与C++对比:系统编程的现代演进
java·c++·rust