refactor: deps cleanup + fetch locales task
This commit is contained in:
56
dev/tasks/localization.js
Normal file
56
dev/tasks/localization.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const Promise = require('bluebird')
|
||||
const colors = require('colors/safe')
|
||||
const fs = Promise.promisifyAll(require('fs-extra'))
|
||||
const path = require('path')
|
||||
const request = require('request-promise')
|
||||
const yaml = require('js-yaml')
|
||||
const _ = require('lodash')
|
||||
|
||||
const config = yaml.safeLoad(fs.readFileSync(path.join(process.cwd(), 'dev/config/config.yml'), 'utf8'))
|
||||
|
||||
/**
|
||||
* Fetch Localization Resources from Lokalise
|
||||
*/
|
||||
const fetchLocalizationResources = async () => {
|
||||
console.info(colors.green('Fetching latest localization resources...'))
|
||||
let langs = await request({
|
||||
method: 'POST',
|
||||
uri: `${config.lokalise.api}/string/list`,
|
||||
form: {
|
||||
api_token: config.lokalise.key,
|
||||
id: config.lokalise.project
|
||||
},
|
||||
json: true
|
||||
})
|
||||
if (langs && langs.strings && _.isPlainObject(langs.strings)) {
|
||||
_.forIn(langs.strings, (langData, langKey) => {
|
||||
let lang = {}
|
||||
let langTotal = 0
|
||||
langData.forEach(item => {
|
||||
if (item.is_archived === '1' || _.includes(item.key, '::')) { return }
|
||||
let keyParts = item.key.split(':')
|
||||
let keyNamespace = (keyParts.length > 1) ? _.head(keyParts) : 'common'
|
||||
let keyString = _.last(keyParts)
|
||||
_.set(lang, `${keyNamespace}.${keyString}`, item.translation)
|
||||
langTotal++
|
||||
})
|
||||
_.forOwn(lang, (langObject, langNamespace) => {
|
||||
let langYaml = yaml.safeDump(langObject, {
|
||||
indent: 2,
|
||||
sortKeys: true,
|
||||
lineWidth: 2048
|
||||
})
|
||||
fs.outputFileSync(path.join(process.cwd(), `server/locales/${langKey}/${langNamespace}.yml`), langYaml, 'utf8')
|
||||
})
|
||||
console.info(colors.grey(`└─ ${langKey} - ${langTotal} keys written`))
|
||||
})
|
||||
} else {
|
||||
throw new Error('Failed to fetch language list from Lokalise API.')
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
fetchLocalizationResources()
|
||||
} catch (err) {
|
||||
console.error(colors.red(err))
|
||||
}
|
Reference in New Issue
Block a user