martinroob / ngx-i18nsupport

Some tooling to be used for Angular i18n workflows
MIT License
283 stars 69 forks source link

State is set back to translated even if source did not change #128

Open hansmaad opened 5 years ago

hansmaad commented 5 years ago

Hi, if I understood correctly, the state should switch from final to translated, when the source changed. If nothing changed, the translation is untouched.

Please check this simple example, which will always set the state back to translated after setting the target state to final.

messages.xlf

<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  <file source-language="de" datatype="plaintext" original="ng2.template">
    <body>
      <trans-unit id="global--formfield-too-large" datatype="html">
        <source>
        Der Wert ist zu groß</source>
        <context-group purpose="location">
          <context context-type="sourcefile">awesome.component.html</context>
          <context context-type="linenumber">2</context>
        </context-group>
      </trans-unit>
    </body>
  </file>
</xliff>

messages.en.xlf

<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  <file datatype="plaintext" date="2018-11-14T14:52:51.3060268Z" original="3223941_9" source-language="de" target-language="en">
    <body>
      <trans-unit id="global--formfield-too-large" datatype="html">
        <source>
        Der Wert ist zu groß</source>
        <target state="final">
        The value is too large</target>
        <context-group purpose="location">
          <context context-type="sourcefile">awesome.component.html</context>
          <context context-type="linenumber">2</context>
        </context-group>
      </trans-unit>
    </body>
  </file>
</xliff>

If you remove whitespace from the source and target, it works as expected:

messages.xlf

<source>Der Wert ist zu groß</source>
<target state="translated">The value is too large</target>

I'm using this profile

{
    "$schema": "./node_modules/ngx-i18nsupport/dist/xliffmerge/configuration-schema.json",
    "xliffmergeOptions": {
      "srcDir": "src/i18n",
      "genDir": "src/i18n",
      "i18nBaseFile": "messages",
      "defaultLanguage": "de",
      "languages": ["de", "en"],
      "targetPraefix": "",
      "targetSuffix": "",
      "beautifyOutput": false,
      "allowIdChange": false,
      "autotranslate": false,
      "apikey": "",
      "apikeyfile": "",
      "verbose": false,
      "quiet": false
    }
  }
hansmaad commented 5 years ago

I figured out that it is a problem of \n in the source file vs. \r\n in the translation file (generated by TinyTranslator on Chrome Win10). The comparison here returns true:
https://github.com/martinroob/ngx-i18nsupport/blob/6a3e151071a5862ed9776a27cedb9d6cd4d129ff/projects/xliffmerge/src/xliffmerge/xliff-merge.ts#L378-L391

As a workaround I'm now using this npm script

"xi18n": "npm run xi18n:crlf && ng xi18n --i18n-locale de --output-path i18n && npm run xi18n:merge",
"xi18n:crlf": "crlf --set=LF src/i18n/*.xlf",
"xi18n:merge": "xliffmerge --profile xliffmerge.json en de"
martinroob commented 5 years ago

Yes, this is clearly a bug. Thanks for already figuring out what the problem is. Will fix it soon.

hansmaad commented 5 years ago

Unfortunately still broken for me.

Check out this example master (with LF). The corresponing translation file has same content but with CRLF.

<?xml version="1.0" encoding="UTF-8" ?>
<!-- issue 128 when source is unchanged but has different line ending translation state should not change -->
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="de" datatype="plaintext" original="ng2.template">
        <body>
            <trans-unit id="issue128" datatype="html">
                <source>
                Erste Zeile.
                Zweite Zeile.
                </source>
                <context-group purpose="location">
                    <context context-type="sourcefile">awesome.component.html</context>
                    <context context-type="linenumber">2</context>
                </context-group>
            </trans-unit>
        </body>
    </file>
</xliff>