angular - ng extract-i18n 配置忽略重复消息

标签 angular internationalization

在 Angular v10 中,我们使用内置的 i18n 功能。 现在,当我运行 ng extract-i18n 时,它会返回许多重复消息警告。

只是一个简短的例子:

   WARNINGS:
    - Duplicate messages with id "lastName":
      - "Last Name" : src\app\core...\
      - "Last Name" ...

但是,这是我故意的。我正在尝试在整个应用程序中重复使用某些 i18n ID。

即来自各种模板的一些 HTML 片段:

 <label i18n="Label|Label for Last Name@@lastName">Last Name</label>
 <mat-label i18n="Label|Label for Last Name@@lastName">Last Name</mat-label>
<mat-radio-button value="LastName" i18n="Button|Button for User Preferences@@lastName">
                Last Name</mat-radio-button
              >
<kendo-grid-column
  i18n-title="Title|Title for Orders Grid@@firstName"
  field="Student.FirstName"
  title="First Name"
 ></kendo-grid-column>

换句话说,像“姓氏”和“名字”这样的文本标签在我们的应用程序中会重复多次,因此我为每个标签都有一个通用的 i18n 标签。

然后 CLI 在 XLF 输出文件中生成 id 属性:

<trans-unit id="lastName" datatype="html">
        <source>Last Name</source>        
        <context-group purpose="location">
          <context context-type="sourcefile">src/app/core/modals/.../info-modal.html</context>
          <context context-type="linenumber">33</context>
        </context-group>
        <context-group purpose="location">
          <context context-type="sourcefile">src/app/.../student-list.component.html</context>
          <context context-type="linenumber">26</context>
        </context-group>
        <context-group purpose="location">
          <context context-type="sourcefile">src/app/.../another-list.component.html</context>
          <context context-type="linenumber">101</context>
        </context-group>        
      </trans-unit>

这个方法有什么问题吗?或者 Angular CLI 只是想提供帮助?

我也许可以忽略这些警告,但不清楚如何忽略。

我还在github上找到了一些信息,但不太清楚。 https://github.com/angular/angular-cli/issues/19207

我还发现了一条指向一些配置选项的评论:https://github.com/angular/angular/blob/79620f5139c80f1b3b38168744ca99a224c7e5cd/packages/localize/src/tools/src/extract/main.ts#L89-L95

最佳答案

你的方法是有效的。

问题(最有可能)来自于空格/换行符的不一致使用:请注意,在您的示例中,一个文本是 \nLast Name,另一个文本是 Last Name!

尽管在这种情况下可能没有什么区别,但保持具有相同 id 的文本相同是一个很好的做法 - 即使对于空白也是如此。

关于angular - ng extract-i18n 配置忽略重复消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70900706/

相关文章:

java - Java数据类型到Typescript类型

javascript - Angular:将 SQL 数据加载到文本框中

angular - 单击 Angular 6 中的按钮时调用不同的函数

django - {% trans "string"as my_translated_string %} 未在模板中呈现内容

angular - 清晰设计 TreeView 递归问题

node.js - 模板网址不起作用

python - django-registration:由于密码字符无效,中国用户无法注册

git - Qt 翻译和 git 的最佳实践

c++ - 如何控制 gettext 系统中的翻译?

android - 基于android上用户区域设置的日期格式