Skip to content

Commit 6948c27

Browse files
committed
langParser.js: Generate language list dynamically
This adds ``langParser.js`` to generate the available language list automatically from available translation files. This also adds a npm script for generating before webpack bundles. Modifies .gitignore so that generated ``languages.js`` file is ignored. Adds tests. Closes: #125
1 parent 8960f21 commit 6948c27

9 files changed

+74
-50
lines changed

.coafile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ max_line_length = 80
55
use_spaces = True
66

77
[all.whitespace]
8+
ignore += src/js/languages.json
89
bears = SpaceConsistencyBear
910
default_actions = *: ApplyPatchAction
1011

lib/langParser.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require('fs')
2+
const iso639 = require('iso-639-1')
3+
4+
const availableLanguages = {}
5+
6+
fs.readdir(`${__dirname}/../static/i18n`, (err, items) => {
7+
items.forEach(function(value) {
8+
const langName = value.substring(0, value.indexOf('.'))
9+
const normalizedName = langName.split('_')[0]
10+
const nativeName = iso639.getNativeName(normalizedName)
11+
availableLanguages[nativeName] = langName
12+
})
13+
})
14+
15+
module.exports = availableLanguages

package-lock.json

+27-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"find-rss": "^1.6.4",
2828
"glob": "^7.1.2",
2929
"graphql-client": "^2.0.0",
30+
"iso-639-1": "^2.0.3",
3031
"jquery": "^3.2.1",
3132
"jquery.i18n": "git+https://github.com/wikimedia/jquery.i18n.git",
3233
"json2yaml": "^1.1.0",
@@ -53,6 +54,7 @@
5354
"eslint-plugin-prettier": "^2.3.1",
5455
"expose-loader": "^0.7.4",
5556
"extract-text-webpack-plugin": "^3.0.2",
57+
"generate-json-webpack-plugin": "^0.2.2",
5658
"jest": "^22.0.3",
5759
"mockdate": "^2.0.2",
5860
"postcss-loader": "^2.0.9",

src/js/languages.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"English":"en","Español":"es","Norsk bokmål":"nb_NO","język polski":"pl"}

src/js/locale.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import init from './app'
2+
const langs = require('./languages.json')
23

34
var browserLocale
45

@@ -26,12 +27,7 @@ function updateTranslation(localex) {
2627
}
2728

2829
$(window).on('load', function() {
29-
var localeOptions = {
30-
English: 'en',
31-
Español: 'es',
32-
Polski: 'pl',
33-
'Norwegian Bokmål': 'nb_NO',
34-
}
30+
var localeOptions = langs
3531

3632
var locList = $('#lang-select')
3733
$.each(localeOptions, function(key, value) {

tests/__data__/test-languages.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/lib/langParser.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* langParser Library Test
3+
*/
4+
5+
const fs = require('fs')
6+
const iso639 = require('iso-639-1')
7+
8+
const testLangs = ['en', 'es', 'ja_AK', 'fr']
9+
const expectedLangs = ['English', 'Español', '日本語', 'Français']
10+
11+
describe('lib.langParser', () => {
12+
it('should get nativeName for each language', () => {
13+
const resultLangs = []
14+
testLangs.forEach(function(value) {
15+
const normalizedName = value.split('_')[0]
16+
const nativeName = iso639.getNativeName(normalizedName)
17+
resultLangs.push(nativeName)
18+
})
19+
20+
expect(resultLangs).toEqual(expectedLangs)
21+
})
22+
})

webpack.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const CleanWebpackPlugin = require('clean-webpack-plugin')
44
const CopyWebpackPlugin = require('copy-webpack-plugin')
55
const ManifestPlugin = require('webpack-manifest-plugin')
66
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
7+
const GenerateJsonPlugin = require('generate-json-webpack-plugin')
78
const path = require('path')
9+
const generatedLangList = require('./lib/langParser')
810

911
const isProduction = process.env.NODE_ENV === 'production'
1012
const hash = isProduction ? '.[hash]' : ''
@@ -104,6 +106,7 @@ module.exports = {
104106
]),
105107
new ExtractTextPlugin(`[name]${hash}.css`),
106108
new ManifestPlugin(),
109+
new GenerateJsonPlugin('../src/js/languages.json', generatedLangList),
107110
].concat(
108111
isProduction
109112
? [

0 commit comments

Comments
 (0)