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.

Closes: coala#125
  • Loading branch information
mooncos committed Jan 14, 2018
1 parent 8960f21 commit a5b9a69
Show file tree
Hide file tree
Showing 6 changed files with 47 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`, function(err, items) {
var avaliableLanguages = {
Languages: {},
}
for (var i = 0; i < items.length; i++) {
var langName = items[i].substring(0, items[i].indexOf('.'))
var normalizedName = langName.split('-')[0]
var nativeName = iso639.getNativeName(normalizedName)
avaliableLanguages['Languages'][nativeName] = langName
}
fs.writeFileSync(
`${__dirname}/../src/js/languages.js`,
'export default ' +
JSON.stringify(avaliableLanguages) +
' // 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.

0 comments on commit a5b9a69

Please sign in to comment.