d4rknazz / laravel-vue-i18n-generator
从您的 Laravel 翻译中生成与 vue-i18n 兼容的包含文件。
dev-master
2024-03-19 18:18 UTC
Requires
- php: >=5.5.0
- ext-json: *
- ext-mbstring: *
- illuminate/console: ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- orchestra/testbench: ^7|^8.0
- phpunit/phpunit: ^9
This package is not auto-updated.
Last update: 2024-10-02 18:46:09 UTC
README
Laravel 5 包,允许您使用 Laravel 本地化 与您的 vue 前端共享,通过 vue-i18n 或 vuex-i18n。
来自 https://github.com/martinlindhe/laravel-vue-i18n-generator 的分支
Laravel 5.7 注意!
Laravel 5.7 中配置路径已更改,为了使此包正常工作,您需要在您的 config\vue-i18n-generator.php
中配置正确的 jsPath 和 jsFile 路径。
安装包
在您的项目中
### For Laravel 5.4 and below:
For older versions of the framework:
Register the service provider in ```config/app.php```
```php
MartinLindhe\VueInternationalizationGenerator\GeneratorProvider::class,
```
Next, publish the package default config:
```
php artisan vendor:publish --provider="MartinLindhe\VueInternationalizationGenerator\GeneratorProvider"
```
## Using vue-i18n
Next, you need to install one out of two supported VueJs i18n libraries. We support [vue-i18n](https://github.com/kazupon/vue-i18n) as default library. Beside that we also support [vuex-i18n](https://github.com/dkfbasel/vuex-i18n).
When you go with the default option, you only need to install the library through your favorite package manager.
### vue-i18n
```
npm i --save vue-i18n
```
```
yarn add vue-i18n
```
Then generate the include file with
```
php artisan vue-i18n:generate
```
Assuming you are using a recent version of vue-i18n (>=6.x), adjust your vue app with something like:
```js
import Vue from 'vue';
import VueInternationalization from 'vue-i18n';
import Locale from './vue-i18n-locales.generated';
Vue.use(VueInternationalization);
const lang = document.documentElement.lang.substr(0, 2);
// or however you determine your current app locale
const i18n = new VueInternationalization({
locale: lang,
messages: Locale
});
const app = new Vue({
el: '#app',
i18n,
components: {
...
}
}
```
For older vue-i18n (5.x), the initialization looks something like:
```js
import Vue from 'vue';
import VueInternationalization from 'vue-i18n';
import Locales from './vue-i18n-locales.generated.js';
Vue.use(VueInternationalization);
Vue.config.lang = 'en';
Object.keys(Locales).forEach(function (lang) {
Vue.locale(lang, Locales[lang])
});
...
```
## Using vuex-i18n
### vuex-i18n
```
npm i --save vuex-i18n
```
```
yarn add vuex-i18n vuex
```
Next, open `config/vue-i18n-generator.php` and do the following changes:
```diff
- 'i18nLib' => 'vue-i18n',
+ 'i18nLib' => 'vuex-i18n',
```
Then generate the include file with
```
php artisan vue-i18n:generate
```
Assuming you are using a recent version of vuex-i18n, adjust your vue app with something like:
```js
import Vuex from 'vuex';
import vuexI18n from 'vuex-i18n';
import Locales from './vue-i18n-locales.generated.js';
const store = new Vuex.Store();
Vue.use(vuexI18n.plugin, store);
Vue.i18n.add('en', Locales.en);
Vue.i18n.add('de', Locales.de);
// set the start locale to use
Vue.i18n.set(Spark.locale);
require('./components/bootstrap');
var app = new Vue({
store,
mixins: [require('spark')]
});
```
## Output Formats
You can specify the output formats from `es6`, `umd`, or `json` with the `--format` option. (defaults to `es6`)
```
php artisan vue-i18n:generate --format {es6,umd,json}
```
### Use case example for UMD module
```
php artisan vue-i18n:generate --format umd
```
An UMD module can be imported into the browser, build system, node and etc.
Now you can include the generated script in the browser as a normal script and reference it with window.vuei18nLocales.
```vue
<script src="{{ asset('js/vue-i18n-locales.generated.js') }}"></script>
// in your js
Vue.use(VueI18n)
Vue.config.lang = Laravel.language
Object.keys(window.vuei18nLocales).forEach(function (lang) {
Vue.locale(lang, window.vuei18nLocales[lang])
})
```
You can still require/import it in your build system as stated above.
One advantage of doing things like this is you are not obligated to do a build of your javascript each time a the translation files get changed/saved. A good example is if you have a backend that can read and write to your translation files (like Backpack). You can listen to a save event there and call vue-i18n-generator.
## Generating Multiple Files
Sometimes you may want to generate multiple files as you want to make use of lazy loading. As such, you can specify that the generator produces multiple files within the destination directory.
There are two options:
1. One file per laravel module language file using switch ```--multi```
2. One file per locale using switch ```--multi-locales```
```
php artisan vue-i18n:generate --multi{-locales}
```
## Parameters
The generator adjusts the strings in order to work with vue-i18n's named formatting,
so you can reuse your Laravel translations with parameters.
resource/lang/message.php:
```php
return [
'hello' => 'Hello :name',
];
```
in vue-i18n-locales.generated.js:
```js
...
"hello": "Hello {name}",
...
```
Blade template:
```php
<div class="message">
<p>{{ trans('message.hello', ['name' => 'visitor']) }}</p>
</div>
```
Vue template:
```js
<div class="message">
<p>{{ $t('message.hello', {name: 'visitor'}) }}</p>
</div>
```
## Notices
- The generated file is an ES6 module.
The more sophisticated pluralization localization as described [here](https://laravel.net.cn/docs/5.5/localization#pluralization) is not supported since neither vue-i18n or vuex-i18n support this.
# License
Under [MIT](LICENSE)