diff --git a/client-addon/src/locales/en.json b/client-addon/src/locales/en.json index 408b2d2..f8d0cb4 100644 --- a/client-addon/src/locales/en.json +++ b/client-addon/src/locales/en.json @@ -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}" diff --git a/client-addon/src/locales/ja.json b/client-addon/src/locales/ja.json index 7fe62e8..81fdd92 100644 --- a/client-addon/src/locales/ja.json +++ b/client-addon/src/locales/ja.json @@ -2,6 +2,21 @@ "org": { "kazupon": { "vue-i18n": { + "title": "プロジェクトローカライゼーション", + "tooltip": "ローカライゼーション", + "config": { + "description": "I18n の設定", + "prompts": { + "locale": { + "message": "ロケール", + "description": "プロジェクトローカライズゼーションのロケール" + }, + "fallbackLocale": { + "message": "フォールバックロケール", + "description": "プロジェクトローカライズゼーションのフォールバックロケール" + } + } + }, "editor": { "messages": "メッセージ", "paths": "パス" @@ -48,9 +63,7 @@ "title": "確認" }, "placeholder": "パスを更新" - }, - "title": "プロジェクトローカライゼーション", - "tooltip": "ローカライゼーション" + } } } } diff --git a/generator/index.js b/generator/index.js index e5cddc9..e6ace84 100644 --- a/generator/index.js +++ b/generator/index.js @@ -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 diff --git a/ui.js b/ui.js index e4d02d1..4c8d33c 100644 --- a/ui.js +++ b/ui.js @@ -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) { @@ -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}`) @@ -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), @@ -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', @@ -272,5 +317,6 @@ module.exports = api => { watchSharedData('clientLocale', (val, old) => { debug('watch `clientLocale`:', val, old) + clientLocale = val }) } diff --git a/utils.js b/utils.js index ba4db54..bbe651e 100644 --- a/utils.js +++ b/utils.js @@ -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() @@ -94,5 +102,6 @@ module.exports = { writeFile, readFile, readEnv, + buildEnvContent, sortObject }