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.

Closes: coala#125
  • Loading branch information
mooncos committed Jan 15, 2018
1 parent 8960f21 commit 6948c27
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 50 deletions.
1 change: 1 addition & 0 deletions .coafile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ max_line_length = 80
use_spaces = True

[all.whitespace]
ignore += src/js/languages.json
bears = SpaceConsistencyBear
default_actions = *: ApplyPatchAction

Expand Down
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.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 +54,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 6948c27

Please sign in to comment.