我可以使用 Angular i18n 翻译打字稿代码中的字符串文字吗 [英] Can I use Angular i18n to translate string literals in typescript code

查看:26
本文介绍了我可以使用 Angular i18n 翻译打字稿代码中的字符串文字吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular 的 i18n 工具对我的 Angular 应用程序进行本地化,该工具将 HTML 模板中的文本提取到xlf 文件,然后使用 AOT(提前编译)构建整个应用的本地化版本.

I'm localizing my Angular app using Angular's i18n tools, which extract text from HTML templates into an xlf file, and then build a localized version of the whole app using AOT (ahead of time compilation).

我的问题是:是否可以使用这个框架来提取打字稿代码中的字符串文字,所以它们被列在同一个 xlf 文件中并在本地化的 AOT 构建中被替换?

My question is: Can is use this framework to extract string literals in typescript code, so they are listed in the same xlf file and replaced in the localized AOT build?

理想情况下,我想在我的打字稿代码中写下这样的内容:

Ideally, I'd like to write something like this in my typescript code:

foo() {
   this.bar = i18n('baz');

字符串 'baz' 将列在我发送给我的翻译的 xlf 文件中.

and the string 'baz' would be listed in the xlf file I send to my translator.

或者,如果这是不可能的,是否有一个库可以做类似的事情?IE.将 typescript 代码中的字符串提取到 xlf 文件中,然后在运行时或构建期间替换它们?

Alternatively, if this isn't possible, is there a library that does something similar? I.e. extract strings from typescript code into an xlf file, then replaces them either at runtime or during build?

推荐答案

我可以确认@JB Nizet 在他的回答中所指的是现在在 Angular 9 中工作,所以这是可行的:

I can confirm what @JB Nizet is referring to in his answer is now working in Angular 9, so this works:

  foo() {
    const appTitle = $localize`:@@siteTitle:Example Title`;
    // appTitle is now translated based on locale

    titleService.setTitle(appTitle); 
  }

您可以在此处了解更多信息 https:///github.com/angular/angular/blob/master/packages/localize/init/index.ts

You can read more about it here https://github.com/angular/angular/blob/master/packages/localize/init/index.ts

这篇关于我可以使用 Angular i18n 翻译打字稿代码中的字符串文字吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆