@nativescript-community/l
Version:
Native internationalization plugin for NativeScript using native capabilities of each platform
144 lines • 7 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 converter_common_1 = require("./converter.common");
var resource_ios_1 = require("./resource.ios");
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'].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);
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) {
if (encodedI18nEntries === void 0) { encodedI18nEntries = new Map(); }
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 = (0, resource_ios_1.encodeValue)(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.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