ngx-i18nsupport-lib
Version:
A Typescript library to work with Angular generated i18n files (xliff, xmb)
82 lines • 4.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var xliff_file_1 = require("../impl/xliff-file");
var xmb_file_1 = require("../impl/xmb-file");
var util_1 = require("util");
var xliff2_file_1 = require("../impl/xliff2-file");
var constants_1 = require("./constants");
var xtb_file_1 = require("../impl/xtb-file");
/**
* Helper class to read translation files depending on format.
* This is part of the public api
*/
var TranslationMessagesFileFactory = /** @class */ (function () {
function TranslationMessagesFileFactory() {
}
/**
* Read file function, result depends on format, either XliffFile or XmbFile.
* @param i18nFormat currently 'xlf' or 'xlf2' or 'xmb' or 'xtb' are supported
* @param xmlContent the file content
* @param path the path of the file (only used to remember it)
* @param encoding utf-8, ... used to parse XML.
* This is read from the file, but if you know it before, you can avoid reading the file twice.
* @param optionalMaster in case of xmb the master file, that contains the original texts.
* (this is used to support state infos, that are based on comparing original with translated version)
* Ignored for other formats.
* @return {ITranslationMessagesFile} either XliffFile or XmbFile
*/
TranslationMessagesFileFactory.fromFileContent = function (i18nFormat, xmlContent, path, encoding, optionalMaster) {
if (i18nFormat === constants_1.FORMAT_XLIFF12) {
return new xliff_file_1.XliffFile(xmlContent, path, encoding);
}
if (i18nFormat === constants_1.FORMAT_XLIFF20) {
return new xliff2_file_1.Xliff2File(xmlContent, path, encoding);
}
if (i18nFormat === constants_1.FORMAT_XMB) {
return new xmb_file_1.XmbFile(xmlContent, path, encoding);
}
if (i18nFormat === constants_1.FORMAT_XTB) {
return new xtb_file_1.XtbFile(xmlContent, path, encoding, optionalMaster);
}
throw new Error(util_1.format('oops, unsupported format "%s"', i18nFormat));
};
/**
* Read file function for any file with unknown format.
* This functions tries to guess the format based on the filename and the content of the file.
* Result depends on detected format, either XliffFile or XmbFile.
* @param xmlContent the file content
* @param path the path of the file (only used to remember it)
* @param encoding utf-8, ... used to parse XML.
* This is read from the file, but if you know it before, you can avoid reading the file twice.
* @param optionalMaster in case of xmb the master file, that contains the original texts.
* (this is used to support state infos, that are based on comparing original with translated version)
* Ignored for other formats.
* @return {ITranslationMessagesFile} either XliffFile or XmbFile
*/
TranslationMessagesFileFactory.fromUnknownFormatFileContent = function (xmlContent, path, encoding, optionalMaster) {
var formatCandidates = [constants_1.FORMAT_XLIFF12, constants_1.FORMAT_XLIFF20, constants_1.FORMAT_XMB, constants_1.FORMAT_XTB];
if (path && path.endsWith('xmb')) {
formatCandidates = [constants_1.FORMAT_XMB, constants_1.FORMAT_XTB, constants_1.FORMAT_XLIFF12, constants_1.FORMAT_XLIFF20];
}
if (path && path.endsWith('xtb')) {
formatCandidates = [constants_1.FORMAT_XTB, constants_1.FORMAT_XMB, constants_1.FORMAT_XLIFF12, constants_1.FORMAT_XLIFF20];
}
// try all candidate formats to get the right one
for (var i = 0; i < formatCandidates.length; i++) {
var format_1 = formatCandidates[i];
try {
var translationFile = TranslationMessagesFileFactory.fromFileContent(format_1, xmlContent, path, encoding, optionalMaster);
if (translationFile) {
return translationFile;
}
}
catch (e) {
// seams to be the wrong format
}
}
throw new Error(util_1.format('could not identify file format, it is neiter XLIFF (1.2 or 2.0) nor XMB/XTB'));
};
return TranslationMessagesFileFactory;
}());
exports.TranslationMessagesFileFactory = TranslationMessagesFileFactory;
//# sourceMappingURL=S:/experimente/ngx-i18nsupport-lib/src/api/translation-messages-file-factory.js.map