erag / laravel-lang-sync-inertia
A powerful Laravel package for syncing and managing language translations across backend and Inertia.js (Vue/React) frontends, offering effortless localization, auto-sync features, and smooth multi-language support for modern Laravel applications.
Package info
github.com/eramitgupta/laravel-lang-sync-inertia
pkg:composer/erag/laravel-lang-sync-inertia
Requires
- php: >=8.1.0
- illuminate/filesystem: ^8.12|^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
- inertiajs/inertia-laravel: ^1.3|^2.0|^3.0
- laravel/pint: ^1.13
This package is auto-updated.
Last update: 2026-04-01 09:40:34 UTC
README
Laravel Lang Sync Inertia is a Laravel package that bridges your backend translation files with your Inertia.js frontend (Vue 3 or React). Load any language file in a controller and access it instantly on the frontend โ no manual prop passing required.
โจ Features
- โ๏ธ Automatic translation sharing via Inertia.js middleware
- ๐ Load single or multiple language files with one call
- ๐ Dynamic placeholder replacement โ
{name}syntax - ๐งฉ Works with both Vue 3 and React
- ๐ Auto-switches locale folder based on Laravel's current locale
- ๐ Artisan command to export
.phplang files to frontend-ready.json - ๐ ๏ธ Clean helper API:
trans()and__()
๐ฆ Installation
Step 1 โ Install the package via Composer:
composer require erag/laravel-lang-sync-inertia
Step 2 โ Publish Laravel's default language files (if not already published):
php artisan lang:publish
๐ This creates the
lang/directory in your project root with default Laravel translation files.
Step 3 โ Publish the package config and composables:
php artisan erag:install-lang
โ ๏ธ Frontend Companion Package (Required)
To use translations in Vue or React, install the NPM package:
npm install @erag/lang-sync-inertia
๐ Full frontend docs: npmjs.com/package/@erag/lang-sync-inertia
๐ Usage โ syncLangFiles()
syncLangFiles() is a global Laravel helper provided by this package. Call it inside any controller method to load translation files and share them with your frontend via Inertia.
โ Think of
syncLangFiles()as a bridge โ it reads your.phplang files on the backend and passes them as Inertia shared props to your frontend components.
// Single file syncLangFiles('auth'); // Multiple files syncLangFiles(['auth', 'validation', 'pagination']);
๐ Step-by-Step Example
1. Define a Language File
๐ resources/lang/en/auth.php
return [ 'greeting' => 'Hello!', 'welcome' => 'Welcome, {name}!', ];
2. Load It in the Controller
use Inertia\Inertia; public function login() { syncLangFiles('auth'); return Inertia::render('Login'); }
This loads resources/lang/{locale}/auth.php based on App::getLocale() and shares it automatically with Inertia.
3. Use It on the Frontend
โ Vue 3
<script setup> import { lang } from '@erag/lang-sync-inertia/vue' const { trans, __ } = lang() </script> <template> <h1>{{ __('auth.greeting') }}</h1> <p>{{ trans('auth.welcome', { name: 'John' }) }}</p> </template>
โ React
import { lang } from '@erag/lang-sync-inertia/react' export default function Login() { const { trans, __ } = lang() return ( <div> <h1>{__('auth.greeting')}</h1> <p>{trans('auth.welcome', { name: 'John' })}</p> </div> ) }
๐ค Output
Hello!
Welcome, John!
๐งช How It Works
When you call syncLangFiles('auth') in your controller:
- Laravel reads
resources/lang/{locale}/auth.phpbased onApp::getLocale() - The translation array is passed to Inertia as a shared prop under
page.props.lang - The frontend helper (
lang()) reads frompage.props.langand resolves keys likeauth.greeting - Placeholders like
{name}are replaced at runtime with the values you pass
Controller โ syncLangFiles('auth')
โ
Laravel reads resources/lang/en/auth.php
โ
Inertia shares โ page.props.lang.auth
โ
Frontend โ __('auth.greeting') or trans('auth.welcome', { name: 'Amit' })
โ
Output โ "Hello!" / "Welcome, Amit!"
๐ง trans() vs __()
| Function | Best For | Description |
|---|---|---|
trans() |
Dynamic values | Use when passing placeholders like { name } |
__() |
Simple lookups | Shortcut for quick string access |
Both functions support placeholder replacement. trans() is recommended when replacements are always present.
// Simple __('auth.greeting') // โ "Hello!" // With replacement trans('auth.welcome', { name: 'Amit' }) // โ "Welcome, Amit!"
๐ Example with Plain String
Sometimes you may pass a plain string instead of a replacement object:
__('auth.welcome', 'Amit') // โ "Welcome, {name}!" (placeholder is NOT replaced โ string is appended as-is)
โ ๏ธ This does not replace
{name}. Always use an object when you need placeholder replacement.
The correct approach:
trans('auth.welcome', { name: 'Amit' }) // โ "Welcome, Amit!" __('auth.welcome', { name: 'Amit' }) // โ "Welcome, Amit!"
๐ก Access Raw Inertia Props
If you need the full translation object directly:
Vue:
import { usePage } from '@inertiajs/vue3' const { lang } = usePage().props
React:
import { usePage } from '@inertiajs/react' const { lang } = usePage().props
๐๏ธ Translation File Structure
Language files are loaded dynamically based on App::getLocale():
resources/lang/
โโโ en/
โ โโโ auth.php
โโโ hi/
โ โโโ auth.php
Calling syncLangFiles('auth') loads resources/lang/{locale}/auth.php automatically.
โ๏ธ Configuration
After publishing, customize config/inertia-lang.php:
return [ /* |-------------------------------------------------------------------------- | Language Files Base Path |-------------------------------------------------------------------------- | The directory where your .php language files are stored. */ 'lang_path' => base_path('lang'), /* |-------------------------------------------------------------------------- | Output Path (Exported JSON Files) |-------------------------------------------------------------------------- | Where generated .json files will be written by the Artisan command. */ 'output_lang' => resource_path('js/lang'), ];
๐ Artisan Command โ Export to JSON
Generate frontend-ready .json files from your .php language files โ useful when you want to use translations without Inertia shared props.
php artisan erag:generate-lang
Input: resources/lang/en/auth.php
return [ 'greeting' => 'Hello!', 'welcome' => 'Welcome, {name}!', ];
Output: resources/js/lang/en/auth.json
{
"greeting": "Hello!",
"welcome": "Welcome, {name}!"
}
Generated structure:
resources/js/lang/
โโโ en/
โ โโโ auth.json
โ โโโ validation.json
โ โโโ pagination.json
โโโ hi/
โ โโโ auth.json
โ โโโ validation.json
๐ License
Licensed under the MIT License.
๐ค Contributing
Pull requests and issues are welcome! Let's make Laravel localization better together. ๐ฌ