mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-05-16 07:41:33 -04:00
Use routes.json
to get localization paths
Before we were using glob, which was about to start failing on subdirectories in views (which we started using in `conference`). Instead of searching for `l10n.json`, it seemed more appropriate to instead look for localization by using the configured pages that need to be localized.
This commit is contained in:
parent
b1be409dea
commit
1f871e7cf1
8 changed files with 123 additions and 119 deletions
|
@ -36,13 +36,13 @@
|
||||||
'''
|
'''
|
||||||
*/
|
*/
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var glob = require('glob');
|
|
||||||
var merge = require('lodash.merge');
|
var merge = require('lodash.merge');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var languages = require('../languages.json');
|
var languages = require('../languages.json');
|
||||||
var localeCompare = require('./lib/locale-compare');
|
var localeCompare = require('./lib/locale-compare');
|
||||||
var localizedUrls = require('./lib/localized-urls');
|
var localizedUrls = require('./lib/localized-urls');
|
||||||
|
var routes = require('../src/routes.json');
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// Main script
|
// Main script
|
||||||
|
@ -64,56 +64,72 @@ try {
|
||||||
fs.mkdirSync(outputDir);
|
fs.mkdirSync(outputDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// message key with english string values (i.e. default values)
|
|
||||||
var viewLocales = {};
|
|
||||||
// FormattedMessage id with english string as value. Use for default values in translations
|
|
||||||
// Sample structure: { 'general-general.blah': 'blah', 'about-about.blah': 'blahblah' }
|
|
||||||
var idsWithICU = {};
|
|
||||||
// reverse (i.e. english string with message key as the value) object for searching po files.
|
|
||||||
// Sample structure: { 'blah': 'general-general.blah', 'blahblah': 'about-about.blah' }
|
|
||||||
var icuWithIds = {};
|
|
||||||
|
|
||||||
// get global locale strings first.
|
// get global locale strings first.
|
||||||
var globalTemplateFile = path.resolve(__dirname, '../src/l10n.json');
|
var globalTemplateFile = path.resolve(__dirname, '../src/l10n.json');
|
||||||
localeCompare.getIdsForView('general', globalTemplateFile, viewLocales, idsWithICU, icuWithIds);
|
var ids = JSON.parse(fs.readFileSync(globalTemplateFile, 'utf8'));
|
||||||
|
|
||||||
|
// message key with english string values (i.e. default values)
|
||||||
|
var viewLocales = {
|
||||||
|
general: {
|
||||||
|
en: ids
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var idsWithICU = localeCompare.idToICUMap('general', ids);
|
||||||
|
var icuWithIds = localeCompare.icuToIdMap('general', ids);
|
||||||
|
// localeCompare.getIdsForView('general', globalTemplateFile, viewLocales, idsWithICU, icuWithIds);
|
||||||
|
|
||||||
|
var views = [];
|
||||||
|
var localizedAssetUrls = {};
|
||||||
|
|
||||||
// start with all views, and remove localized ones as they are iterated over
|
// start with all views, and remove localized ones as they are iterated over
|
||||||
var views = glob.sync(path.resolve(__dirname, '../src/views/*'));
|
for (var v in routes) {
|
||||||
for (var i = 0; i < views.length; i++) {
|
if (typeof routes[v].redirect !== 'undefined') {
|
||||||
views[i] = views[i].split('/').pop();
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get view-specific locale strings.
|
views.push(routes[v].name);
|
||||||
var files = glob.sync(path.resolve(__dirname, '../src/views/**/l10n.json'));
|
try {
|
||||||
files.forEach(function (file) {
|
var subdir = routes[v].view.split('/');
|
||||||
var dirPath = file.split('/');
|
subdir.pop();
|
||||||
dirPath.pop();
|
var l10n = path.resolve(__dirname, '../src/views/' + subdir.join('/') + '/l10n.json');
|
||||||
var view = dirPath.pop();
|
fs.accessSync(l10n, fs.F_OK);
|
||||||
localeCompare.getIdsForView(view, file, viewLocales, idsWithICU, icuWithIds);
|
ids = JSON.parse(fs.readFileSync(l10n, 'utf8'));
|
||||||
});
|
viewLocales[routes[v].name] = {
|
||||||
|
en: ids
|
||||||
// get asset url translations
|
};
|
||||||
var localizedAssetUrls = {};
|
idsWithICU = merge(idsWithICU, localeCompare.idToICUMap(routes[v].name, ids));
|
||||||
files = glob.sync(path.resolve(__dirname, '../src/views/**/l10n-static.json'));
|
icuWithIds = merge(icuWithIds, localeCompare.icuToIdMap(routes[v].name, ids));
|
||||||
files.forEach(function (file) {
|
} catch (err) {
|
||||||
var dirPath = file.split('/');
|
// check that routes' view path is not malformed, error if it is
|
||||||
dirPath.pop();
|
try {
|
||||||
var view = dirPath.pop();
|
fs.accessSync(path.resolve(__dirname, '../src/views/' + routes[v].view + '.jsx'));
|
||||||
localizedAssetUrls[view] = {};
|
} catch (err) {
|
||||||
|
// the config for the view is not set up correctly, so throw the error.
|
||||||
var assetUrls = JSON.parse(fs.readFileSync(file, 'utf8'));
|
throw err;
|
||||||
for (var lang in localizedUrls) {
|
|
||||||
localizedAssetUrls[view][lang] = {};
|
|
||||||
for (var key in assetUrls) {
|
|
||||||
if (localizedUrls[lang].hasOwnProperty(key)) {
|
|
||||||
localizedAssetUrls[view][lang][key] = localizedUrls[lang][key];
|
|
||||||
} else {
|
|
||||||
localizedAssetUrls[view][lang][key] = assetUrls[key];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
// get asset url translations
|
||||||
|
try {
|
||||||
|
var l10nStatic = path.resolve(__dirname, '../src/views/' + routes[v].view + '/l10n-static.json');
|
||||||
|
fs.accessSync(l10nStatic, fs.F_OK);
|
||||||
|
localizedAssetUrls[routes[v].name] = {};
|
||||||
|
|
||||||
|
var assetUrls = JSON.parse(fs.readFileSync(l10nStatic, 'utf8'));
|
||||||
|
for (var lang in localizedUrls) {
|
||||||
|
localizedAssetUrls[routes[v].name][lang] = {};
|
||||||
|
for (var key in assetUrls) {
|
||||||
|
if (localizedUrls[lang].hasOwnProperty(key)) {
|
||||||
|
localizedAssetUrls[routes[v].name][lang][key] = localizedUrls[lang][key];
|
||||||
|
} else {
|
||||||
|
localizedAssetUrls[routes[v].name][lang][key] = assetUrls[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// :)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// md5 of english strings with message key as the value for searching po files.
|
// md5 of english strings with message key as the value for searching po files.
|
||||||
// Sample structure: { 'sdfas43534sdfasdf': 'general-general.blah', 'lkjfasdf4t342asdfa': 'about-about.blah' }
|
// Sample structure: { 'sdfas43534sdfasdf': 'general-general.blah', 'lkjfasdf4t342asdfa': 'about-about.blah' }
|
||||||
|
@ -121,9 +137,9 @@ var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
|
|
||||||
// Get ui localization strings first
|
// Get ui localization strings first
|
||||||
var isoCodes = Object.keys(languages);
|
var isoCodes = Object.keys(languages);
|
||||||
for (i in isoCodes) {
|
for (var i in isoCodes) {
|
||||||
var translations = localeCompare.getTranslationsForLanguage(isoCodes[i], idsWithICU, md5WithIds);
|
var translations = localeCompare.getTranslationsForLanguage(isoCodes[i], idsWithICU, md5WithIds);
|
||||||
for (var key in translations) {
|
for (key in translations) {
|
||||||
viewLocales[key] = merge(viewLocales[key], translations[key]);
|
viewLocales[key] = merge(viewLocales[key], translations[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,15 +125,24 @@ Helpers.writeTranslationsToJS = function (outputDir, viewName, translationObject
|
||||||
fs.writeFileSync(outputDir + '/' + viewName + '.intl.js', fileString);
|
fs.writeFileSync(outputDir + '/' + viewName + '.intl.js', fileString);
|
||||||
};
|
};
|
||||||
|
|
||||||
Helpers.getIdsForView = function (viewName, viewFile, localeObject, idsWithICU, icuWithIds) {
|
// Returns a FormattedMessage id with english string as value. Use for default values in translations
|
||||||
var ids = JSON.parse(fs.readFileSync(viewFile, 'utf8'));
|
// Sample structure: { 'general-general.blah': 'blah', 'about-about.blah': 'blahblah' }
|
||||||
localeObject[viewName] = {
|
Helpers.idToICUMap = function (viewName, ids) {
|
||||||
en: ids
|
var idsToICU = {};
|
||||||
};
|
|
||||||
for (var id in ids) {
|
for (var id in ids) {
|
||||||
idsWithICU[viewName + '-' + id] = ids[id];
|
idsToICU[viewName + '-' + id] = ids[id];
|
||||||
icuWithIds[ids[id]] = viewName + '-' + id; // add viewName to identifier for later
|
|
||||||
}
|
}
|
||||||
|
return idsToICU;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Reuturns reverse (i.e. english string with message key as the value) object for searching po files.
|
||||||
|
// Sample structure: { 'blah': 'general-general.blah', 'blahblah': 'about-about.blah' }
|
||||||
|
Helpers.icuToIdMap = function (viewName, ids) {
|
||||||
|
var icuToIds = {};
|
||||||
|
for (var id in ids) {
|
||||||
|
icuToIds[ids[id]] = viewName + '-' + id;
|
||||||
|
}
|
||||||
|
return icuToIds;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Helpers;
|
module.exports = Helpers;
|
||||||
|
|
|
@ -12,16 +12,13 @@ var localeCompare = require('../../bin/lib/locale-compare');
|
||||||
tap.test('spotCheckAboutStrings', function (t) {
|
tap.test('spotCheckAboutStrings', function (t) {
|
||||||
var isoCodes = Object.keys(languages);
|
var isoCodes = Object.keys(languages);
|
||||||
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
||||||
var viewLocales = {};
|
|
||||||
var idsWithICU = {};
|
var ids = path.resolve(__dirname, '../../views/about/l10n.json');
|
||||||
var icuWithIds = {};
|
var viewLocales = {
|
||||||
localeCompare.getIdsForView(
|
about: {en: ids}
|
||||||
'about',
|
};
|
||||||
path.resolve(__dirname, '../../src/views/about/l10n.json'),
|
var idsWithICU = localeCompare.idToICUMap('about', ids);
|
||||||
viewLocales,
|
var icuWithIds = localeCompare.icuToIdMap('about', ids);
|
||||||
idsWithICU,
|
|
||||||
icuWithIds
|
|
||||||
);
|
|
||||||
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
var keysToCheck = Object.keys(merge(viewLocales['about']['en'])).sort();
|
var keysToCheck = Object.keys(merge(viewLocales['about']['en'])).sort();
|
||||||
for (var i in isoCodes) {
|
for (var i in isoCodes) {
|
||||||
|
|
|
@ -9,19 +9,16 @@ var tap = require('tap');
|
||||||
var languages = require('../../languages.json');
|
var languages = require('../../languages.json');
|
||||||
var localeCompare = require('../../bin/lib/locale-compare');
|
var localeCompare = require('../../bin/lib/locale-compare');
|
||||||
|
|
||||||
tap.test('spotCheckAboutStrings', function (t) {
|
tap.test('spotCheckCardStrings', function (t) {
|
||||||
var isoCodes = Object.keys(languages);
|
var isoCodes = Object.keys(languages);
|
||||||
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
||||||
var viewLocales = {};
|
|
||||||
var idsWithICU = {};
|
var ids = path.resolve(__dirname, '../../views/cards/l10n.json');
|
||||||
var icuWithIds = {};
|
var viewLocales = {
|
||||||
localeCompare.getIdsForView(
|
cards: {en: ids}
|
||||||
'cards',
|
};
|
||||||
path.resolve(__dirname, '../../src/views/cards/l10n.json'),
|
var idsWithICU = localeCompare.idToICUMap('cards', ids);
|
||||||
viewLocales,
|
var icuWithIds = localeCompare.icuToIdMap('cards', ids);
|
||||||
idsWithICU,
|
|
||||||
icuWithIds
|
|
||||||
);
|
|
||||||
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
var keysToCheck = Object.keys(merge(viewLocales['cards']['en'])).sort();
|
var keysToCheck = Object.keys(merge(viewLocales['cards']['en'])).sort();
|
||||||
for (var i in isoCodes) {
|
for (var i in isoCodes) {
|
||||||
|
|
|
@ -9,19 +9,16 @@ var tap = require('tap');
|
||||||
var languages = require('../../languages.json');
|
var languages = require('../../languages.json');
|
||||||
var localeCompare = require('../../bin/lib/locale-compare');
|
var localeCompare = require('../../bin/lib/locale-compare');
|
||||||
|
|
||||||
tap.test('spotCheckAboutStrings', function (t) {
|
tap.test('spotCheckGeneralStrings', function (t) {
|
||||||
var isoCodes = Object.keys(languages);
|
var isoCodes = Object.keys(languages);
|
||||||
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
||||||
var viewLocales = {};
|
|
||||||
var idsWithICU = {};
|
var ids = path.resolve(__dirname, '../../src/l10n.json');
|
||||||
var icuWithIds = {};
|
var viewLocales = {
|
||||||
localeCompare.getIdsForView(
|
general: {en: ids}
|
||||||
'general',
|
};
|
||||||
path.resolve(__dirname, '../../src/l10n.json'),
|
var idsWithICU = localeCompare.idToICUMap('general', ids);
|
||||||
viewLocales,
|
var icuWithIds = localeCompare.icuToIdMap('general', ids);
|
||||||
idsWithICU,
|
|
||||||
icuWithIds
|
|
||||||
);
|
|
||||||
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
var keysToCheck = Object.keys(merge(viewLocales['general']['en'])).sort();
|
var keysToCheck = Object.keys(merge(viewLocales['general']['en'])).sort();
|
||||||
for (var i in isoCodes) {
|
for (var i in isoCodes) {
|
||||||
|
|
|
@ -16,10 +16,6 @@ var path = require('path');
|
||||||
var tap = require('tap');
|
var tap = require('tap');
|
||||||
|
|
||||||
var localeCompare = require('../../bin/lib/locale-compare');
|
var localeCompare = require('../../bin/lib/locale-compare');
|
||||||
var viewLocales = {};
|
|
||||||
var idsWithICU = {};
|
|
||||||
var icuWithIds = {};
|
|
||||||
|
|
||||||
var languagesToCheck = [
|
var languagesToCheck = [
|
||||||
'he', 'zh-cn', 'ja', 'pt-br', 'pl', 'nb'
|
'he', 'zh-cn', 'ja', 'pt-br', 'pl', 'nb'
|
||||||
];
|
];
|
||||||
|
@ -29,14 +25,12 @@ var idsToCheck = [
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
// Test nav for real languages.
|
var ids = path.resolve(__dirname, '../../src/l10n.json');
|
||||||
localeCompare.getIdsForView(
|
var viewLocales = {
|
||||||
'general',
|
general: {en: ids}
|
||||||
path.resolve(__dirname, '../../src/l10n.json'),
|
};
|
||||||
viewLocales,
|
var idsWithICU = localeCompare.idToICUMap('general', ids);
|
||||||
idsWithICU,
|
var icuWithIds = localeCompare.icuToIdMap('general', ids);
|
||||||
icuWithIds
|
|
||||||
);
|
|
||||||
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
|
|
||||||
tap.test('spotCheckNavBar', function (t) {
|
tap.test('spotCheckNavBar', function (t) {
|
||||||
|
|
|
@ -9,19 +9,16 @@ var tap = require('tap');
|
||||||
var languages = require('../../languages.json');
|
var languages = require('../../languages.json');
|
||||||
var localeCompare = require('../../bin/lib/locale-compare');
|
var localeCompare = require('../../bin/lib/locale-compare');
|
||||||
|
|
||||||
tap.test('spotCheckAboutStrings', function (t) {
|
tap.test('spotCheckSplashStrings', function (t) {
|
||||||
var isoCodes = Object.keys(languages);
|
var isoCodes = Object.keys(languages);
|
||||||
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
||||||
var viewLocales = {};
|
|
||||||
var idsWithICU = {};
|
var ids = path.resolve(__dirname, '../../views/splash/l10n.json');
|
||||||
var icuWithIds = {};
|
var viewLocales = {
|
||||||
localeCompare.getIdsForView(
|
splash: {en: ids}
|
||||||
'splash',
|
};
|
||||||
path.resolve(__dirname, '../../src/views/splash/l10n.json'),
|
var idsWithICU = localeCompare.idToICUMap('splash', ids);
|
||||||
viewLocales,
|
var icuWithIds = localeCompare.icuToIdMap('splash', ids);
|
||||||
idsWithICU,
|
|
||||||
icuWithIds
|
|
||||||
);
|
|
||||||
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
var keysToCheck = Object.keys(merge(viewLocales['splash']['en'])).sort();
|
var keysToCheck = Object.keys(merge(viewLocales['splash']['en'])).sort();
|
||||||
for (var i in isoCodes) {
|
for (var i in isoCodes) {
|
||||||
|
|
|
@ -9,19 +9,16 @@ var tap = require('tap');
|
||||||
var languages = require('../../languages.json');
|
var languages = require('../../languages.json');
|
||||||
var localeCompare = require('../../bin/lib/locale-compare');
|
var localeCompare = require('../../bin/lib/locale-compare');
|
||||||
|
|
||||||
tap.test('spotCheckAboutStrings', function (t) {
|
tap.test('spotCheckWedo2Strings', function (t) {
|
||||||
var isoCodes = Object.keys(languages);
|
var isoCodes = Object.keys(languages);
|
||||||
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
isoCodes.splice(isoCodes.indexOf('en'), 1);
|
||||||
var viewLocales = {};
|
|
||||||
var idsWithICU = {};
|
var ids = path.resolve(__dirname, '../../views/wedo2/l10n.json');
|
||||||
var icuWithIds = {};
|
var viewLocales = {
|
||||||
localeCompare.getIdsForView(
|
wedo2: {en: ids}
|
||||||
'wedo2',
|
};
|
||||||
path.resolve(__dirname, '../../src/views/wedo2/l10n.json'),
|
var idsWithICU = localeCompare.idToICUMap('wedo2', ids);
|
||||||
viewLocales,
|
var icuWithIds = localeCompare.icuToIdMap('wedo2', ids);
|
||||||
idsWithICU,
|
|
||||||
icuWithIds
|
|
||||||
);
|
|
||||||
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
var md5WithIds = localeCompare.getMD5Map(icuWithIds);
|
||||||
var keysToCheck = Object.keys(merge(viewLocales['wedo2']['en'])).sort();
|
var keysToCheck = Object.keys(merge(viewLocales['wedo2']['en'])).sort();
|
||||||
for (var i in isoCodes) {
|
for (var i in isoCodes) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue