本文档介绍了 openpilot 如何支持其用户界面的多种语言。它概述了翻译文件的结构、语言选择机制以及系统如何在整个应用程序中处理不同语言。
openpilot 通过基于 Qt 翻译框架的强大国际化系统支持多种语言。这使得用户界面能够以多种语言显示,同时保持单一的代码库。
国际化系统包括:
来源:selfdrive/ui/qt/offroad/settings.cc218-229 selfdrive/ui/translations/main_ko.ts1-15
openpilot 目前支持以下语言:
| 语言代码 | 语言名称 | 文件 |
|---|---|---|
| en | 英语(默认) | 不适用(内置于源代码中) |
| ko | 韩语 | main_ko.ts |
| pt-BR | 巴西葡萄牙语 | main_pt-BR.ts |
| zh-CHT | 繁体中文 | main_zh-CHT.ts |
| zh-CHS | 简体中文 | main_zh-CHS.ts |
| ja | 日语 | main_ja.ts |
| de | 德语 | main_de.ts |
| ar | 阿拉伯语 | main_ar.ts |
| th | 泰语 | main_th.ts |
| fr | 法语 | main_fr.ts |
| tr | 土耳其语 | main_tr.ts |
| es | 西班牙语 | main_es.ts |
来源:selfdrive/ui/translations/main_ko.ts1-5 selfdrive/ui/translations/main_pt-BR.ts1-5 selfdrive/ui/translations/main_zh-CHT.ts1-5 selfdrive/ui/translations/main_zh-CHS.ts1-5 selfdrive/ui/translations/main_ja.ts1-5 selfdrive/ui/translations/main_de.ts1-5 selfdrive/ui/translations/main_ar.ts1-5 selfdrive/ui/translations/main_th.ts1-5 selfdrive/ui/translations/main_fr.ts1-5 selfdrive/ui/translations/main_tr.ts1-5 selfdrive/ui/translations/main_es.ts1-5
openpilot 中的翻译文件遵循 Qt Linguist 翻译源 (TS) 格式,这是一种基于 XML 的存储翻译的格式。
每个翻译文件遵循此结构:
TS` 元素context` 元素,用于对相关消息进行分组message` 元素,这些元素包含:source` 元素translation` 元素来自韩语翻译文件的示例
来源:selfdrive/ui/translations/main_ko.ts1-20
用户可以通过 UI 中的“设备设置”面板更改语言。语言选择过程如下:
负责语言选择的代码位于 `DevicePanel` 构造函数中。
这段代码:
来源:selfdrive/ui/qt/offroad/settings.cc218-229
UI 组件使用 Qt 的翻译机制以所选语言显示文本。这是通过 `tr()` 函数完成的,该函数在运行时查找翻译。
当 UI 加载特定语言时,`tr()` 函数会从相应的翻译文件中返回翻译后的文本。
语言设置存储在系统参数中,并在 UI 启动时读取。这使得系统可以加载适当的翻译文件并将其应用于 UI。
来源:selfdrive/ui/qt/offroad/settings.cc218-229 selfdrive/ui/qt/offroad/settings.cc347-349
要为 openpilot 添加新语言:
selfdrive/ui/translations/` 目录中创建一个名为 `main_XX.ts`(其中 XX 是语言代码)的新翻译文件。可以使用 Qt Linguist 编辑翻译文件,这是一个专门用于编辑 Qt 翻译文件的工具。
openpilot 中的语言代码遵循 ISO 语言代码标准。
en`)。pt-BR`)。这些代码用于翻译文件名以及 `TS` 元素的 `language` 属性。
翻译系统还支持复数和格式化等特殊情况。
这允许正确处理复数形式,因为复数形式可能因语言而异。
来源:selfdrive/ui/translations/main_ko.ts587-592
当没有显式选择语言时,openpilot 默认为英语。该系统目前不会自动检测设备语言;用户必须手动选择他们偏好的语言。
openpilot 中的国际化系统提供了一种灵活的方式来支持用户界面的多种语言。它使用 Qt 的翻译框架,该框架提供了一种强大且标准化的国际化方法。
该系统设计为易于扩展,支持更多语言,并且 UI 为用户提供了一种直观的方式来选择他们偏好的语言。
刷新此 Wiki
最后索引时间2025 年 4 月 18 日(65c210)