Image

laravel

Localization

How to use i18n into Blade and Vue

1 min read
Last update: November 28, 2021

Get Laravel translations into VueJS

Use tohidplus/laravel-vue-translation PHP package, just add it with Composer and publish it:

composer require tohidplus/laravel-vue-translation

php artisan vendor:publish --provider="Tohidplus\Translation\TranslationServiceProvider"

And into the config/app.php:

config/app.php
<?php

'providers' => [
    // ...
    Tohidplus\Translation\TranslationServiceProvider::class,
    // ...
];
resources/js/app.js
window.Vue = require('vue');

// If you want to add to window object
window.translate = require('./VueTranslation/Translation').default.translate;

// If you want to use it in your vue components
Vue.prototype.translate = require('./VueTranslation/Translation').default.translate;

Generation JSON archive with all translations, you will have to execute this after any update of translations (from resources/lang/):

php artisan VueTranslation:generate --watch=1

Into VueJS component

resources/js/components/my-component.vue
<template>
    <div>
        {{ translate('titles.home') }}
    </div>
</template>