Skip to content

Commit

Permalink
langParser.js: Generate language list dynamically
Browse files Browse the repository at this point in the history
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. Renames
``nb_NO.json`` to ``nb-NO.json`` to comply with RFC
standards.

Closes: #125
  • Loading branch information
mooncos committed Jan 15, 2018
1 parent 8960f21 commit c8ba585
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 51 deletions.
15 changes: 15 additions & 0 deletions lib/langParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs')
const iso639 = require('iso-639-1')

const availableLanguages = {}

fs.readdir(`${__dirname}/../static/i18n`, (err, items) => {
items.forEach(function(value) {
const langName = value.substring(0, value.indexOf('.'))
const normalizedName = langName.split('_')[0]
const nativeName = iso639.getNativeName(normalizedName)
availableLanguages[nativeName] = langName
})
})

module.exports = availableLanguages
71 changes: 27 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"generate": "node ./lib/generate.js",
"gather": "node ./lib/gather.js",
"main": "node ./lib/main.js",
"languages": "node ./lib/langParser.js",
"clean": "rm -rf out/*",
"dev": "webpack-dev-server --progress",
"bundle": "NODE_ENV=production webpack --progress",
"bundle": "set NODE_ENV=production && webpack --progress",
"build": "npm run bundle && npm run gather && npm run scrape && npm run planet && npm run main && npm run generate",
"build:clean": "npm run clean && npm run build",
"test": "jest"
Expand All @@ -27,6 +28,7 @@
"find-rss": "^1.6.4",
"glob": "^7.1.2",
"graphql-client": "^2.0.0",
"iso-639-1": "^2.0.3",
"jquery": "^3.2.1",
"jquery.i18n": "git+https://github.com/wikimedia/jquery.i18n.git",
"json2yaml": "^1.1.0",
Expand All @@ -53,6 +55,7 @@
"eslint-plugin-prettier": "^2.3.1",
"expose-loader": "^0.7.4",
"extract-text-webpack-plugin": "^3.0.2",
"generate-json-webpack-plugin": "^0.2.2",
"jest": "^22.0.3",
"mockdate": "^2.0.2",
"postcss-loader": "^2.0.9",
Expand Down
1 change: 1 addition & 0 deletions src/js/languages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"English":"en","Español":"es","Norsk bokmål":"nb_NO","język polski":"pl"}
8 changes: 2 additions & 6 deletions src/js/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import init from './app'
const langs = require('./languages.json')

var browserLocale

Expand Down Expand Up @@ -26,12 +27,7 @@ function updateTranslation(localex) {
}

$(window).on('load', function() {
var localeOptions = {
English: 'en',
Español: 'es',
Polski: 'pl',
'Norwegian Bokmål': 'nb_NO',
}
var localeOptions = langs

var locList = $('#lang-select')
$.each(localeOptions, function(key, value) {
Expand Down
1 change: 1 addition & 0 deletions tests/__data__/test-languages.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tests/lib/langParser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* langParser Library Test
*/

const fs = require('fs')
const iso639 = require('iso-639-1')

const testLangs = ['en', 'es', 'ja_AK', 'fr']
const expectedLangs = ['English', 'Español', '日本語', 'Français']

describe('lib.langParser', () => {
it('should get nativeName for each language', () => {
const resultLangs = []
testLangs.forEach(function(value) {
const normalizedName = value.split('_')[0]
const nativeName = iso639.getNativeName(normalizedName)
resultLangs.push(nativeName)
})

expect(resultLangs).toEqual(expectedLangs)
})
})
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const CleanWebpackPlugin = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ManifestPlugin = require('webpack-manifest-plugin')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const GenerateJsonPlugin = require('generate-json-webpack-plugin')
const path = require('path')
const generatedLangList = require('./lib/langParser')

const isProduction = process.env.NODE_ENV === 'production'
const hash = isProduction ? '.[hash]' : ''
Expand Down Expand Up @@ -104,6 +106,7 @@ module.exports = {
]),
new ExtractTextPlugin(`[name]${hash}.css`),
new ManifestPlugin(),
new GenerateJsonPlugin('../src/js/languages.json', generatedLangList),
].concat(
isProduction
? [
Expand Down

0 comments on commit c8ba585

Please sign in to comment.