-
-
Notifications
You must be signed in to change notification settings - Fork 918
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
28 changed files
with
383 additions
and
181 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
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,11 @@ | ||
/** | ||
* constants | ||
*/ | ||
|
||
let {NODE_ENV} = process.env | ||
|
||
module.exports = { | ||
isDev: NODE_ENV === 'development', | ||
defaultLang: 'zh_cn' | ||
} | ||
|
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,73 @@ | ||
/** | ||
* multi language support | ||
*/ | ||
|
||
const {isDev, defaultLang} = require('./constants') | ||
const fs = require('fs') | ||
const _ = require('lodash') | ||
const {resolve} = require('path') | ||
const {userConfig} = require('./user-config-controller') | ||
|
||
let path = (isDev | ||
? '../' | ||
: '') + | ||
'../node_modules/electerm-locales/locales' | ||
let localeFolder = resolve(__dirname, path) | ||
|
||
//languages array | ||
const langs = fs.readdirSync(localeFolder) | ||
.map(fileName => { | ||
let filePath = resolve(localeFolder, fileName) | ||
let lang = require(filePath) | ||
return { | ||
path: filePath, | ||
id: fileName.replace('.js', ''), | ||
name: lang.name | ||
} | ||
}) | ||
const langMap = langs.reduce((prev, l) => { | ||
prev[l.id] = l | ||
return prev | ||
}, {}) | ||
|
||
const getLang = () => { | ||
if (userConfig.language) { | ||
return userConfig.language | ||
} | ||
let languageEnv = process.env.LANGUAGE.toLowerCase() | ||
if (langMap[languageEnv]) { | ||
return languageEnv | ||
} | ||
return defaultLang | ||
} | ||
|
||
let language = getLang() | ||
|
||
exports.lang = require(langMap[language].path).lang | ||
|
||
/** | ||
* string translate | ||
* @param {string} id eg: 'menu.cut' | ||
*/ | ||
exports.e = id => { | ||
return _.get(exports.lang, id) || id | ||
} | ||
|
||
exports.prefix = prefix => { | ||
return (id) => { | ||
return _.get(exports.lang, `${prefix}.${id}`) || id | ||
} | ||
} | ||
|
||
exports.langs = langs | ||
|
||
exports.saveLangConfig = (saveUserConfig, userConfig) => { | ||
if (!userConfig.language) { | ||
saveUserConfig({ | ||
language: defaultLang | ||
}) | ||
language = defaultLang | ||
} | ||
} | ||
|
||
|
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
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
Oops, something went wrong.