windstep / laravel-vue-i18n-generator
从您的Laravel翻译中生成一个与vue-i18n兼容的包含文件。
Requires
- php: >=5.5.0
- ext-json: *
- ext-mbstring: *
- illuminate/console: ^5.0|^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- orchestra/testbench: 3.1.*
- phpunit/phpunit: ~4.7
- dev-master
- 0.2.0
- 0.1.47
- 0.1.46
- 0.1.45
- 0.1.44
- 0.1.43
- 0.1.42
- 0.1.41
- 0.1.40
- 0.1.39
- 0.1.38
- 0.1.37
- 0.1.36
- 0.1.35
- 0.1.34
- 0.1.33
- 0.1.32
- 0.1.31
- 0.1.30
- 0.1.29
- 0.1.28
- 0.1.27
- 0.1.26
- 0.1.25
- 0.1.24
- 0.1.23
- 0.1.22
- 0.1.21
- 0.1.20
- 0.1.19
- 0.1.18
- 0.1.17
- 0.1.16
- 0.1.15
- 0.1.14
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/composer/symfony/http-foundation-2.7.51
- dev-fixes
This package is auto-updated.
Last update: 2024-09-16 19:49:04 UTC
README
Laravel 5包,允许您使用vue-i18n或vuex-i18n与您的vue前端共享Laravel本地化。
来自https://github.com/martinlindhe/laravel-vue-i18n-generator的分支
Laravel 5.7注意事项!
由于Laravel 5.7中配置路径已更改,为了使此包正常工作,您需要配置config\vue-i18n-generator.php中jsPath和jsFile的正确路径。
安装包
在您的项目中: composer require librenms/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。
生成多个文件
有时您可能想要生成多个文件,因为您想要使用懒加载。因此,您可以在目标目录内指定生成器生成多个文件。
有两种选项
- 每个Laravel模块语言文件使用
--multi
切换生成一个文件 - 每个区域设置使用
--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协议