forked from coala/gci-leaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langParser.js: Generate language list dynamically
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
Showing
8 changed files
with
90 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ out | |
yarn.lock | ||
.DS_Store | ||
**.orig | ||
src/js/languages.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |