Laravel Lang Sync Inertia helps you add different languages to your Laravel app with Vue or React. It makes translations easy!
- βοΈ Inertia.js integration with automatic sharing
- π Load single or multiple language files
- π Dynamic replacement support in translations
- π§© Supports both Vue.js and React
- π§΅ Built-in middleware for automatic sharing
- π οΈ Helper functions like
trans()and__()for frontend usage - π Automatically switches language folder based on current Laravel locale
To install the package, run the following command via Composer:
composer require erag/laravel-lang-sync-inertiaTo publish the configuration and composables, run:
php artisan erag:install-langTo use translations on your frontend, π¦ you must install the NPM companion package:
npm install @erag/lang-sync-inertiaπ Full frontend documentation: β‘ https://www.npmjs.com/package/@erag/lang-sync-inertia
The syncLangFiles() function is a Laravel helper provided by this package. Use it inside your controller methods to load translation files and automatically share them with your Vue or React frontend via Inertia.js.
β Think of
syncLangFiles()as a bridge between Laravelβs backend translations and your Inertia-powered frontend.
// Load and sync a single translation file
syncLangFiles('auth');
// Load and sync multiple translation files
syncLangFiles(['auth', 'validation', 'pagination']);The syncLangFiles() function supports:
-
A string: For a single translation file β
syncLangFiles('auth') -
An array of strings: For multiple translation files β
syncLangFiles(['auth', 'validation'])
Suppose you have the following language file:
π resources/lang/en/auth.php
return [
'welcome' => 'Welcome, {name}!',
'greeting' => 'Hello!',
];Now, you want to show auth.welcome and auth.greeting on the frontend using Vue or React.
use Inertia\Inertia;
public function login()
{
// Load the auth.php language file
syncLangFiles('auth');
return Inertia::render('Login');
}π§ This loads the file resources/lang/en/auth.php based on the current Laravel locale and shares its content with Inertia.
<template>
<div>
<h1>{{ __('auth.greeting') }}</h1>
<p>{{ trans('auth.welcome', { name: 'John' }) }}</p>
</div>
</template>
<script setup>
import { vueLang } from '@erag/lang-sync-inertia'
const { trans, __ } = vueLang()
</script>import React from 'react'
import { reactLang } from '@erag/lang-sync-inertia'
export default function Login() {
const { trans, __ } = reactLang()
return (
<div>
<h1>{__('auth.greeting')}</h1>
<p>{trans('auth.welcome', { name: 'John' })}</p>
</div>
)
}When the above code is rendered, this will be the output:
Hello!
Welcome, John!
| Function | Use Case | Description |
|---|---|---|
trans() |
Advanced | Use when you need to pass dynamic placeholders like {name} |
__() |
Simple | Shortcut for quick access to translated strings |
β You can use them interchangeably for basic translations. β Both support placeholder replacement.
Sometimes, you might want to append something without a key:
__('auth.welcome', 'Vue Developer')
// Output: "Welcome, {name}! Vue Developer" (if placeholder is not used)But recommended usage is always with an object:
trans('auth.welcome', { name: 'Amit' })
// Output: "Welcome, Amit!"Vue:
import { usePage } from '@inertiajs/vue3'
const { lang } = usePage().propsReact:
import { usePage } from '@inertiajs/react'
const { lang } = usePage().propsYou can directly access the full language object shared by Inertia.
Language files are loaded based on the current Laravel locale. By default, Laravel uses resources/lang/{locale} structure:
resources/lang/
βββ en/
β βββ auth.php
βββ hi/
β βββ auth.php
When calling:
syncLangFiles('auth');It dynamically loads resources/lang/{locale}/auth.php.
You can customize the language directory by modifying config/inertia-lang.php:
return [
'lang_path' => resource_path('lang'), // Default: /resources/lang
];vueLang()β Vue 3reactLang()β React
This package is licensed under the MIT License.
Pull requests and issues are welcome! Letβs work together to improve localization in Laravel! π¬