larajs / i18n
从您的 Laravel 翻译中生成一个与 vue-i18n 兼容的包含文件。
Requires
- php: ^7.4|^8.0|^8.1
- ext-json: *
- ext-mbstring: *
- illuminate/console: ~8.0|~9.0|~10.0|~11.0
- illuminate/support: ~8.0|~9.0|~10.0|~11.0
Requires (Dev)
- phpunit/phpunit: ~9.0
This package is auto-updated.
Last update: 2024-09-13 04:57:21 UTC
README
已不再维护
Laravel 5 包,允许您使用 Laravel 本地化 与您的 vue 前端共享,使用 vue-i18n 或 vuex-i18n。
Laravel 5.7 注意!
在 Laravel 5.7 中,配置路径已更改,为了使此包正常工作,您需要在您的 config\vue-i18n-generator.php
中配置正确的 jsPath 和 jsFile 路径。
安装包
在您的项目中: composer require martinlindhe/laravel-vue-i18n-generator --dev
对于 Laravel 5.4 及以下版本
对于框架的旧版本
在 config/app.php
中注册服务提供者
MartinLindhe\VueInternationalizationGenerator\GeneratorProvider::class,
接下来,发布包默认配置
php artisan vendor:publish --provider="MartinLindhe\VueInternationalizationGenerator\GeneratorProvider"
使用 vue-i18n
接下来,您需要安装两种支持的 VueJs i18n 库之一。我们默认支持 vue-i18n。除此之外,我们还支持 vuex-i18n。
如果选择默认选项,您只需要通过您最喜欢的包管理器安装库。
vue-i18n
npm i --save vue-i18n
yarn add vue-i18n
然后使用以下命令生成包含文件
php artisan vue-i18n:generate
假设您正在使用 vue-i18n 的最新版本(>=6.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 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。
生成多个文件
有时您可能想生成多个文件,因为您想利用懒加载。因此,您可以在目标目录中指定生成器生成多个文件。
有两种选项
- 使用
--multi
开关为每个 Laravel 模块语言文件生成一个文件 - 使用
--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>
注意
- 生成的文件是一个 ES6 模块。
更复杂的复数本地化,如在此处所述(此处),由于vue-i18n或vuex-i18n均不支持此功能,因此不被支持。
许可证
根据MIT许可证