mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Update translation registration to file path rather than bundling
After review discussions it was agreed to just register the absolute paths to translation files rather than bundling each file into one central file at registration.
This commit is contained in:
parent
b84f40a40b
commit
2dd3065856
2 changed files with 63 additions and 219 deletions
|
@ -1,26 +1,15 @@
|
|||
import expect from 'expect.js';
|
||||
import fs from 'fs';
|
||||
import fse from 'fs-extra';
|
||||
import i18n from '../i18n/i18n';
|
||||
import path from 'path';
|
||||
import process from 'child_process';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
const PATH_SEPARATOR = path.sep;
|
||||
const DATA_PATH = __dirname + PATH_SEPARATOR + 'data';
|
||||
const TRANSLATION_BACKUP_PATH = DATA_PATH + '/translations_backup';
|
||||
|
||||
const translationStorePath = i18n.getTranslationStoragePath();
|
||||
const stat = Promise.promisify(fs.stat);
|
||||
|
||||
describe('Test registering translations for test_plugin_1', function () {
|
||||
const pluginName = 'test_plugin_1';
|
||||
const pluginTranslationPath = DATA_PATH + PATH_SEPARATOR + 'translations' + PATH_SEPARATOR + pluginName;
|
||||
|
||||
before(function (done) {
|
||||
backupTranslations(done);
|
||||
});
|
||||
|
||||
it('Register translations' , function (done) {
|
||||
let result = true;
|
||||
const translationFiles = [
|
||||
|
@ -28,16 +17,9 @@ describe('Test registering translations for test_plugin_1', function () {
|
|||
pluginTranslationPath + PATH_SEPARATOR + 'en.json'
|
||||
];
|
||||
|
||||
registerTranslations(translationFiles).then(() => {
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
console.log(err);
|
||||
result = false;
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
|
||||
registerTranslations(translationFiles);
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
|
||||
it('EN translations are registered' , function (done) {
|
||||
|
@ -62,18 +44,10 @@ describe('Test registering translations for test_plugin_1', function () {
|
|||
const expectedLocales = ['en', 'de'];
|
||||
checkRegisteredLocales(expectedLocales, done);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
restoreTranslations(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Test registering translations for test_plugin_1 and test_plugin_2', function () {
|
||||
|
||||
before(function (done) {
|
||||
backupTranslations(done);
|
||||
});
|
||||
|
||||
it('Register translations for test_plugin_1' , function (done) {
|
||||
let result = true;
|
||||
const pluginName = 'test_plugin_1';
|
||||
|
@ -83,15 +57,9 @@ describe('Test registering translations for test_plugin_1 and test_plugin_2', fu
|
|||
pluginTranslationPath + PATH_SEPARATOR + 'en.json'
|
||||
];
|
||||
|
||||
registerTranslations(translationFiles).then(() => {
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
console.log(err);
|
||||
result = false;
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
registerTranslations(translationFiles);
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
|
||||
it('Register translations for test_plugin_2' , function (done) {
|
||||
|
@ -102,15 +70,9 @@ describe('Test registering translations for test_plugin_1 and test_plugin_2', fu
|
|||
pluginTranslationPath + PATH_SEPARATOR + 'en.json'
|
||||
];
|
||||
|
||||
registerTranslations(translationFiles).then(() => {
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
console.log(err);
|
||||
result = false;
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
registerTranslations(translationFiles);
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
|
||||
it('EN translations are registered' , function (done) {
|
||||
|
@ -131,10 +93,6 @@ describe('Test registering translations for test_plugin_1 and test_plugin_2', fu
|
|||
const expectedLocales = ['en', 'de'];
|
||||
checkRegisteredLocales(expectedLocales, done);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
restoreTranslations(done);
|
||||
});
|
||||
});
|
||||
|
||||
function compareTranslations(actual, expected) {
|
||||
|
@ -172,84 +130,29 @@ function checkTranslations(locale, expectedTranslations, done) {
|
|||
}
|
||||
|
||||
function registerTranslations(pluginTranslationFiles) {
|
||||
return Promise.map(pluginTranslationFiles, (translationFile) => {
|
||||
return i18n.registerTranslations(translationFile).then(() => {
|
||||
});
|
||||
});
|
||||
const numFiles = pluginTranslationFiles.length;
|
||||
for (let indx = 0; indx < numFiles; indx++) {
|
||||
i18n.registerTranslations(pluginTranslationFiles[indx]);
|
||||
}
|
||||
}
|
||||
|
||||
function checkRegisteredLocales(expectedLocales, done) {
|
||||
let result = true;
|
||||
|
||||
i18n.getRegisteredTranslationLocales().then(function (actualLocales) {
|
||||
if (actualLocales.length !== expectedLocales.length) {
|
||||
result = false;
|
||||
} else {
|
||||
let index = actualLocales.length;
|
||||
actualLocales.sort();
|
||||
expectedLocales.sort();
|
||||
while (index--) {
|
||||
if (actualLocales[index] !== expectedLocales[index]) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
console.log(err);
|
||||
const actualLocales = i18n.getRegisteredTranslationLocales();
|
||||
if (actualLocales.length !== expectedLocales.length) {
|
||||
result = false;
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
function backupTranslations(done) {
|
||||
const translationStorePath = i18n.getTranslationStoragePath();
|
||||
return stat(translationStorePath).then((stats) => {
|
||||
fse.copy(translationStorePath, TRANSLATION_BACKUP_PATH, function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
done();
|
||||
return;
|
||||
} else {
|
||||
let index = actualLocales.length;
|
||||
actualLocales.sort();
|
||||
expectedLocales.sort();
|
||||
while (index--) {
|
||||
if (actualLocales[index] !== expectedLocales[index]) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
fse.emptyDir(translationStorePath, function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
}).catch(function (e) {
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
function restoreTranslations(done) {
|
||||
const translationStorePath = i18n.getTranslationStoragePath();
|
||||
fse.emptyDir(translationStorePath, function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
done();
|
||||
return;
|
||||
}
|
||||
return stat(TRANSLATION_BACKUP_PATH).then((stats) => {
|
||||
fse.copy(TRANSLATION_BACKUP_PATH, translationStorePath, function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
done();
|
||||
return;
|
||||
}
|
||||
fse.remove(TRANSLATION_BACKUP_PATH, function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
}).catch(function (e) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
expect(result).to.be(true);
|
||||
done();
|
||||
}
|
||||
|
|
|
@ -1,124 +1,65 @@
|
|||
import fs from 'fs';
|
||||
import kibanaPackage from '../../../../utils/package_json';
|
||||
import mkdirp from 'mkdirp';
|
||||
import path from 'path';
|
||||
import Promise from 'bluebird';
|
||||
|
||||
const readdir = Promise.promisify(fs.readdir);
|
||||
const readFile = Promise.promisify(fs.readFile);
|
||||
const stat = Promise.promisify(fs.stat);
|
||||
const writeFile = Promise.promisify(fs.writeFile);
|
||||
const mkdirpAsync = Promise.promisify(mkdirp);
|
||||
|
||||
const PATH_SEPARATOR = path.sep;
|
||||
const TRANSLATION_FILE_EXTENSION = 'json';
|
||||
const TRANSLATION_STORE_PATH = kibanaPackage.__dirname + PATH_SEPARATOR + 'data' + PATH_SEPARATOR + 'translations';
|
||||
|
||||
const getTranslationStoragePath = function () {
|
||||
return TRANSLATION_STORE_PATH;
|
||||
};
|
||||
let registeredTranslations = {};
|
||||
|
||||
const getRegisteredTranslationLocales = function () {
|
||||
let translationFiles = [];
|
||||
let localeList = [];
|
||||
const translationStorePath = getTranslationStoragePath();
|
||||
|
||||
return getTranslationDetailsFromDirectory(translationStorePath, translationFiles, localeList).then(function () {
|
||||
return localeList;
|
||||
});
|
||||
return Object.keys(registeredTranslations);
|
||||
};
|
||||
|
||||
const registerTranslations = function (absolutePluginTranslationFilePath) {
|
||||
const translationStorePath = getTranslationStoragePath();
|
||||
let translationFiles = [];
|
||||
|
||||
return createTranslationDirectory(translationStorePath).then(() => {
|
||||
return storeTranslations(absolutePluginTranslationFilePath, translationStorePath);
|
||||
});
|
||||
const locale = getLocaleFromFileName(absolutePluginTranslationFilePath);
|
||||
if (registeredTranslations.hasOwnProperty(locale)) {
|
||||
translationFiles = registeredTranslations[locale];
|
||||
}
|
||||
translationFiles.push(absolutePluginTranslationFilePath);
|
||||
registeredTranslations[locale] = translationFiles;
|
||||
};
|
||||
|
||||
const getRegisteredLocaleTranslations = function (locale) {
|
||||
const translationStorePath = getTranslationStoragePath();
|
||||
const translationFileName = locale + '.' + TRANSLATION_FILE_EXTENSION;
|
||||
const translationFile = translationStorePath + PATH_SEPARATOR + translationFileName;
|
||||
if (!registeredTranslations.hasOwnProperty(locale)) {
|
||||
throw new Error('No translations registered for locale: ' + locale);
|
||||
}
|
||||
|
||||
return readFile(translationFile, 'utf8').then(function (translationStr) {
|
||||
let translationJson = [];
|
||||
try {
|
||||
translationJson = JSON.parse(translationStr);
|
||||
} catch (err) {
|
||||
throw new Error('Bad ' + locale + ' translation strings. Reason: ' + err);
|
||||
}
|
||||
const translationFiles = registeredTranslations[locale];
|
||||
let translationJson = {};
|
||||
|
||||
return Promise.map(translationFiles, (translationFile) => {
|
||||
return readFile(translationFile, 'utf8').then(function (translationStr) {
|
||||
const translationToAddJson = JSON.parse(translationStr);
|
||||
for (let key in translationToAddJson) {
|
||||
if (translationToAddJson.hasOwnProperty(key)) {
|
||||
const attrName = key;
|
||||
const attrValue = translationToAddJson[key];
|
||||
translationJson[attrName] = attrValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
}).then(function () {
|
||||
return translationJson;
|
||||
}).catch(function (e) {
|
||||
throw new Error('Failed to read translation file. Reason: ' + e);
|
||||
});
|
||||
};
|
||||
|
||||
function storeTranslations(absolutePluginTranslationFilePath, translationStorePath) {
|
||||
const translationFileName = getFileName(absolutePluginTranslationFilePath);
|
||||
const translationJson = require(absolutePluginTranslationFilePath);
|
||||
const fileToWrite = translationStorePath + PATH_SEPARATOR + translationFileName;
|
||||
return getTranslationJsonToWrite(fileToWrite, translationJson).then((jsonToWrite) => {
|
||||
return writeFile(fileToWrite, JSON.stringify(jsonToWrite));
|
||||
});
|
||||
}
|
||||
|
||||
function getTranslationJsonToWrite(translationFullFileName, translationToAddJson) {
|
||||
return stat(translationFullFileName).then((stats) => {
|
||||
let jsonToWrite = [];
|
||||
const currentTranslationJson = require(translationFullFileName);
|
||||
jsonToWrite = currentTranslationJson;
|
||||
for (let key in translationToAddJson) {
|
||||
if (translationToAddJson.hasOwnProperty(key)) {
|
||||
const attrName = key;
|
||||
const attrValue = translationToAddJson[key];
|
||||
jsonToWrite[attrName] = attrValue;
|
||||
}
|
||||
}
|
||||
return jsonToWrite;
|
||||
}).catch(function (e) {
|
||||
return translationToAddJson;
|
||||
});
|
||||
}
|
||||
|
||||
function createTranslationDirectory(translationStorePath) {
|
||||
return stat(translationStorePath).then((stats) => {
|
||||
}).catch(function (e) {
|
||||
return mkdirpAsync(translationStorePath);
|
||||
});
|
||||
}
|
||||
|
||||
function getTranslationDetailsFromDirectory(dir, translationFiles, localeList) {
|
||||
return readdir(dir).then((dirListing) => {
|
||||
return Promise.map(dirListing, (listing) => {
|
||||
const fullPath = path.join(dir, listing);
|
||||
return stat(fullPath).then((stats) => {
|
||||
if (!stats.isDirectory()) {
|
||||
getTranslationDetailsFromFile(fullPath, translationFiles, localeList);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getTranslationDetailsFromFile(fullFileName, translationFiles, localeList) {
|
||||
const fileName = getFileName(fullFileName);
|
||||
const fileExt = fileName.split('.').pop();
|
||||
|
||||
if (fileName === fileExt) return;
|
||||
if (fileExt !== TRANSLATION_FILE_EXTENSION) return;
|
||||
translationFiles.push(fullFileName);
|
||||
const locale = fileName.substr(0, fileName.lastIndexOf('.'));
|
||||
if (localeList.indexOf(locale) === -1) {
|
||||
localeList.push(locale);
|
||||
}
|
||||
}
|
||||
|
||||
function getFileName(fullPath) {
|
||||
return fullPath.replace(/^.*[\\\/]/, '');
|
||||
}
|
||||
|
||||
function getLocaleFromFileName(fullFileName) {
|
||||
const fileName = getFileName(fullFileName);
|
||||
const fileExt = fileName.split('.').pop();
|
||||
|
||||
if (fileName === fileExt) return null;
|
||||
if (fileExt !== TRANSLATION_FILE_EXTENSION) return null;
|
||||
const locale = fileName.substr(0, fileName.lastIndexOf('.'));
|
||||
return locale;
|
||||
}
|
||||
|
||||
module.exports.registerTranslations = registerTranslations;
|
||||
module.exports.getRegisteredLocaleTranslations = getRegisteredLocaleTranslations;
|
||||
module.exports.getTranslationStoragePath = getTranslationStoragePath;
|
||||
module.exports.getRegisteredTranslationLocales = getRegisteredTranslationLocales;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue