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: coala#125
  • Loading branch information
mooncos committed Jan 14, 2018
1 parent 8960f21 commit 8a2cd2c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ out
yarn.lock
.DS_Store
**.orig
src/js/languages.js
20 changes: 20 additions & 0 deletions lib/langParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs')
const iso639 = require('iso-639-1')

fs.readdir(`${__dirname}/../static/i18n`, (err, items) => {
const availableLanguages = {
Languages: {},
}
items.forEach(function(value) {
const langName = value.substring(0, value.indexOf('.'))
const normalizedName = langName.split('-')[0]
const nativeName = iso639.getNativeName(normalizedName)
availableLanguages['Languages'][nativeName] = langName
})
fs.writeFileSync(
`${__dirname}/../src/js/languages.js`,
'export default ' +
JSON.stringify(availableLanguages) +
' // eslint-disable-line'
)
})
65 changes: 21 additions & 44 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"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",
"build": "npm run bundle && npm run gather && npm run scrape && npm run planet && npm run main && npm run generate",
"build": "npm run languages && 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 Down
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'
import Languages from './languages'

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 = Languages['Languages']

var locList = $('#lang-select')
$.each(localeOptions, function(key, value) {
Expand Down
File renamed without changes.
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.

42 changes: 42 additions & 0 deletions tests/lib/langParser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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)
})

it('should create a file given language codes', () => {
const langDict = {
Languages: {},
}
testLangs.forEach(function(value) {
const normalizedName = value.split('-')[0]
const nativeName = iso639.getNativeName(normalizedName)
langDict['Languages'][nativeName] = value
})

const expectedData = fs
.readFileSync(`${__dirname}/../__data__/test-languages.js`)
.toString()

const output =
'export default ' + JSON.stringify(langDict) + ' // eslint-disable-line'

expect(output).toEqual(expectedData)
})
})

0 comments on commit 8a2cd2c

Please sign in to comment.