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

Commit 4b64d81

Browse files
committed
⭐ new: env variables
1 parent a12be8e commit 4b64d81

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,35 @@ Vue CLI 3 plugin to add vue-i18n to your Vue Project
88
## :star: Features
99
- [vue-i18n](https://github.com/kazupon/vue-i18n) basic scaffolding
1010
- Locale messages in Single File components with [vue-i18n-loader](https://github.com/kazupon/vue-i18n-loader)
11+
- Env Variables
1112

1213
## :rocket: Getting Started
1314
If yon don't have a project created with Vue CLI 3:
1415

1516
```sh
16-
$ vue create my-vue-app
17+
vue create my-vue-app
1718
```
1819

1920
Install the plugin into your project:
2021

2122
```sh
22-
$ cd my-vue-app
23-
$ vue add i18n
23+
cd my-vue-app
24+
vue add i18n
2425
```
2526

27+
## :clipboard: Env variables
28+
When vue-i18n code files had been scaffolded into your project, the following env variables generate into `.env`:
29+
30+
- **`VUE_APP_I18N_LOCALE`**
31+
32+
The locale of project localization, default `en`.
33+
34+
- **`VUE_APP_I18N_FALLBACK_LOCALE`**
35+
36+
The locale of project fallback localization, default `en`.
37+
38+
These env variables are read in `src/i18n.(js|ts)`.
39+
2640
## :wrench: Configrations
2741

2842
`vue-cli-plugin-i18n` have some plugin options in `vue.config.js`:

generator/index.js

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,63 @@ function checkInstalled (target) {
55
let ret = true
66
try {
77
const resolveModule = require(path.resolve(target))
8-
if (!resolveModule) { ret = false }
8+
if (!resolveModule) {
9+
ret = false
10+
}
11+
} catch (e) {
12+
ret = false
13+
}
14+
return ret
15+
}
16+
17+
function exists (path) {
18+
let ret = true
19+
try {
20+
fs.accessSync(path, fs.constants.F_OK)
21+
} catch (e) {
22+
ret = false
23+
}
24+
return ret
25+
}
26+
27+
function mkdir (path) {
28+
let ret = true
29+
try {
30+
fs.mkdirSync(path)
31+
} catch (e) {
32+
ret = false
33+
}
34+
return ret
35+
}
36+
37+
function writeFile (path, content) {
38+
let ret = true
39+
try {
40+
fs.writeFileSync(path, content, { encoding: 'utf8' })
941
} catch (e) {
1042
ret = false
1143
}
1244
return ret
1345
}
1446

47+
function readFile (path) {
48+
let ret = ''
49+
try {
50+
ret = fs.readFileSync(path, { encoding: 'utf8' })
51+
} catch (e) {
52+
ret = ''
53+
}
54+
return ret
55+
}
56+
1557
module.exports = (api, options, rootOptions) => {
16-
const { enableInSFC } = options
58+
const { locale, fallbackLocale, enableInSFC } = options
1759

1860
try {
1961
const tsPath = api.resolve('src/main.ts')
2062
const jsPath = api.resolve('src/main.js')
21-
const tsExists = fs.existsSync(tsPath)
22-
const jsExists = fs.existsSync(jsPath)
63+
const tsExists = exists(tsPath)
64+
const jsExists = exists(jsPath)
2365

2466
if (!tsExists && !jsExists) {
2567
api.exitLog('No entry found', 'error')
@@ -67,9 +109,6 @@ module.exports = (api, options, rootOptions) => {
67109
* render templates
68110
*/
69111

70-
// common i18n templates
71-
api.render('./templates/common', { ...additionalOptions })
72-
73112
// i18n templates for program language
74113
api.render(`./templates/${lang}`, { ...additionalOptions })
75114

@@ -88,6 +127,59 @@ module.exports = (api, options, rootOptions) => {
88127
api.exitLog(`unexpected error in vue-cli-plugin-i18n: ${e.message}`, 'error')
89128
}
90129

130+
api.onCreateComplete(() => {
131+
const defaultLocaleMessages = JSON.stringify({
132+
message: 'hello i18n !!'
133+
}, null, 2)
134+
135+
function writeLocaleFile (path) {
136+
if (!exists(path)) {
137+
if (!writeFile(path, defaultLocaleMessages)) {
138+
api.exitLog(`cannot make ${path}`, 'error')
139+
}
140+
} else {
141+
console.warn(`already exist ${path}`)
142+
}
143+
}
144+
145+
const localesDirPath = api.resolve('./src/locales')
146+
if (!exists(localesDirPath)) {
147+
if (!mkdir(localesDirPath)) {
148+
api.exitLog(`cannot make ${localesDirPath}`, 'error')
149+
}
150+
}
151+
152+
writeLocaleFile(api.resolve(`./src/locales/${locale}.json`))
153+
if (locale !== fallbackLocale) {
154+
writeLocaleFile(api.resolve(`./src/locales/${fallbackLocale}.json`))
155+
}
156+
157+
const envPath = api.resolve('.env')
158+
let content = exists(envPath) ? readFile(envPath) : ''
159+
160+
if (content.indexOf('VUE_APP_I18N_LOCALE=') === -1) {
161+
content += `VUE_APP_I18N_LOCALE=${locale}\n`
162+
} else {
163+
content = content.replace(
164+
/VUE_APP_I18N_LOCALE=(.*)\n/,
165+
`VUE_APP_I18N_LOCALE=${locale}`
166+
)
167+
}
168+
169+
if (content.indexOf('VUE_APP_I18N_FALLBACK_LOCALE=') === -1) {
170+
content += `VUE_APP_I18N_FALLBACK_LOCALE=${fallbackLocale}\n`
171+
} else {
172+
content = content.replace(
173+
/VUE_APP_I18N_FALLBACK_LOCALE=(.*)\n/,
174+
`VUE_APP_I18N_FALLBACK_LOCALE=${fallbackLocale}`
175+
)
176+
}
177+
178+
if (!writeFile(envPath, content)) {
179+
api.exitLog(`cannot write ${envPath}`, 'error')
180+
}
181+
})
182+
91183
api.postProcessFiles(files => {
92184
if (!api.hasPlugin('typescript')) { return }
93185
const tsConfigRaw = files['tsconfig.json']

generator/templates/common/src/locales/en.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

generator/templates/js/src/i18n.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function loadLocaleMessages () {
1717
}
1818

1919
export default new VueI18n({
20-
locale: 'en',
21-
fallbackLocale: 'en',
20+
locale: process.env.VUE_APP_I18N_LOCALE || 'en',
21+
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',
2222
messages: loadLocaleMessages()
2323
})

generator/templates/ts/src/i18n.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function loadLocaleMessages (): LocaleMessages {
1717
}
1818

1919
export default new VueI18n({
20-
locale: 'en',
21-
fallbackLocale: 'en',
20+
locale: process.env.VUE_APP_I18N_LOCALE || 'en',
21+
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',
2222
messages: loadLocaleMessages()
2323
})

prompts.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
module.exports = [{
2+
type: 'input',
3+
name: 'locale',
4+
message: 'The locale of project localization.',
5+
validate: input => !!input,
6+
default: 'en'
7+
}, {
8+
type: 'input',
9+
name: 'fallbackLocale',
10+
message: 'The locale of project fallback localization.',
11+
validate: input => !!input,
12+
default: 'en'
13+
}, {
214
type: 'confirm',
315
name: 'enableInSFC',
416
message: 'Enable locale messages in Single file components ?',

0 commit comments

Comments
 (0)