This plugin was created in order to provide working Vue3 translations for a project of mine. It uses mariuzzo/laravel-js-localization & some code from eli5-amsterdam/vue-lang-js. This package is by no means perfect, so any contribution is well appreciated.
composer require mariuzzo/laravel-js-localization
Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider::class
php artisan lang:js resources/js/vue-translations.js --no-lib --quiet
$ npm install -S vue3-langjs
Start by importing the component.
import Vue from 'vue'
import Vue3Langjs from 'vue3-lang-js'
// get the data source
import translations from './vue-translations.js';
Vue.use(Vue3Langjs, {
messages: translations, // Provide locale file
locale: 'de', // Set locale
fallback: 'en' // Set fallback lacale
})
Get language string
this.$trans('auth.title')
this.$trans('auth.title', { name: 'eli5' })
{{ $trans('auth.title') }}
{{ $trans('auth.title', { name: 'eli5' }) }}
// Alias
this.$t('auth.title')
this.$t('auth.title', { name: 'eli5' })
{{ $t('auth.title') }}
{{ $t('auth.title', { name: 'eli5' }) }}
Set locale
this.$lang.setLocale('en')