All Projects → martinlindhe → Laravel Vue I18n Generator

martinlindhe / Laravel Vue I18n Generator

Licence: mit
Generates a vue-i18n compatible include file from your Laravel translations

Projects that are alternatives of or similar to Laravel Vue I18n Generator

Laravel Log Reader
A simple and beautiful laravel log reader
Stars: ✭ 298 (-1.97%)
Mutual labels:  laravel
Laravel Form Components
A set of Blade components to rapidly build forms with Tailwind CSS (v1.0 and v2.0) and Bootstrap 4. Supports validation, model binding, default values, translations, Laravel Livewire, includes default vendor styling and fully customizable!
Stars: ✭ 295 (-2.96%)
Mutual labels:  laravel
Eloquent Sluggable
Easy creation of slugs for your Eloquent models in Laravel
Stars: ✭ 3,321 (+992.43%)
Mutual labels:  laravel
Stream Laravel
Laravel Client - Build Activity Feeds & Streams with GetStream.io
Stars: ✭ 299 (-1.64%)
Mutual labels:  laravel
Lawoole
Lawoole is a high-performance PHP framework based on Laravel and Swoole
Stars: ✭ 300 (-1.32%)
Mutual labels:  laravel
Providers
A Collection of Providers for Laravel Socialite
Stars: ✭ 301 (-0.99%)
Mutual labels:  laravel
Laravel Short Schedule
Schedule artisan commands to run at a sub-minute frequency
Stars: ✭ 297 (-2.3%)
Mutual labels:  laravel
Laravel
A Vimeo bridge for Laravel
Stars: ✭ 302 (-0.66%)
Mutual labels:  laravel
Laravel Google Drive Demo
Laravel & Google Drive Storage - Demo project with Laravel 5.4
Stars: ✭ 299 (-1.64%)
Mutual labels:  laravel
Laravel Generator
InfyOm Laravel Generator - API, Scaffold, Tests, CRUD Laravel Generator
Stars: ✭ 3,306 (+987.5%)
Mutual labels:  laravel
Laravel Welcome Notification
Send a welcome notification to new users
Stars: ✭ 299 (-1.64%)
Mutual labels:  laravel
Roastandbrew
Updated content available! We learned a lot since we originally wrote this article. We now have this updated for Laravel 8, Vue, and NuxtJS 👉 https://srvrsi.de/book
Stars: ✭ 300 (-1.32%)
Mutual labels:  laravel
Moell Blog
基于 Laravel 开发,支持 Markdown 语法的博客
Stars: ✭ 301 (-0.99%)
Mutual labels:  laravel
Invoices
Generate PDF invoices for your customers in laravel
Stars: ✭ 298 (-1.97%)
Mutual labels:  laravel
Laravel Sketchpad
An innovative front-end environment for interactive Laravel development
Stars: ✭ 302 (-0.66%)
Mutual labels:  laravel
Borgert Cms
Borgert is a CMS Open Source created with Laravel Framework 5.6
Stars: ✭ 298 (-1.97%)
Mutual labels:  laravel
Laravel Api Boilerplate
Laravel API Boilerplate | Please consult the Wiki !
Stars: ✭ 300 (-1.32%)
Mutual labels:  laravel
Laravel Starter
A CMS like modular starter application project built with Laravel 8.x.
Stars: ✭ 299 (-1.64%)
Mutual labels:  laravel
Decoy
A Laravel model-based CMS
Stars: ✭ 303 (-0.33%)
Mutual labels:  laravel
Adminlte Generator
Boilerplate of Laravel with InfyOm Laravel Generator for AdminLTE Templates
Stars: ✭ 300 (-1.32%)
Mutual labels:  laravel

About

NO LONGER MAINTAINED

Build Status

Laravel 5 package that allows you to share your Laravel localizations with your vue front-end, using vue-i18n or vuex-i18n.

Laravel 5.7 notice!

Configuration paths have changed in Laravel 5.7, in order for this package to function properly you need to configure correct paths for jsPath and jsFile in your config\vue-i18n-generator.php.

Install the package

In your project: composer require martinlindhe/laravel-vue-i18n-generator --dev

For Laravel 5.4 and below:

For older versions of the framework:

Register the service provider in config/app.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 as default library. Beside that we also support 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:

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:

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:

- '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:

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.

<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:

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

in vue-i18n-locales.generated.js:

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

Blade template:

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

Vue template:

<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 is not supported since neither vue-i18n or vuex-i18n support this.

License

Under MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].