How to remove location, sourcefile and linenumber from Angular 6's auto generated XLIFF file?
Asked Answered
T

2

7

We are using Angular's build in tools to extract messages from templates.

This works fine and we are getting all the information in a XLIFF file using:

ng xi18n

In this file a trans-unit looks like this:

<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>

Even though the content in <context-group purpose="location"> looks interesting, it exploids project and implementation details to an external translation service.

Is there a way to tell Angular to not include these information into the XLIFF-file?

Alternatively, are there other tools, that could do the transformation? Maybe it's important for the compiler to have that information during a build.

Tripodic answered 20/2, 2019 at 14:42 Comment(0)
E
5

You should not delete those, as Angular needs it to translate.

But if it's what you want, you can use a JS script to remove them :

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);

store it in whichever JS file you want and make a script in your package file :

"i18n:update": "ng xi18n && node path/to/your/script/js"
Evan answered 25/6, 2019 at 9:43 Comment(8)
Thanks for your answer. Do you have a link to the information, that these lines are necessary for Angular to compile correctly?Tripodic
And thanks for the suggested script. But if you say, that these lines are necessary for a successful build, I guess they must be present in the final original and in all translated files. So simply removing the content, wouldn't work and we need a script that transforms between the version with and without context-group. RIght?Tripodic
No I have no link sorry, it's my own statement : I'm sure that if you remove them, Angular won't be able to translate, as they tell it where to find the placeholder for the translation ! But your issue seems like an XY problem : why would you do that in the first place ?Evan
Well, as I wrote in the question: We are sending these XLIFF files to an external translation service. And these information in context-group exploid/ reveal the whole project and and template structure to a third party. I'm not sure, whether this is a good thing.Tripodic
Well unless they get your application structure, no, it's no big deal. And I mean, you pay them to do that job, if they try to screw you over, you have the high ground ! Furthermore, they're a translation service, they probably use a software dedicated to that, they wouldn't even be able to read such file (let alone understand it ...). Finally, it's plain HTML, that doesn't reveal any business logic or technical loophole, so I would say that you're pretty safe there.Evan
If you really fear that @lampshade, consider extracting all the translations in another file, send it to them, and then apply those translations to your own translated file : this is tideous, hard to automate, but at least you have your mind at ease !Evan
We are also removing all context from xlf files with a script there are no issues with missing context for build process for us and we have few thousand translations. Bigger problem usually occurs when translators start adding html tags to translations that are not prepared for additional tags. We also sort translations by their source value this way its easier to check when doing PR review.Olsson
modified regex a little to clean also extra multilines, you can see code here: https://github.com/OpenMAVN/MAVN.FrontEnd.BackOffice/blob/master/i18n-clear.jsMon
A
4

The following Angular's code generate this lines (check the full source on 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);
      });

      [...]

Based on my code analyse, it seems there is no way to tell Angular to not include these infos into the XLIFF-file. BUT these generated lines doesn't seem to be used elsewhere. I suppose this is just to be in agreement with XLIFF specs.

You probably could remove it with a script (like the one of @Maryannah) without breaking Angular.

Arlo answered 28/6, 2019 at 15:4 Comment(1)
Confirming supposition regarding agreement with XLIFF specs: github.com/angular/angular/issues/37202Adowa

© 2022 - 2024 — McMap. All rights reserved.