Skip to content

Commit 68442f2

Browse files
committed
⚡ Optimize the i18n system
1 parent 1eaa45a commit 68442f2

File tree

14 files changed

+599
-497
lines changed

14 files changed

+599
-497
lines changed

.vscode/launch.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
{
22
"version": "0.2.0",
33
"configurations": [
4-
54
{
65
"type": "node",
76
"request": "launch",
87
"name": "Launch Program",
98
"program": "${workspaceRoot}/test/nodejs/serializeProject.js",
109
"cwd": "${workspaceRoot}",
11-
"args": ["test/jclic-demo/demo.jclic"]
10+
"args": [
11+
"test/jclic-demo/demo.jclic"
12+
]
1213
},
1314
{
1415
"name": "Launch Chrome against localhost",
1516
"type": "chrome",
1617
"request": "launch",
17-
"url": "http://localhost:8080/index.html",
18-
"webRoot": "${workspaceRoot}",
18+
"url": "http://localhost:9001/index.html",
19+
"webRoot": "${workspaceFolder}",
1920
"runtimeExecutable": "/usr/bin/chromium-browser",
2021
"runtimeArgs": [
2122
"--incognito"
@@ -29,4 +30,4 @@
2930
"webRoot": "${workspaceRoot}"
3031
}
3132
]
32-
}
33+
}

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### v2.1.1 (2021-04-12)
2+
#### Bug fixes
3+
- Fixed an issue with i18n, which did not correctly apply the required language selection.
4+
5+
#### Improvements
6+
- i18n has been written from scratch, simplifying its operation and supporting different "locale" encodings.
7+
18
### v2.1.0 (2021-04-10)
29
#### Breaking changes
310
- JClic projects can be now encapsulated in JSON format (files with extension `.jclic.json`). Current files in XML format (`.jclic`) will be still supported, but JSON will be the default format from now. Both formats can also be packaged in ZIP files (files of type `.jclic.zip` and `.scorm.zip`). This will simplify the development of the upcoming new project _JClic Author HTML5_.

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ JClic.js makes use of:
6565
* [jQuery](https://jquery.com/) to parse XML documents and manage DOM objects
6666
* [JSZip](https://stuk.github.io/jszip/) to extract contents from "jclic.zip" files
6767
* [clipboard-polyfill](https://github.com/lgarron/clipboard-polyfill) to copy reports data into the user's clipboard
68-
* [i18next](https://github.com/i18next/i18next) to deal with messages translated into different languages
6968
* [script.js](https://github.com/ded/script.js) to read JClic projects from local file systems as JSONP
7069
* [webfontloader](https://github.com/typekit/webfontloader) to dynamically load web fonts as needed
7170
* [MidiPlayerJS](https://github.com/grimmdude/MidiPlayerJS), [soundfont-player](https://github.com/danigb/soundfont-player), [audio-loader](https://github.com/audiojs/audio-loader) and [sample-player](https://github.com/danigb/sample-player) to process and play MIDI files

build-locales.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/usr/bin/env node
22

33
/*
4-
* Process a set of '.po' language files, extracting its keys and messages, and building
5-
* an object suitable for [i18next](https://www.npmjs.com/package/i18next)
4+
* Process a set of '.po' language files, extracting its keys and messages
65
*/
76

8-
/* global module:true */
9-
const fs = require('fs')
10-
const path = require('path')
11-
const po2json = require('po2json')
12-
const pkg = require('./package.json')
7+
/* global require, console, module:true */
8+
const fs = require('fs');
9+
const path = require('path');
10+
const po2json = require('po2json');
11+
const pkg = require('./package.json');
1312

1413
const _LOCALES = path.resolve('locales');
1514
const _GLOBALDATA = path.resolve('src', 'GlobalData.js');
@@ -21,65 +20,65 @@ const newerData = function (locales = _LOCALES, fileName = _GLOBALDATA) {
2120
const gTime = fs.statSync(fileName).ctimeMs;
2221
return fs.statSync(path.resolve('./package.json')).ctimeMs > gTime
2322
|| fs.readdirSync(locales).find(fn => fn.endsWith('.po') && fs.statSync(path.resolve(locales, fn)).ctimeMs > gTime) !== undefined;
24-
}
23+
};
2524

26-
// Compiles all 'po' language files in ./locales, returning the resulting i18next options object
25+
// Compiles all 'po' language files in ./locales, returning the resulting options object
2726
const getData = function (locales = _LOCALES, verbose = true) {
2827

29-
// Initialize the i18next options object
28+
// Initialize the options object
3029
const opt = {
3130
version: `${pkg.version} (${(new Date()).toISOString().substr(0, 10)})`,
3231
languages: ['en'],
33-
messages: {}
34-
}
32+
messages: {},
33+
};
3534

3635
// Process all .po files found in `localesDir`
3736
fs.readdirSync(locales).forEach(fn => {
3837
if (fn.endsWith('.po')) {
3938
// Gent language code from file name, skipping extension
40-
const lang = fn.substr(0, fn.lastIndexOf('.'))
41-
const file = path.resolve(locales, fn)
39+
const lang = fn.substr(0, fn.lastIndexOf('.'));
40+
const file = path.resolve(locales, fn);
4241
if (verbose)
43-
console.log(`Processing language ${lang}`)
42+
console.log(`Processing language ${lang}`);
4443
try {
4544
// Parse file with po2json and store result in `opt.messages`
46-
opt.messages[lang] = po2json.parseFileSync(file, { format: 'mf' })
45+
opt.messages[lang] = po2json.parseFileSync(file, { format: 'mf' });
4746
// Add lang to the list of known languages
48-
opt.languages.push(lang)
47+
opt.languages.push(lang);
4948
} catch (e) {
50-
console.error(`Error processing ${file}: ${e}`)
49+
console.error(`Error processing ${file}: ${e}`);
5150
}
5251
}
53-
})
54-
return opt
55-
}
52+
});
53+
return opt;
54+
};
5655

5756
// Generates the AMD module `GlobalData.js`, containing a single call to `define` with the content of `opt`
5857
const writeDataToJSFile = function (opt, file = _GLOBALDATA, verbose = true) {
5958
if (verbose)
60-
console.log(`Generating file ${file}`)
59+
console.log(`Generating file ${file}`);
6160

6261
try {
6362
fs.writeFileSync(file, `
6463
// WARNING: Auto-generated file, based on "language.po" files stored in "/locales". Do not edit!
6564
// Launch "npm run build-locales" to update this file
6665
67-
export default ${JSON.stringify(opt)};`)
66+
export default ${JSON.stringify(opt)};`);
6867
} catch (e) {
69-
console.error(`Error generating file ${file}: ${e}`)
68+
console.error(`Error generating file ${file}: ${e}`);
7069
}
71-
}
70+
};
7271

7372
module.exports = function (locales = _LOCALES, file = _GLOBALDATA, verbose = true) {
7473
// Run only if newer data
7574
if (newerData(locales, file))
76-
writeDataToJSFile(getData(locales, verbose), file, verbose)
77-
}
75+
writeDataToJSFile(getData(locales, verbose), file, verbose);
76+
};
7877

79-
module.exports.getData = getData
80-
module.exports.writeDataToJSFile = writeDataToJSFile
78+
module.exports.getData = getData;
79+
module.exports.writeDataToJSFile = writeDataToJSFile;
8180

8281
// Allow direct call from nodejs CLI
8382
if (require.main == module)
84-
module.exports()
83+
module.exports();
8584

0 commit comments

Comments
 (0)