Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langParser.js: Generate language list dynamically #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you want to prettify the output.

GenerateJsonPlugin(<file>, <json>, [replacer], [space])

Fill out that space argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

].concat(
isProduction
? [
Expand Down