@nativescript-community/l
Version:
Native internationalization plugin for NativeScript using native capabilities of each platform
186 lines • 9.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConverterIOS = void 0;
var tslib_1 = require("tslib");
var fs = require("fs");
var path = require("path");
var plist = require("simple-plist");
var placeholder_1 = require("../placeholder");
var converter_common_1 = require("./converter.common");
var plural_common_1 = require("./plural.common");
var resource_ios_1 = require("./resource.ios");
function encodeFormatValue(value) {
return (0, placeholder_1.convertStringSignToAtSign)((0, placeholder_1.convertPlaceholders)(value));
}
function escapeXML(value) {
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
var ConverterIOS = (function (_super) {
tslib_1.__extends(ConverterIOS, _super);
function ConverterIOS() {
return _super !== null && _super.apply(this, arguments) || this;
}
ConverterIOS.prototype.cleanObsoleteResourcesFiles = function (resourcesDirectory, languages) {
var _this = this;
fs.readdirSync(resourcesDirectory)
.filter(function (fileName) {
var match = /^(.+)\.lproj$/.exec(fileName);
return match && !languages.has(match[1]);
})
.map(function (fileName) { return path.join(resourcesDirectory, fileName); })
.filter(function (filePath) { return fs.statSync(filePath).isDirectory(); })
.forEach(function (lngResourcesDir) {
['InfoPlist.strings', 'Localizable.strings', 'Localizable.stringsdict'].forEach(function (fileName) {
var resourceFilePath = path.join(lngResourcesDir, fileName);
_this.removeFileIfExists(resourceFilePath);
});
_this.removeDirectoryIfEmpty(lngResourcesDir);
});
var settingsBundlePath = path.join(this.appResourcesDirectoryPath, 'Settings.bundle');
if (fs.existsSync(settingsBundlePath)) {
fs.readdirSync(settingsBundlePath)
.filter(function (fileName) {
var match = /^(.+)\.lproj$/.exec(fileName);
return match && !languages.has(match[1]);
})
.map(function (fileName) { return path.join(resourcesDirectory, fileName); })
.filter(function (filePath) { return fs.statSync(filePath).isDirectory(); })
.forEach(function (lngResourcesDir) {
fs.readdirSync(lngResourcesDir).forEach(function (fileName) {
_this.removeFileIfExists(path.join(lngResourcesDir, fileName));
});
_this.removeDirectoryIfEmpty(lngResourcesDir);
});
}
return this;
};
ConverterIOS.prototype.createLanguageResourcesFiles = function (language, isDefaultLanguage, i18nEntries) {
var _this = this;
var infoPlistStrings = new Map();
var projectData = this.projectData;
var appId = projectData.nsConfig.id;
var encodedEntries = this.encodeI18nEntries(i18nEntries);
encodedEntries.forEach(function (value, key) {
if (key.startsWith('ios.info.plist.')) {
infoPlistStrings.set(key.substring(15), value);
}
});
var languageResourcesDir = path.join(this.appResourcesDirectoryPath, "".concat(language, ".lproj"));
this.createDirectoryIfNeeded(languageResourcesDir)
.writeStrings(languageResourcesDir, 'Localizable.strings', encodedEntries)
.writeStrings(languageResourcesDir, 'InfoPlist.strings', infoPlistStrings)
.writeStringsDict(languageResourcesDir, this.encodeI18nEntries(i18nEntries, new Map(), encodeFormatValue));
if (isDefaultLanguage) {
infoPlistStrings.set('CFBundleDevelopmentRegion', language);
this.writeInfoPlist(infoPlistStrings);
}
var settingsBundlePath = path.join(this.appResourcesDirectoryPath, 'Settings.bundle');
if (fs.existsSync(settingsBundlePath)) {
fs.readdirSync(settingsBundlePath)
.filter(function (fileName) { return fileName.endsWith('.plist'); })
.forEach(function (settingsPagePlist) {
var settingsLanguageResourcesDir = path.join(settingsBundlePath, "".concat(language, ".lproj"));
_this.createDirectoryIfNeeded(settingsLanguageResourcesDir);
fs.copyFileSync(path.join(languageResourcesDir, 'Localizable.strings'), path.join(settingsLanguageResourcesDir, settingsPagePlist.split('.').slice(0, -1).join('.') + '.strings'));
});
}
return this;
};
ConverterIOS.prototype.encodeI18nEntries = function (i18nEntries, encodedI18nEntries, encode) {
if (encodedI18nEntries === void 0) { encodedI18nEntries = new Map(); }
if (encode === void 0) { encode = resource_ios_1.encodeValue; }
var projectData = this.projectData;
var appId = projectData.nsConfig.id;
i18nEntries.forEach(function (value, key) {
var encodedKey = key;
var forceWrite = false;
if (encodedKey.startsWith('$' + appId)) {
encodedKey = encodedKey.substring(appId.length + 2);
forceWrite = true;
}
else if (encodedKey.startsWith('$')) {
return;
}
var encodedValue = encode(value);
if (encodedKey.startsWith('android.strings.')) {
return;
}
else if (encodedKey === 'app.name') {
if (forceWrite || !encodedI18nEntries.has('ios.info.plist.CFBundleDisplayName')) {
encodedI18nEntries.set('ios.info.plist.CFBundleDisplayName', encodedValue);
}
}
else {
if (forceWrite || !encodedI18nEntries.has(encodedKey)) {
encodedI18nEntries.set(encodedKey, encodedValue);
}
}
});
return encodedI18nEntries;
};
ConverterIOS.prototype.writeStrings = function (languageResourcesDir, resourceFileName, i18nEntries) {
var content = '';
i18nEntries.forEach(function (encodedValue, encodedKey) {
if (!encodedKey.startsWith('android.strings.') && !encodedKey.startsWith('ios.info.plist.')) {
content += "\"".concat(encodedKey, "\" = \"").concat(encodedValue, "\";\n");
}
});
var resourceFilePath = path.join(languageResourcesDir, resourceFileName);
this.writeFileSyncIfNeeded(resourceFilePath, content);
return this;
};
ConverterIOS.prototype.writeStringsDict = function (languageResourcesDir, i18nEntries) {
var groups = (0, plural_common_1.groupPluralEntries)(i18nEntries);
['android.strings.', 'ios.info.plist.'].forEach(function (prefix) {
groups.forEach(function (_, name) {
if (name.startsWith(prefix)) {
groups.delete(name);
}
});
});
var resourceFilePath = path.join(languageResourcesDir, 'Localizable.stringsdict');
if (groups.size === 0) {
this.removeFileIfExists(resourceFilePath);
return this;
}
var content = '<?xml version="1.0" encoding="UTF-8"?>\n';
content += '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n';
content += '<plist version="1.0">\n<dict>\n';
groups.forEach(function (forms, name) {
content += " <key>".concat(escapeXML(name), "</key>\n <dict>\n");
content += ' <key>NSStringLocalizedFormatKey</key>\n <string>%#@value@</string>\n';
content += ' <key>value</key>\n <dict>\n';
content += ' <key>NSStringFormatSpecTypeKey</key>\n <string>NSStringPluralRuleType</string>\n';
content += ' <key>NSStringFormatValueTypeKey</key>\n <string>d</string>\n';
forms.forEach(function (value, category) {
content += " <key>".concat(category, "</key>\n <string>").concat(escapeXML(value.replace(/%1\$[@s]/g, '%1$d')), "</string>\n");
});
content += ' </dict>\n </dict>\n';
});
content += '</dict>\n</plist>\n';
this.writeFileSyncIfNeeded(resourceFilePath, content);
return this;
};
ConverterIOS.prototype.writeInfoPlist = function (infoPlistValues) {
var resourceFilePath = path.join(this.appResourcesDirectoryPath, 'Info.plist');
if (!fs.existsSync(resourceFilePath)) {
this.logger.warn("'".concat(resourceFilePath, "' doesn't exists: unable to set default language"));
return this;
}
var data = plist.readFileSync(resourceFilePath);
var resourceChanged = false;
infoPlistValues.forEach(function (value, key) {
if (!data.hasOwnProperty(key) || data[key] !== value) {
data[key] = value;
resourceChanged = true;
}
});
if (resourceChanged) {
plist.writeFileSync(resourceFilePath, data);
}
return this;
};
return ConverterIOS;
}(converter_common_1.ConverterCommon));
exports.ConverterIOS = ConverterIOS;
//# sourceMappingURL=converter.ios.js.map