【问题标题】:How to remove location, sourcefile and linenumber from Angular 6's auto generated XLIFF file?如何从 Angular 6 的自动生成的 XLIFF 文件中删除位置、源文件和行号?
【发布时间】:2019-07-14 07:04:19
【问题描述】:

我们正在使用 Angular 的内置工具从模板中提取消息。

这很好用,我们正在使用 XLIFF 文件获取所有信息:

ng xi18n

在此文件中,trans-unit 如下所示:

<trans-unit id="3535a6c8296b457542000d2cbd15ca93f86cdd79" datatype="html">
    <source>Homepage</source>
    <context-group purpose="location">
      <context context-type="sourcefile">app/component/nav/nav.component.html</context>
      <context context-type="linenumber">39</context>
    </context-group>
    <note priority="1" from="description">description</note>
    <note priority="1" from="meaning">title</note>
</trans-unit>

尽管&lt;context-group purpose="location"&gt; 中的内容看起来很有趣,但它向外部翻译服务解释了项目和实施细节。

有没有办法告诉 Angular不要将这些信息包含到 XLIFF 文件中?

或者,是否有其他工具可以进行转换?也许编译器在构建期间拥有该信息很重要。

【问题讨论】:

    标签: angular internationalization angular6 xliff angular8


    【解决方案1】:

    以下 Angular 的代码会生成这些行(查看Github 上的完整源代码):

    const _CONTEXT_GROUP_TAG = 'context-group';
    const _CONTEXT_TAG = 'context';
    
    // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
    // http://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html
    export class Xliff extends Serializer {
      write(messages: i18n.Message[], locale: string|null): string {
        const visitor = new _WriteVisitor();
        const transUnits: xml.Node[] = [];
    
        messages.forEach(message => {
          let contextTags: xml.Node[] = [];
          message.sources.forEach((source: i18n.MessageSpan) => {
            let contextGroupTag = new xml.Tag(_CONTEXT_GROUP_TAG, {purpose: 'location'});
            contextGroupTag.children.push(
                new xml.CR(10),
                new xml.Tag(
                    _CONTEXT_TAG, {'context-type': 'sourcefile'}, [new xml.Text(source.filePath)]),
                new xml.CR(10), new xml.Tag(
                                    _CONTEXT_TAG, {'context-type': 'linenumber'},
                                    [new xml.Text(`${source.startLine}`)]),
                new xml.CR(8));
            contextTags.push(new xml.CR(8), contextGroupTag);
          });
    
          [...]
    

    根据我的代码分析,似乎没有办法告诉 Angular 不将这些信息包含到 XLIFF 文件中。 但是这些生成的行似乎没有在其他地方使用。我想这只是为了与XLIFF specs一致。

    您可能可以使用脚本(如@Maryannah 的脚本)将其删除,而不会破坏 Angular。

    【讨论】:

    【解决方案2】:

    你不应该删除那些,因为 Angular 需要它来翻译。

    但如果是你想要的,你可以使用 JS 脚本来删除它们:

    const fs = require('fs');
    const data = fs.readFileSync('./i18n.xliff').toString();
    const contentToRemoveRegexp = /<context-group purpose="location">([.\s\S])*<\/context-group>/g;
    const replaced = data.replace(contentToRemoveRegexp, '');
    fs.writeFileSync('./i18n-updated.xliff', replaced);
    

    将它存储在您想要的任何 JS 文件中,并在您的包文件中创建一个脚本:

    "i18n:update": "ng xi18n && node path/to/your/script/js"
    

    【讨论】:

    • 感谢您的回答。您是否有指向这些信息的链接,这些行是 Angular 正确编译所必需的?
    • 感谢建议的脚本。但是,如果您说这些行是成功构建所必需的,我想它们必须存在于最终的原始文件和所有翻译文件中。所以简单地删除内容是行不通的,我们需要一个在有和没有context-group 的版本之间转换的脚本。对吗?
    • 不,对不起,我没有链接,这是我自己的声明:我敢肯定,如果你删除它们,Angular 将无法翻译,因为它们告诉它在哪里可以找到翻译 !但是您的问题似乎是XY problem:您为什么首先要这样做?
    • 我们还使用脚本从 xlf 文件中删除所有上下文,对于我们来说,构建过程缺少上下文没有问题,我们有几千个翻译。当翻译人员开始将 html 标记添加到未准备附加标记的翻译时,通常会出现更大的问题。我们还按来源价值对翻译进行排序,这样在进行 PR 审查时更容易检查。
    • 稍微修改了正则表达式以清理额外的多行,您可以在此处查看代码:https://github.com/OpenMAVN/MAVN.FrontEnd.BackOffice/blob/master/i18n-clear.js
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多