ngx-i18nsupport-lib
Version:
A Typescript library to work with Angular generated i18n files (xliff, xmb)
669 lines • 42.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var api_1 = require("../api");
var fs = require("fs");
var dom_utilities_1 = require("./dom-utilities");
var xmldom_1 = require("xmldom");
/**
* Created by martin on 28.04.2017.
* Testcases for xliff 1.2 files.
*/
describe('ngx-i18nsupport-lib xliff 1.2 test spec', function () {
var SRCDIR = 'test/testdata/i18n/';
var ENCODING = 'UTF-8';
/**
* Helper function to read Xliff from File
* @type {string}
*/
function readFile(path) {
var content = fs.readFileSync(path, ENCODING);
return api_1.TranslationMessagesFileFactory.fromFileContent('xlf', content, path, ENCODING);
}
describe('xlf 1.2 format tests', function () {
var MASTER1SRC = SRCDIR + 'ngExtractedMaster1.xlf';
var TRANSLATED_FILE_SRC = SRCDIR + 'translatedFile.xlf';
var ID_TRANSLATED_SCHLIESSEN = '1ead0ad1063d0c9e005fe56c9529aef4c1ef9d21'; // an ID from ngExtractedMaster1.xlf
var ID_WITH_PLACEHOLDER = 'af0819ea4a5db68737ebcabde2f5e432b66352e8';
var ID_WITH_REPEATED_PLACEHOLDER = 'af0819ea4a5db68737ebcabde2f5e432b663repl';
var ID_WITH_PLACEHOLDER_2 = 'af0819ea4a5db68737ebcabde2f5e432b66352e8xxx'; // same with </x> tags
var ID_WITH_MEANING_AND_DESCRIPTION = '84e8cd8ba480129d90f512cc3462bb43efcf389f';
var ID_WITH_NO_SOURCEREFS = 'no_sourceref_test'; // an ID with no source elements
var ID_WITH_ONE_SOURCEREF = '57e605bfa130afb4de4ee40e496e854a9e8a28a7';
var ID_WITH_TWO_SOURCEREFS = '78eab955529ba0f1817c84991d9175f55bfdf937'; // an ID with 2 source elements
var ID_WITH_TAGS = '7e8dd1fd1c57afafc38550ce80b5bcc1ced49f85';
var ID_WITH_TAGS_2 = '7e8dd1fd1c57afafc38550ce80b5bcc1ced49f85xxx'; // same with </x> tags
var ID_UNTRANSLATED_DESCRIPTION = 'a52ba049c16778bdb2e5a19a41acaadf87b104dc';
var ID_TO_MERGE = 'unittomerge';
var ID_ICU_PLURAL = 'efec69fdcf74bd6d640b2a771558b7b09e271c28';
var ID_ICU_SELECT = '8967ef1e10bdf8ea6d8e65ba7ffe7fc69960d1da';
var ID_ICU_EMBEDDED_TAGS = '304b4d798bf51257538949844e121724110d37ed';
var ID_CONTAINS_ICU = '1f3c670be000dbb6cbe05353d12ef62793d91fec';
var ID_CONTAINS_TWO_ICU = 'complextags.icuTwoICU';
var ID_WITH_BR_TAG = 'e05aa009b849e0a5725e819b14c77a32576513a8';
var ID_WITH_IMG_TAG = 'ea7a3dece8868782142a70b3e1c8b064f6027e20';
it('should read xlf file', function () {
var file = readFile(MASTER1SRC);
expect(file).toBeTruthy();
expect(file.fileType()).toBe('XLIFF 1.2');
var tu = file.transUnitWithId(ID_TRANSLATED_SCHLIESSEN);
expect(tu).toBeTruthy();
expect(tu.sourceContent()).toBe('Schließen');
});
it('should read xlf file and pretty print it', function () {
var file = readFile(MASTER1SRC);
expect(file).toBeTruthy();
expect(file.editedContent()).toContain('<source>Beschreibung zu <x id="INTERPOLATION"/> (<x id="INTERPOLATION_1"/>)</source>');
expect(file.editedContent(true)).toContain(" <source>Beschreibung zu <x id=\"INTERPOLATION\"/> (<x id=\"INTERPOLATION_1\"/>)</source>");
});
it('should not add empty lines when beautifying (issue ngx-i18nsupport #97)', function () {
var file = readFile(MASTER1SRC);
expect(file).toBeTruthy();
var editedContentBeautified = file.editedContent(true);
var file2 = api_1.TranslationMessagesFileFactory.fromFileContent('xlf', editedContentBeautified, null, ENCODING);
var editedContentBeautifiedAgain = file2.editedContent(true);
expect(editedContentBeautifiedAgain).toMatch(/Beschreibung zu <x/);
expect(editedContentBeautifiedAgain).not.toMatch(/Beschreibung zu\s*\r\n?/);
});
it('should emit warnings', function () {
var file = readFile(MASTER1SRC);
expect(file.warnings().length).toBe(1);
expect(file.warnings()[0]).toContain('trans-unit without "id"');
});
it('should count units', function () {
var file = readFile(MASTER1SRC);
expect(file.numberOfTransUnits()).toBe(28);
expect(file.numberOfTransUnitsWithMissingId()).toBe(1);
expect(file.numberOfUntranslatedTransUnits()).toBe(file.numberOfTransUnits());
expect(file.numberOfReviewedTransUnits()).toBe(0);
});
it('should return source language', function () {
var file = readFile(MASTER1SRC);
expect(file.sourceLanguage()).toBe('en');
});
it('should change source language', function () {
var file = readFile(MASTER1SRC);
expect(file.sourceLanguage()).toBe('en');
file.setSourceLanguage('de');
expect(file.sourceLanguage()).toBe('de');
});
it('should return target language', function () {
var file = readFile(MASTER1SRC);
expect(file.targetLanguage()).toBeFalsy();
var translatedFile = readFile(TRANSLATED_FILE_SRC);
expect(translatedFile.targetLanguage()).toBe('de');
});
it('should change target language', function () {
var file = readFile(TRANSLATED_FILE_SRC);
expect(file.targetLanguage()).toBe('de');
file.setTargetLanguage('suahel');
expect(file.targetLanguage()).toBe('suahel');
});
it('should loop over all trans units', function () {
var translatedFile = readFile(TRANSLATED_FILE_SRC);
var count = 0;
translatedFile.forEachTransUnit(function (tu) {
expect(tu).toBeTruthy();
count++;
});
expect(count).toBeGreaterThan(30);
});
it('should normalize source of every trans units', function () {
var file = readFile(MASTER1SRC);
var count = 0;
file.forEachTransUnit(function (tu) {
expect(tu.sourceContentNormalized()).toBeTruthy();
count++;
});
});
it('should change source content', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_BR_TAG);
expect(tu).toBeTruthy();
expect(tu.supportsSetSourceContent()).toBeTruthy();
var changedMessage = tu.sourceContent() + 'a changed source content';
tu.setSourceContent(changedMessage);
expect(tu.sourceContent()).toBe(changedMessage);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_BR_TAG);
expect(tu2.sourceContent()).toBe(changedMessage);
});
it('should read meaning and description of tu', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.meaning()).toBe('dateservice.monday');
expect(tu.description()).toBe('ngx-translate');
});
it('should change description', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.description()).toBeTruthy();
expect(tu.supportsSetDescriptionAndMeaning()).toBeTruthy();
var changedMessage = 'a changed description';
tu.setDescription(changedMessage);
expect(tu.description()).toBe(changedMessage);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu2.description()).toBe(changedMessage);
});
it('should set description (creates new description)', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_BR_TAG); // anyone without description
expect(tu).toBeTruthy();
expect(tu.description()).toBeFalsy();
expect(tu.supportsSetDescriptionAndMeaning()).toBeTruthy();
var changedMessage = 'a changed description';
tu.setDescription(changedMessage);
expect(tu.description()).toBe(changedMessage);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_BR_TAG);
expect(tu.description()).toBe(changedMessage);
var xmlElem = tu2.asXmlElement();
var noteElem = dom_utilities_1.DOMUtilities.getFirstElementByTagName(xmlElem, 'note');
expect(noteElem).toBeTruthy();
expect(noteElem.getAttribute('priority')).toBe('1');
});
it('should remove description', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.description()).toBeTruthy();
expect(tu.supportsSetDescriptionAndMeaning()).toBeTruthy();
tu.setDescription(null);
expect(tu.description()).toBeFalsy();
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu2.description()).toBeFalsy();
});
it('should change meaning', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.meaning()).toBeTruthy();
expect(tu.supportsSetDescriptionAndMeaning()).toBeTruthy();
var changedMessage = 'a changed description';
tu.setMeaning(changedMessage);
expect(tu.meaning()).toBe(changedMessage);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu2.meaning()).toBe(changedMessage);
});
it('should set meaning (creates new meaning)', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_BR_TAG); // anyone without description
expect(tu).toBeTruthy();
expect(tu.meaning()).toBeFalsy();
expect(tu.supportsSetDescriptionAndMeaning()).toBeTruthy();
var changedMessage = 'a changed description';
tu.setMeaning(changedMessage);
expect(tu.meaning()).toBe(changedMessage);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_BR_TAG);
expect(tu2.meaning()).toBe(changedMessage);
});
it('should remove meaning', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.meaning()).toBeTruthy();
expect(tu.supportsSetDescriptionAndMeaning()).toBeTruthy();
tu.setMeaning(null);
expect(tu.meaning()).toBeFalsy();
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_MEANING_AND_DESCRIPTION);
expect(tu2.meaning()).toBeFalsy();
});
it('should return empty source references array if source not set', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_NO_SOURCEREFS);
expect(tu).toBeTruthy();
expect(tu.sourceReferences().length).toBe(0);
});
it('should return source reference', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_ONE_SOURCEREF);
expect(tu).toBeTruthy();
expect(tu.sourceReferences().length).toBe(1);
expect(tu.sourceReferences()[0].sourcefile).toBe('S:/experimente/sampleapp41/src/app/app.component.ts');
expect(tu.sourceReferences()[0].linenumber).toBe(10);
});
it('should return more than one source references', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu).toBeTruthy();
expect(tu.sourceReferences().length).toBe(2);
expect(tu.sourceReferences()[0].sourcefile).toBe('S:/experimente/sampleapp41/src/app/app.component.ts');
expect(tu.sourceReferences()[0].linenumber).toBe(20);
expect(tu.sourceReferences()[1].sourcefile).toBe('S:/experimente/sampleapp41/src/app/app.component.ts');
expect(tu.sourceReferences()[1].linenumber).toBe(21);
});
it('should set source references', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
expect(tu.sourceReferences().length).toBe(0);
tu.setSourceReferences([{ sourcefile: 'x', linenumber: 10 }, { sourcefile: 'y', linenumber: 20 }]);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_TO_MERGE);
expect(tu2.sourceReferences().length).toBe(2);
expect(tu2.sourceReferences()[0].sourcefile).toBe('x');
expect(tu2.sourceReferences()[0].linenumber).toBe(10);
expect(tu2.sourceReferences()[1].sourcefile).toBe('y');
expect(tu2.sourceReferences()[1].linenumber).toBe(20);
});
it('should override source references', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu).toBeTruthy();
expect(tu.supportsSetSourceReferences()).toBeTruthy();
expect(tu.sourceReferences().length).toBe(2);
tu.setSourceReferences([{ sourcefile: 'x:komisch', linenumber: 10 }]);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu2.sourceReferences().length).toBe(1);
expect(tu2.sourceReferences()[0].sourcefile).toBe('x:komisch');
expect(tu2.sourceReferences()[0].linenumber).toBe(10);
});
it('should not change source reference when translating', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu).toBeTruthy();
expect(tu.sourceReferences().length).toBe(2);
tu.translate('a translated value');
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu2.targetContent()).toBe('a translated value');
expect(tu2.sourceReferences().length).toBe(2);
expect(tu2.sourceReferences()[0].sourcefile).toBe('S:/experimente/sampleapp41/src/app/app.component.ts');
expect(tu2.sourceReferences()[0].linenumber).toBe(20);
expect(tu2.sourceReferences()[1].sourcefile).toBe('S:/experimente/sampleapp41/src/app/app.component.ts');
expect(tu2.sourceReferences()[1].linenumber).toBe(21);
});
it('should run through 3 different states while translating', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_PLACEHOLDER);
expect(tu).toBeTruthy();
expect(tu.targetState()).toBe(api_1.STATE_NEW);
tu.translate('a translation');
expect(tu.targetState()).toBe(api_1.STATE_TRANSLATED);
tu.setTargetState(api_1.STATE_FINAL);
expect(tu.targetState()).toBe(api_1.STATE_FINAL);
});
it('should normalize placeholders to {{0}} etc', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_PLACEHOLDER);
expect(tu.targetContentNormalized().asDisplayString()).toBe('Eintrag {{0}} von {{1}} hinzugefügt.');
var tu2 = file.transUnitWithId(ID_WITH_PLACEHOLDER_2);
expect(tu2.targetContentNormalized().asDisplayString()).toBe('Eintrag {{0}} von {{1}} hinzugefügt.');
});
it('should normalize repeated placeholders to {{0}} {{1}} etc', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_REPEATED_PLACEHOLDER);
expect(tu.targetContentNormalized().asDisplayString()).toBe('{{0}}: Eine Nachricht mit 2 Platzhaltern: {{0}} {{1}}');
});
it('should normalize embedded html tags', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_TAGS);
expect(tu.targetContentNormalized().asDisplayString()).toBe('Dieser Text enthält <b>eingebettetes html</b>');
var tu2 = file.transUnitWithId(ID_WITH_TAGS_2);
expect(tu2.targetContentNormalized().asDisplayString()).toBe('Dieser Text enthält <b>eingebettetes html</b>');
});
it('should normalize empty html tag br', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_BR_TAG);
expect(tu.sourceContentNormalized().asDisplayString()).toBe('Dieser Text enthält<br>einen Zeilenumbruch per HTML-br-Element.');
var translation = tu.sourceContentNormalized().translate('This text contains<br> a linebreak');
tu.translate(translation);
expect(tu.targetContent()).toBe('This text contains<x id="LINE_BREAK" ctype="lb" equiv-text="<br/>"/> a linebreak');
});
it('should normalize empty html tag img', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_IMG_TAG);
expect(tu.sourceContentNormalized().asDisplayString()).toBe('Dieser Text enthält ein Bild <img> mitt en in der Nachricht');
var translation = tu.sourceContentNormalized().translate('This text contains an img <img> in the message');
tu.translate(translation);
expect(tu.targetContent()).toBe('This text contains an img <x id="TAG_IMG" ctype="image" equiv-text="<img/>"/> in the message');
});
it('should remove a transunit by id', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu).toBeTruthy();
file.removeTransUnitWithId(ID_WITH_TWO_SOURCEREFS);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_WITH_TWO_SOURCEREFS);
expect(tu2).toBeFalsy(); // should not exist any more
});
it('should translate source without or with target', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TRANSLATED_SCHLIESSEN);
expect(tu).toBeTruthy();
expect(tu.targetContent()).toBeFalsy();
// first translate
tu.translate('Anwendung läuft');
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_TRANSLATED_SCHLIESSEN);
expect(tu2.targetContentNormalized().asDisplayString()).toBe('Anwendung läuft');
expect(tu2.targetState()).toBe(api_1.STATE_TRANSLATED);
// translate again
tu2.translate('Anwendung funktioniert');
var file3 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file2.editedContent(), null, null);
var tu3 = file3.transUnitWithId(ID_TRANSLATED_SCHLIESSEN);
expect(tu3.targetContentNormalized().asDisplayString()).toBe('Anwendung funktioniert');
expect(tu3.targetState()).toBe(api_1.STATE_TRANSLATED);
});
it('should copy source to target for default lang', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.targetContent()).toBeFalsy();
var isDefaultLang = true;
var copyContent = false;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tu2 = file2.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu2.targetContentNormalized().asDisplayString()).toBe('Beschreibung zu {{0}} ({{1}})');
});
it('should copy source to target for non default lang if wanted', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.targetContent()).toBeFalsy();
var isDefaultLang = false;
var copyContent = true;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tu2 = file2.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu2.targetContentNormalized().asDisplayString()).toBe('Beschreibung zu {{0}} ({{1}})');
});
it('should copy source to target for non default lang and set state new, native state initial, #57', function () {
var file = readFile(MASTER1SRC);
var isDefaultLang = false;
var copyContent = true;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tu2 = file2.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu2.targetState()).toBe(api_1.STATE_NEW);
expect(tu2.nativeTargetState()).toBe('new');
});
it('should not copy source to target for non default lang if not wanted', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu).toBeTruthy();
expect(tu.targetContent()).toBeFalsy();
var isDefaultLang = false;
var copyContent = false;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tu2 = file2.transUnitWithId(ID_UNTRANSLATED_DESCRIPTION);
expect(tu2.targetContent()).toBeFalsy();
});
it('should put new target element directy behind source element, #50', function () {
var ID_NO_TARGET = 'a52ba049c16778bdb2e5a19a41acaadf87b10001';
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_NO_TARGET);
expect(tu).toBeTruthy();
expect(tu.targetContent()).toBeFalsy();
var isDefaultLang = false;
var copyContent = true;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tu2 = file2.transUnitWithId(ID_NO_TARGET);
expect(tu2.targetContent()).toBeTruthy();
// check the xml of file that target directly follows source
var document = new xmldom_1.DOMParser().parseFromString(file2.editedContent(), 'text/xml');
var targetList = document.getElementsByTagName('target');
for (var i = 0; i < targetList.length; i++) {
var targetNode = targetList.item(i);
var sibling = dom_utilities_1.DOMUtilities.getPreviousElementSibling(targetNode);
var failText = 'found target element without sibling, id ' + targetNode.toString();
expect(sibling !== null).toBeTruthy(failText);
expect(sibling.localName).toBe('source');
}
});
it('should copy a transunit from file a to file b', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeFalsy();
var newTu = targetFile.importNewTransUnit(tu, false, true);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeTruthy();
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toEqual(newTu);
var changedTargetFile = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(targetFile.editedContent(), null, null);
var targetTu = changedTargetFile.transUnitWithId(ID_TO_MERGE);
expect(targetTu.sourceContent()).toBe('Test for merging units');
expect(targetTu.targetContent()).toBe('Test for merging units');
});
it('should copy a transunit from file a to file b and leave content blank (xliffmerge #103)', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeFalsy();
// flag copyContent set to false here...
var newTu = targetFile.importNewTransUnit(tu, false, false);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeTruthy();
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toEqual(newTu);
var changedTargetFile = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(targetFile.editedContent(), null, null);
var targetTu = changedTargetFile.transUnitWithId(ID_TO_MERGE);
expect(targetTu.sourceContent()).toBe('Test for merging units');
expect(targetTu.targetContent()).toBe('');
});
it('should copy a transunit to a specified position (#53)', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeFalsy();
var ID_EXISTING = 'f540f05dc71be88e226a3920dbf1140b2658e5ea';
var existingTu = targetFile.transUnitWithId(ID_EXISTING);
expect(existingTu).toBeTruthy();
var newTu = targetFile.importNewTransUnit(tu, false, true, existingTu);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeTruthy();
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toEqual(newTu);
var doc = new xmldom_1.DOMParser().parseFromString(targetFile.editedContent());
var existingElem = dom_utilities_1.DOMUtilities.getElementByTagNameAndId(doc, 'trans-unit', ID_EXISTING);
var newElem = dom_utilities_1.DOMUtilities.getElementByTagNameAndId(doc, 'trans-unit', ID_TO_MERGE);
expect(dom_utilities_1.DOMUtilities.getElementFollowingSibling(existingElem)).toEqual(newElem);
var changedTargetFile = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(targetFile.editedContent(), null, null);
var targetTu = changedTargetFile.transUnitWithId(ID_TO_MERGE);
expect(targetTu.sourceContent()).toBe('Test for merging units');
});
it('should copy a transunit to first position (#53)', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeFalsy();
// when importNewTransUnit is called with null, new unit will be added at first position
var newTu = targetFile.importNewTransUnit(tu, false, true, null);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeTruthy();
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toEqual(newTu);
var doc = new xmldom_1.DOMParser().parseFromString(targetFile.editedContent());
var newElem = dom_utilities_1.DOMUtilities.getElementByTagNameAndId(doc, 'trans-unit', ID_TO_MERGE);
expect(newElem).toBeTruthy();
expect(dom_utilities_1.DOMUtilities.getElementPrecedingSibling(newElem)).toBeFalsy();
var changedTargetFile = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(targetFile.editedContent(), null, null);
var targetTu = changedTargetFile.transUnitWithId(ID_TO_MERGE);
expect(targetTu.sourceContent()).toBe('Test for merging units');
});
it('should copy source to target and set a praefix and suffix', function () {
var file = readFile(MASTER1SRC);
file.setNewTransUnitTargetPraefix('%%');
file.setNewTransUnitTargetSuffix('!!');
var isDefaultLang = false;
var copyContent = true;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tu2 = file2.transUnitWithId(ID_TO_MERGE);
expect(tu2.targetState()).toBe(api_1.STATE_NEW);
expect(tu2.targetContent()).toBe('%%Test for merging units!!');
});
it('should copy source to target, but should not set a praefix and suffix for ICU messages', function () {
var file = readFile(MASTER1SRC);
file.setNewTransUnitTargetPraefix('%%');
file.setNewTransUnitTargetSuffix('!!');
var isDefaultLang = false;
var copyContent = true;
var file2 = file.createTranslationFileForLang('xy', null, isDefaultLang, copyContent);
var tuICU = file2.transUnitWithId(ID_ICU_PLURAL);
expect(tuICU.targetState()).toBe(api_1.STATE_NEW);
expect(tuICU.targetContent()).not.toContain('%%');
expect(tuICU.targetContent()).not.toContain('!!');
});
it('should copy a transunit from file a to file b and set a praefix and suffix', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
targetFile.setNewTransUnitTargetPraefix('%%');
targetFile.setNewTransUnitTargetSuffix('!!');
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeFalsy();
var newTu = targetFile.importNewTransUnit(tu, false, true);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeTruthy();
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toEqual(newTu);
var changedTargetFile = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(targetFile.editedContent(), null, null);
var targetTu = changedTargetFile.transUnitWithId(ID_TO_MERGE);
expect(targetTu.targetContent()).toBe('%%Test for merging units!!');
});
it('should copy a transunit from file a to file b, but should not set a praefix and suffix for ICU messages', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_ICU_SELECT);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
targetFile.setNewTransUnitTargetPraefix('%%');
targetFile.setNewTransUnitTargetSuffix('!!');
expect(targetFile.transUnitWithId(ID_ICU_SELECT)).toBeFalsy();
var newTu = targetFile.importNewTransUnit(tu, false, true);
expect(targetFile.transUnitWithId(ID_ICU_SELECT)).toBeTruthy();
expect(targetFile.transUnitWithId(ID_ICU_SELECT)).toEqual(newTu);
var changedTargetFile = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(targetFile.editedContent(), null, null);
var targetTu = changedTargetFile.transUnitWithId(ID_ICU_SELECT);
expect(targetTu.targetContent()).not.toContain('%%');
expect(targetTu.targetContent()).not.toContain('!!');
});
it('should preserve line end at end of file while editing', function () {
var content = fs.readFileSync(MASTER1SRC, ENCODING);
expect(content.endsWith('\n')).toBeTruthy('Master should end with EOL');
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TO_MERGE);
expect(tu).toBeTruthy();
var targetFile = readFile(TRANSLATED_FILE_SRC);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeFalsy();
targetFile.importNewTransUnit(tu, false, true);
expect(targetFile.transUnitWithId(ID_TO_MERGE)).toBeTruthy();
expect(targetFile.editedContent().endsWith('\n')).toBeTruthy('Edited content should end with EOL');
});
it('should translate using NormalizedMessage (plain text case, no placeholders, no markup)', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_TRANSLATED_SCHLIESSEN);
expect(tu).toBeTruthy();
var translationString = 'Anwendung läuft';
// first translate
var translation = tu.sourceContentNormalized().translate(translationString);
tu.translate(translation);
expect(tu.targetContent()).toBe(translationString);
var file2 = api_1.TranslationMessagesFileFactory.fromUnknownFormatFileContent(file.editedContent(), null, null);
var tu2 = file2.transUnitWithId(ID_TRANSLATED_SCHLIESSEN);
expect(tu2.targetContentNormalized().asDisplayString()).toBe(translationString);
});
it('should contain ICU reference in sourceContentNormalized', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_CONTAINS_ICU);
expect(tu).toBeTruthy();
expect(tu.sourceContent()).toBe('Zum Wert <x id="INTERPOLATION"/> gehört der Text <x id="ICU"/>');
expect(tu.sourceContentNormalized().asDisplayString()).toBe('Zum Wert {{0}} gehört der Text <ICU-Message-Ref_0/>');
});
it('should contain 2 ICU references in sourceContentNormalized', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_CONTAINS_TWO_ICU);
expect(tu).toBeTruthy();
expect(tu.sourceContent()).toBe('first: <x id="ICU"/>, second <x id="ICU_1"/>');
expect(tu.sourceContentNormalized().asDisplayString()).toBe('first: <ICU-Message-Ref_0/>, second <ICU-Message-Ref_1/>');
});
it('should handle plural ICU', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_ICU_PLURAL);
expect(tu).toBeTruthy();
var normalizedMessage = tu.sourceContentNormalized();
expect(normalizedMessage.asDisplayString()).toBe('<ICU-Message/>');
var icuMessage = normalizedMessage.getICUMessage();
expect(icuMessage).toBeTruthy();
expect(icuMessage.isPluralMessage()).toBeTruthy();
expect(icuMessage.isSelectMessage()).toBeFalsy();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[0].getCategory()).toBe('=0');
expect(icuMessage.getCategories()[0].getMessageNormalized().asDisplayString()).toBe('kein Schaf');
expect(icuMessage.getCategories()[1].getCategory()).toBe('=1');
expect(icuMessage.getCategories()[1].getMessageNormalized().asDisplayString()).toBe('1 Schaf');
expect(icuMessage.getCategories()[2].getCategory()).toBe('other');
expect(icuMessage.getCategories()[2].getMessageNormalized().asDisplayString()).toBe('x Schafe');
});
it('should translate plural ICU', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_ICU_PLURAL);
expect(tu).toBeTruthy();
var normalizedMessage = tu.sourceContentNormalized();
expect(normalizedMessage.asDisplayString()).toBe('<ICU-Message/>');
var translatedMessage = normalizedMessage.translateICUMessage({ '=0': 'nothing' });
tu.translate(translatedMessage);
var icuMessage = tu.targetContentNormalized().getICUMessage();
expect(icuMessage).toBeTruthy();
expect(icuMessage.isPluralMessage()).toBeTruthy();
expect(icuMessage.isSelectMessage()).toBeFalsy();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[0].getCategory()).toBe('=0');
expect(icuMessage.getCategories()[0].getMessageNormalized().asDisplayString()).toBe('nothing');
expect(tu.targetContent()).toBe('{VAR_PLURAL, plural, =0 {nothing} =1 {1 Schaf} other {x Schafe}}');
});
it('should handle select ICU message', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_ICU_SELECT);
var normalizedMessage = tu.sourceContentNormalized();
expect(normalizedMessage.asDisplayString()).toBe('<ICU-Message/>');
var icuMessage = normalizedMessage.getICUMessage();
expect(icuMessage).toBeTruthy();
expect(icuMessage.isPluralMessage()).toBeFalsy();
expect(icuMessage.isSelectMessage()).toBeTruthy();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[0].getCategory()).toBe('wert0');
expect(icuMessage.getCategories()[0].getMessageNormalized().asDisplayString()).toBe('wert0 wurde gewählt');
expect(icuMessage.getCategories()[1].getCategory()).toBe('wert1');
expect(icuMessage.getCategories()[1].getMessageNormalized().asDisplayString()).toBe('ein anderer Wert (wert1) wurde gewählt');
expect(icuMessage.getCategories()[2].getCategory()).toBe('wert2');
expect(icuMessage.getCategories()[2].getMessageNormalized().asDisplayString()).toBe('was ganz anderes wurde gewählt');
});
it('should handle ICU with embedded tags', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_ICU_EMBEDDED_TAGS);
var normalizedMessage = tu.sourceContentNormalized();
expect(normalizedMessage.asDisplayString()).toBe('<ICU-Message/>');
var icuMessage = normalizedMessage.getICUMessage();
expect(icuMessage).toBeTruthy();
expect(icuMessage.isPluralMessage()).toBeFalsy();
expect(icuMessage.isSelectMessage()).toBeTruthy();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[0].getCategory()).toBe('wert0');
expect(icuMessage.getCategories()[0].getMessageNormalized().asDisplayString()).toBe('wert0 ausgewählt');
expect(icuMessage.getCategories()[1].getCategory()).toBe('wert1');
expect(icuMessage.getCategories()[1].getMessageNormalized().asDisplayString()).toBe('ein <b>anderer</b> Wert (wert1) ausgewählt');
expect(icuMessage.getCategories()[2].getCategory()).toBe('wert2');
expect(icuMessage.getCategories()[2].getMessageNormalized().asDisplayString()).toBe('was <em>ganz anderes</em> wurde ausgewählt');
});
it('should translate ICU with embedded tags', function () {
var file = readFile(MASTER1SRC);
var tu = file.transUnitWithId(ID_ICU_EMBEDDED_TAGS);
var normalizedMessage = tu.sourceContentNormalized();
expect(normalizedMessage.asDisplayString()).toBe('<ICU-Message/>');
var translatedMessage = normalizedMessage.translateICUMessage({ 'wert1': '<em>changed</em>' });
tu.translate(translatedMessage);
var icuMessage = tu.targetContentNormalized().getICUMessage();
expect(icuMessage).toBeTruthy();
expect(icuMessage.isPluralMessage()).toBeFalsy();
expect(icuMessage.isSelectMessage()).toBeTruthy();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[1].getCategory()).toBe('wert1');
expect(icuMessage.getCategories()[1].getMessageNormalized().asDisplayString()).toBe('<em>changed</em>');
expect(tu.targetContent()).toContain('wert1 {<x id="START_EMPHASISED_TEXT" ctype="x-em" equiv-text="<em>"/>changed<x id="CLOSE_EMPHASISED_TEXT" ctype="x-em"/>}');
// TODO find warnings in embedded message, known limitation in the moment.
// const warnings = icuMessage.getCategories()[1].getMessageNormalized().validateWarnings();
// expect(warnings).toBeTruthy();
});
});
});
//# sourceMappingURL=S:/experimente/ngx-i18nsupport-lib/src/impl/xliff-file.spec.js.map