对于那些希望向 Discourse 主题或主题组件添加自定义语言和翻译的人来说,他们现在可以包含本地化字符串,这些字符串可供用户界面组件使用。翻译以与核心/插件翻译相同的格式存储,并且可以以几乎相同的方式使用。
主题可以提供格式如 /locales/{locale}.yml 的翻译文件。这些文件应为有效的 YAML,其中一个顶层键等于所定义的语言环境。这些可以通过 discourse_theme 命令行工具 (CLI) 定义,导入 .tar.gz 文件,从 GIT 存储库安装,或通过 theme-creator.discourse.org 上的编辑器进行。
一个示例语言环境文件可能如下所示:
en:
theme_metadata:
description: "This is a description for my theme"
settings:
theme_setting_name: "This is a description for the setting `theme_setting_name`"
another_theme_setting_name:
description: "This is a description for the setting `another_theme_setting_name`"
sidebar:
welcome: "Welcome"
back: "back,"
welcome_subhead: "We're glad you're here!"
likes_header: "Share the Love"
badges_header: "Your Top Badges"
full_profile: "View your full profile"
管理员可以在 /admin/customize/themes 用户界面中按主题覆盖单个键。回退处理方式与核心相同,因此对于非英语语言,不完整的翻译使用英语键是可以的。
在后台,这些翻译与核心翻译一起存储,位于一个特定于主题的命名空间下。例如:
theme_translation.{theme_id}.sidebar.welcome
您绝不应在主题代码中硬编码 theme_id,因此有几种方法可以帮助您访问翻译。
在 .hbs 文件中,您可以使用专用的助手 (helper):
{{theme-i18n "my_translation_key"}}
或者,如果您需要将翻译键传递给另一个组件,您可以使用 theme-prefix 助手:
<DButton @label={{theme-prefix "my_translation_key"}} />
在 Javascript 或 .gjs 文件中,您可以使用 themePrefix 函数。它会自动注入,无需导入:
const result = I18n.t(themePrefix("my_translation_key"));
<template>{{i18n (themePrefix "blah")}}</template>
有关在主题中使用翻译的完整示例,请查看 @awesomerobot 的 Fakebook 主题:GitHub - discourse/Fakebook
本文档是版本控制的 - 在 github 上建议更改。


