Skip to content

Commit e2ebd2a

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 e2ebd2a

File tree

9 files changed

+76
-51
lines changed

9 files changed

+76
-51
lines changed

.coafile

Lines changed: 1 addition & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 27 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
"generate": "node ./lib/generate.js",
1010
"gather": "node ./lib/gather.js",
1111
"main": "node ./lib/main.js",
12+
"languages": "node ./lib/langParser.js",
1213
"clean": "rm -rf out/*",
1314
"dev": "webpack-dev-server --progress",
14-
"bundle": "NODE_ENV=production webpack --progress",
15+
"bundle": "set NODE_ENV=production && webpack --progress",
1516
"build": "npm run bundle && npm run gather && npm run scrape && npm run planet && npm run main && npm run generate",
1617
"build:clean": "npm run clean && npm run build",
1718
"test": "jest"
@@ -27,6 +28,7 @@
2728
"find-rss": "^1.6.4",
2829
"glob": "^7.1.2",
2930
"graphql-client": "^2.0.0",
31+
"iso-639-1": "^2.0.3",
3032
"jquery": "^3.2.1",
3133
"jquery.i18n": "git+https://github.com/wikimedia/jquery.i18n.git",
3234
"json2yaml": "^1.1.0",
@@ -53,6 +55,7 @@
5355
"eslint-plugin-prettier": "^2.3.1",
5456
"expose-loader": "^0.7.4",
5557
"extract-text-webpack-plugin": "^3.0.2",
58+
"generate-json-webpack-plugin": "^0.2.2",
5659
"jest": "^22.0.3",
5760
"mockdate": "^2.0.2",
5861
"postcss-loader": "^2.0.9",

src/js/languages.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/lib/langParser.test.js

Lines changed: 22 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)