Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
⭐ new: locale configration
Browse files Browse the repository at this point in the history
closes #11
  • Loading branch information
kazupon committed Jul 23, 2018
1 parent 008cbf3 commit 44544f7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 26 deletions.
13 changes: 13 additions & 0 deletions client-addon/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
"vue-i18n": {
"title": "Project Localizations",
"tooltip": "Localizations",
"config": {
"description": "Settings of I18n",
"prompts": {
"locale": {
"message": "Locale",
"description": "Locale of project localization"
},
"fallbackLocale": {
"message": "Fallback Locale",
"description": "Fallback locale of project localization"
}
}
},
"notification": {
"title": "Vue I18n",
"message": "Added a {locale}"
Expand Down
19 changes: 16 additions & 3 deletions client-addon/src/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
"org": {
"kazupon": {
"vue-i18n": {
"title": "プロジェクトローカライゼーション",
"tooltip": "ローカライゼーション",
"config": {
"description": "I18n の設定",
"prompts": {
"locale": {
"message": "ロケール",
"description": "プロジェクトローカライズゼーションのロケール"
},
"fallbackLocale": {
"message": "フォールバックロケール",
"description": "プロジェクトローカライズゼーションのフォールバックロケール"
}
}
},
"editor": {
"messages": "メッセージ",
"paths": "パス"
Expand Down Expand Up @@ -48,9 +63,7 @@
"title": "確認"
},
"placeholder": "パスを更新"
},
"title": "プロジェクトローカライゼーション",
"tooltip": "ローカライゼーション"
}
}
}
}
Expand Down
23 changes: 2 additions & 21 deletions generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,9 @@ const {
exists,
mkdir,
writeFile,
readFile
readEnv,
buildEnvContent
} = require('../utils')
const dotenv = require('dotenv')

function readEnv (path) {
let ret = {}
try {
ret = dotenv.parse(Buffer.from(readFile(path)))
} catch (e) {
console.warn('readEnv error:', e.message)
ret = {}
}
return ret
}

function buildEnvContent (values) {
let content = ''
Object.keys(values).forEach(key => {
content += `${key}=${values[key]}\n`
})
return content
}

module.exports = (api, options, rootOptions) => {
const { locale, fallbackLocale, localeDir, enableInSFC } = options
Expand Down
50 changes: 48 additions & 2 deletions ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ const path = require('path')
const deepmerge = require('deepmerge')
const flatten = require('flat')
const unflatten = require('flat').unflatten
const { isObject, readEnv, sortObject } = require('./utils')
const {
isObject,
readEnv,
buildEnvContent,
writeFile,
sortObject
} = require('./utils')
const i18n = require('./i18n')

function getValuesFromPath (path, messages) {
Expand Down Expand Up @@ -73,6 +79,7 @@ function writeLocaleMessages (targetPath, locale, messages, order) {

module.exports = api => {
const { getSharedData, setSharedData, watchSharedData } = api.namespace('org.kazupon.vue-i18n.')
let clientLocale = 'en'

function setupAddon (path, options) {
debug(`setupAddon: path -> ${path}, options -> ${options}`)
Expand Down Expand Up @@ -236,7 +243,6 @@ module.exports = api => {
setSharedData('locales', locales)
setSharedData('current', locale)

const clientLocale = getSharedData('clientLocale').value
debug('add-locale-action: clientLocale', clientLocale)
api.notify({
title: i18n.t('org.kazupon.vue-i18n.notification.title', clientLocale),
Expand All @@ -245,6 +251,45 @@ module.exports = api => {
})
})

api.describeConfig({
id: 'org.kazupon.vue-i18n.config',
name: 'Vue I18n',
description: 'org.kazupon.vue-i18n.config.description',
icon: '/_plugin/vue-cli-plugin-i18n/nav-logo.svg',
onRead: ({ data, cwd }) => {
const env = readEnv(`${cwd}/.env`)
const currentLocale = env['VUE_APP_I18N_LOCALE']
const defaultLocale = env['VUE_APP_I18N_FALLBACK_LOCALE']
debug('onRead', data, clientLocale, currentLocale, defaultLocale)
return {
prompts: [{
type: 'input',
name: 'locale',
message: 'org.kazupon.vue-i18n.config.prompts.locale.message',
description: 'org.kazupon.vue-i18n.config.prompts.locale.description',
value: currentLocale || 'en'
}, {
type: 'input',
name: 'fallbackLocale',
message: 'org.kazupon.vue-i18n.config.prompts.fallbackLocale.message',
description: 'org.kazupon.vue-i18n.config.prompts.fallbackLocale.description',
value: defaultLocale || 'en'
}]
}
},
onWrite: ({ answers, data, cwd }) => {
debug('onWrite', cwd, answers, data)
const envPath = `${cwd}/.env`
const env = readEnv(envPath)
const { locale, fallbackLocale } = answers
env['VUE_APP_I18N_LOCALE'] = locale
env['VUE_APP_I18N_FALLBACK_LOCALE'] = fallbackLocale
if (!writeFile(envPath, buildEnvContent(env))) {
console.error('onWrite: failed env variables')
}
}
})

api.addView({
id: 'org.kazupon.vue-i18n.views.entry',
name: 'org.kazupon.vue-i18n.routes.entry',
Expand Down Expand Up @@ -272,5 +317,6 @@ module.exports = api => {

watchSharedData('clientLocale', (val, old) => {
debug('watch `clientLocale`:', val, old)
clientLocale = val
})
}
9 changes: 9 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ function readEnv (path) {
return ret
}

function buildEnvContent (values) {
let content = ''
Object.keys(values).forEach(key => {
content += `${key}=${values[key]}\n`
})
return content
}

function sortObject (obj, order = 'asc') {
const keys = Object.keys(obj)
const sortedKeys = order === 'asc' ? keys.sort() : keys.reverse()
Expand All @@ -94,5 +102,6 @@ module.exports = {
writeFile,
readFile,
readEnv,
buildEnvContent,
sortObject
}

0 comments on commit 44544f7

Please sign in to comment.