audio_vibe/docs/文本与国际化规范.md
2026-05-30 20:54:38 +08:00

127 lines
5.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 文本与国际化规范
最后更新:2026-05-30
本规范用于统一 AudioVibe 中用户可见文本、Toast、通知、错误提示和分享内容的取值方式。目标是让 App 内语言设置稳定生效,避免同一页面一部分跟随 App 语言、一部分跟随系统语言。
## 总原则
- 用户可见文本必须进入 `strings.xml` 和 `AppStrings`,不能在业务代码里硬编码。
- App 内语言选择优先于系统语言;跟随系统时才按系统语言解析。
- 原始异常、FFmpeg 输出、jaudiotagger 输出等技术细节进入 Timber 日志,不直接展示给普通用户。
- UI 展示文案和底层诊断信息分离:用户看到本地化摘要,反馈日志保留原始错误。
## 分层规则
| 层级 | 推荐方式 | 禁止/限制 | 原因 |
|:---|:---|:---|:---|
| Compose UI 层 | `LocalAppStrings.current` | 不直接 `context.getString(R.string.xxx)`;不直接 `AppStringResolver.getString()` | UI 会随 Compose 状态重组,用户切换语言后能即时刷新 |
| ViewModel | 优先返回状态码、事件类型或 `StringRes + args`;确实要发 Toast/一次性消息时用 `AppStringResolver.getString()` | 不持有 `Context`;不拼复杂最终文案 | ViewModel 不在 Composition 内,不能读取 `LocalAppStrings` |
| Repository / Service / common 工具 | `AppStringResolver.getString(context, R.string.xxx)` | 不直接 `context.getString(R.string.xxx)` | 后台、通知、分享、写文件错误也要跟随 App 内语言 |
| Android 系统 API 特例 | 允许 `context.getString()` | 只在确定必须跟随系统语言或系统 API 要求时使用 | 例如极少数平台级标签,需在代码旁说明原因 |
## Compose UI 层写法
页面、组件、弹窗、按钮、标签、`contentDescription`、业务 Toast 文案优先从 `LocalAppStrings.current` 读取。
```kotlin
@Composable
private fun SaveBar(onSave: () -> Unit) {
val strings = LocalAppStrings.current
PrimaryButton(
label = strings.save,
onClick = onSave
)
}
```
Toast 也应由 UI 层使用当前主题样式展示:
```kotlin
val strings = LocalAppStrings.current
val toastStyle = currentVibeToastStyle()
showVibeToast(
context = context,
message = strings.temporarilyUnavailable,
style = toastStyle
)
```
## ViewModel 写法
ViewModel 优先不要保存最终展示字符串。更推荐保存状态或事件,让 Composable 解析文案。
推荐方向:
```kotlin
data class UiState(
val saveError: SaveError? = null
)
enum class SaveError {
PermissionRequired,
UnsupportedFormat,
WriteFailed
}
```
如果当前结构已经使用一次性 `message: String?`,或者消息来自 Repository / Service 回调,允许使用 `AppStringResolver` 生成短文案:
```kotlin
private fun str(@StringRes id: Int) =
AppStringResolver.getString(getApplication(), id)
```
注意:ViewModel 不能为了取字符串而持有 Activity/Composable `Context`。
## Repository / Service / common 写法
非 Compose 层统一用 `AppStringResolver`,不能直接 `context.getString()`。
```kotlin
private fun text(@StringRes id: Int): String =
AppStringResolver.getString(appContext, id)
```
失败返回给 UI 的文案应是本地化摘要:
```kotlin
Timber.w(error, "Tag write failed: trackId=%d", track.id)
return MetadataEditResult.Failure(text(R.string.tag_write_failed))
```
不要这样做:
```kotlin
return MetadataEditResult.Failure(error.message ?: "写入失败")
```
原因:`error.message` 往往是英文、系统语言、库内部输出或包含路径信息,直接展示会导致语言不一致,也可能暴露不适合用户看的诊断内容。
## 禁止项
- 禁止在新增业务 UI 中硬编码用户可见文字。
- 禁止直接使用系统默认 `Toast.makeText()`。
- 禁止在 Compose UI 里用 `context.getString(R.string.xxx)` 获取业务文案。
- 禁止在 Repository / Service / common 工具里直接 `context.getString(R.string.xxx)`。
- 禁止把底层异常原文直接作为 Toast 或页面提示。
- 禁止只改 `values/strings.xml`,忘记同步其它 `values-*` 语言文件。
## 允许例外
以下情况可以例外,但需要在代码旁说明原因:
- Android 系统 API 明确要求系统语言字符串,且该文本不属于 App 内业务文案。
- 调试日志、测试名、内部枚举名、协议字段、文件扩展名、FFmpeg 参数等非用户展示内容。
- 应用市场截图、文档示例中的演示文本。
## Review 检查点
- 搜索新增 Kotlin 代码中是否存在中文、英文用户文案硬编码。
- 搜索 `context.getString(R.string`、`appContext.getString(R.string`、`getString(R.string`,确认只出现在允许位置。
- Compose UI 是否使用 `LocalAppStrings.current`。
- ViewModel 是否避免持有 `Context`,是否避免拼复杂最终文案。
- Repository / Service / common 是否使用 `AppStringResolver`。
- Toast、通知栏、反馈邮件、错误提示是否跟随 App 内语言设置。
- 所有新增 string key 是否同步到全部 `values-*` 文件。