barbosa89/laravel-vue-i18n-generator

从您的Laravel翻译中生成与vue-i18n兼容的包含文件。

1.0.0 2022-05-27 21:29 UTC

README

run-tests Latest Stable Version License

Laravel包,允许您使用vue-i18n或vuex-i18n将Laravel本地化与VueJS前端共享。支持的版本:Laravel >=6.x。

Laravel 9注意!

框架的新版本将语言文件夹从资源文件夹中移出,因此您需要更新配置文件中的langPath。

'langPath' => env('VUE_I18N_OUTPUT_PATH', '/lang'),

您还可以设置环境变量来指定路径。

VUE_I18N_OUTPUT_PATH='/lang'

安装包

在您的项目中

composer require barbosa89/laravel-vue-i18n-generator --dev

接下来,发布包默认配置

php artisan vendor:publish --provider="MartinLindhe\VueInternationalizationGenerator\GeneratorProvider"

使用vue-i18n

接下来,您需要安装两种支持的VueJs i18n库之一。我们默认支持vue-i18n。除此之外,我们还支持vuex-i18n

如果您选择默认选项,您只需要通过您喜欢的包管理器安装库。

vue-i18n

npm i --save-dev vue-i18n
yarn add vue-i18n

然后使用以下命令生成包含文件

php artisan vue-i18n:generate

假设您正在使用一个较新的vue-i18n版本(>=6.x, <=8.x),调整您的vue应用如下

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: {
       ...
    }
}

对于较旧的vue-i18n (5.x),初始化看起来如下

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])
});

...

使用vuex-i18n

vuex-i18n

npm i --save-dev vuex-i18n
yarn add vuex-i18n vuex

接下来,打开config/vue-i18n-generator.php并做以下更改

- 'i18nLib' => 'vue-i18n',
+ 'i18nLib' => 'vuex-i18n',

然后使用以下命令生成包含文件

php artisan vue-i18n:generate

假设您正在使用一个较新的vuex-i18n版本,调整您的vue应用如下

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')]
});

输出格式

您可以使用--format选项从es6、umd或json中指定输出格式。(默认为es6)

php artisan vue-i18n:generate --format {es6,umd,json}

UMD模块的使用案例

php artisan vue-i18n:generate --format umd

UMD模块可以导入到浏览器、构建系统、node等。

现在您可以将生成的脚本包含在浏览器中作为普通脚本,并通过window.vuei18nLocales引用它。

<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])
})

您还可以像上面所述那样在构建系统中require/import它。

这样做的一个优点是,当翻译文件更改或保存时,您不必每次都构建JavaScript。一个好的例子是,如果您有一个可以读取和写入翻译文件的后端(如Backpack),您可以在那里监听保存事件并调用vue-i18n-generator。

生成多个文件

有时您可能希望生成多个文件,因为您想利用懒加载。为此,您可以在目标目录中指定生成器生成多个文件。

有两种选项

  1. 使用--multi开关按laravel模块语言文件生成一个文件
  2. 使用--multi-locales开关按区域设置生成一个文件
php artisan vue-i18n:generate --multi{-locales}

参数

生成器调整字符串以与vue-i18n的命名格式化一起使用,因此您可以使用参数重用Laravel翻译。

resource/lang/message.php

return [
    'hello' => 'Hello :name',
];

在vue-i18n-locales.generated.js中

...
    "hello": "Hello {name}",
...

Blade模板

<div class="message">
    <p>{{ trans('message.hello', ['name' => 'visitor']) }}</p>
</div>

Vue模板

<div class="message">
    <p>{{ $t('message.hello', {name: 'visitor'}) }}</p>
</div>

许可

根据MIT