MOBILE-3665 lang: Move en.json to lang.json on parent directory

main
Pau Ferrer Ocaña 2020-12-14 15:35:18 +01:00
parent 5e085f69b1
commit 678ce2c1c8
19 changed files with 28 additions and 31 deletions

View File

@ -41,22 +41,24 @@ class BuildLangTask {
/** /**
* Run the task. * Run the task.
* *
* @param language Language to treat.
* @param langPaths Paths to the possible language files. * @param langPaths Paths to the possible language files.
* @param done Function to call when done. * @param done Function to call when done.
*/ */
run(language, langPaths, done) { run(langPaths, done) {
const filename = language + '.json';
const data = {}; const data = {};
let firstFile = null; let firstFile = null;
const self = this; const self = this;
const paths = langPaths.map((path) => { const paths = langPaths.map((path) => {
if (path.slice(-1) != '/') { if (path.endsWith('.json')) {
return path;
}
if (!path.endsWith('/')) {
path = path + '/'; path = path + '/';
} }
return path + language + '.json'; return path + 'lang.json';
}); });
gulp.src(paths, { allowEmpty: true }) gulp.src(paths, { allowEmpty: true })
@ -72,7 +74,7 @@ class BuildLangTask {
/* This implementation is based on gulp-jsoncombine module. /* This implementation is based on gulp-jsoncombine module.
* https://github.com/reflog/gulp-jsoncombine */ * https://github.com/reflog/gulp-jsoncombine */
if (firstFile) { if (firstFile) {
const joinedPath = pathLib.join(firstFile.base, language + '.json'); const joinedPath = pathLib.join(firstFile.base, 'en.json');
const joinedFile = new File({ const joinedFile = new File({
cwd: firstFile.cwd, cwd: firstFile.cwd,
@ -138,29 +140,28 @@ class BuildLangTask {
const mergedOrdered = {}; const mergedOrdered = {};
const getPrefix = (path) => { const getPrefix = (path) => {
const folders = path.split(/[\/\\]/); const folders = path.split(/[\/\\]/);
let filename = folders.pop();
switch (folders[0]) { switch (folders[0]) {
case 'core': case 'core':
switch (folders[1]) { switch (folders[1]) {
case 'lang':
return 'core.';
case 'features': case 'features':
return `core.${folders[2]}.`; return `core.${folders[2]}.`;
default:
return 'core.';
} }
break;
case 'addons': case 'addons':
return `addon.${folders.slice(1, -2).join('_')}.`; return `addon.${folders.slice(1).join('_')}.`;
case 'assets': case 'assets':
return `assets.${folders[1]}.`; filename = filename.split('.').slice(0, -1).join('.');
return `assets.${filename}.`;
default:
return `${folders[0]}.${folders[1]}.`;
} }
return null;
} }
for (let filepath in data) { for (let filepath in data) {
const prefix = getPrefix(filepath); const prefix = getPrefix(filepath);
if (prefix) { if (prefix) {
this.addProperties(merged, data[filepath], prefix); this.addProperties(merged, data[filepath], prefix);
} }

View File

@ -19,11 +19,11 @@ const gulp = require('gulp');
const paths = { const paths = {
lang: [ lang: [
'./src/addons/**/lang/', './src/addons/**/',
'./src/assets/countries/', './src/assets/countries.json',
'./src/assets/mimetypes/', './src/assets/mimetypes.json',
'./src/core/features/**/lang/', './src/core/features/**/',
'./src/core/lang/', './src/core/',
], ],
}; };
@ -31,7 +31,7 @@ const args = Utils.getCommandLineArguments();
// Build the language files into a single file per language. // Build the language files into a single file per language.
gulp.task('lang', (done) => { gulp.task('lang', (done) => {
new BuildLangTask().run('en', paths.lang, done); new BuildLangTask().run(paths.lang, done);
}); });
gulp.task('push', (done) => { gulp.task('push', (done) => {
@ -41,7 +41,5 @@ gulp.task('push', (done) => {
gulp.task('default', gulp.parallel('lang')); gulp.task('default', gulp.parallel('lang'));
gulp.task('watch', () => { gulp.task('watch', () => {
const langsPaths = paths.lang.map(path => path + 'en.json');
gulp.watch(langsPaths, { interval: 500 }, gulp.parallel('lang')); gulp.watch(langsPaths, { interval: 500 }, gulp.parallel('lang'));
}); });

View File

@ -324,4 +324,4 @@ jq -S --indent 2 -s '.[0]' langindex.json > langindex_new.json
mv langindex_new.json langindex.json mv langindex_new.json langindex.json
rm langindex_old.json rm langindex_old.json
print_ok 'All done!' print_ok 'All done!'

View File

@ -358,9 +358,7 @@ function detect_lang($lang, $keys) {
return false; return false;
} }
function save_key($key, $value, $path) { function save_key($key, $value, $filePath) {
$filePath = $path . '/en.json';
$file = file_get_contents($filePath); $file = file_get_contents($filePath);
$file = (array) json_decode($file); $file = (array) json_decode($file);
$value = html_entity_decode($value); $value = html_entity_decode($value);
@ -390,20 +388,20 @@ function override_component_lang_files($keys, $translations) {
case 'addon': case 'addon':
switch($component) { switch($component) {
case 'moodle': case 'moodle':
$path .= 'lang'; $path .= 'lang.json';
break; break;
default: default:
$path .= $type.'/'.str_replace('_', '/', $component).'/lang'; $path .= $type.'/'.str_replace('_', '/', $component).'/lang.json';
break; break;
} }
break; break;
case 'assets': case 'assets':
$path .= $type.'/'.$component; $path .= $type.'/'.$component.'.json';
break; break;
} }
if (is_file($path.'/en.json')) { if (is_file($path)) {
save_key($plainid, $value, $path); save_key($plainid, $value, $path);
} }
} }