【发布时间】:2021-07-05 15:12:36
【问题描述】:
简介
你好,我有一个 Angular+Ionic 项目,我正在使用 Transloco 翻译一些文本。
问题
我有一个 consts 文件,其中包含字符串,我希望能够在同一个文件中翻译它们,但我不知道如何执行此操作,因为我无法在 consts 文件中导入 TranslocoService。
代码
test.consts.ts
export const LABELS = {
hello_world: 'Hello World!',
goodbye: "Goodbye"
}
es.json
{
"label_1": "Hola mundo!",
"label_2": "Adios"
}
替代方案
我想了一个替代方案,以防无法在 consts 文件上进行翻译:
- 使用翻译键代替短语:例如将
Hello World!替换为label_1。然后在使用 consts 文件时,我必须翻译该值,例如:this.translocoSrv.t(LABELS.label_1)
虽然这种替代方案有效,但实施起来需要很多时间。我将不得不替换每个 consts 文件并重构每个使用 consts 文件的组件、模板等。
提前致谢。
【问题讨论】: