Laravel
Use syncLangFiles() inside any controller action that returns an Inertia response.
syncLangFiles() is a global Laravel helper from this package. It reads your backend language files and shares them with Inertia automatically, so you do not have to pass translation props manually in every response.
Think of syncLangFiles() as a bridge between your Laravel language files and your Inertia responses.
Single file
syncLangFiles('auth');Multiple files
syncLangFiles(['auth', 'validation', 'pagination']);Dashboard example
syncLangFiles('messages');
return Inertia::render('Dashboard');Full example
<?php
// app/Http/Controllers/DashboardController.php
namespace App\Http\Controllers;
use Inertia\Inertia;
use Inertia\Response;
class DashboardController extends Controller
{
public function index(): Response
{
syncLangFiles('messages');
return Inertia::render('Dashboard');
}
}<?php
return [
'greeting' => [
'name' => 'Welcome, :name',
'welcome_with_message' => 'Welcome, :name. :message',
'legacy_welcome' => 'Welcome, {name}',
],
'fruit' => [
'apples' => '{0} No apples available|{1} One apple available|[2,*] :count apples available',
],
'time' => [
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',
],
];<script setup>
import { lang } from '@erag/lang-sync-inertia/vue';
const { __, trans, transChoice, trans_choice } = lang();
</script>
<template>
<section>
<p>{{ __('messages.greeting.name', { name: 'dayle' }) }}</p>
<p>
{{
trans('messages.greeting.welcome_with_message', {
name: 'dayle',
message: 'Good to see you',
})
}}
</p>
<p>
{{ trans('messages.greeting.legacy_welcome', { name: 'Taylor' }) }}
</p>
<p>{{ transChoice('messages.fruit.apples', 18) }}</p>
<p>{{ trans_choice('messages.time.minutes_ago', 5) }}</p>
</section>
</template>import { lang } from '@erag/lang-sync-inertia/react';
export default function Dashboard() {
const { __, trans, transChoice, trans_choice } = lang();
return (
<section>
<p>{__('messages.greeting.name', { name: 'dayle' })}</p>
<p>
{trans('messages.greeting.welcome_with_message', {
name: 'dayle',
message: 'Good to see you',
})}
</p>
<p>
{trans('messages.greeting.legacy_welcome', { name: 'Taylor' })}
</p>
<p>{transChoice('messages.fruit.apples', 18)}</p>
<p>{trans_choice('messages.time.minutes_ago', 5)}</p>
</section>
);
}Controller -> syncLangFiles('messages')
Laravel -> lang/{locale}/messages.php
Inertia -> page.props.lang.messages
Frontend -> __('messages.greeting.name', { name: 'dayle' })
Output -> Welcome, dayleThis loads lang/{locale}/messages.php based on Laravel's current locale and shares it with Inertia automatically.
Laravel language keys
Use Laravel's placeholder style for new translation lines:
'name' => 'Welcome, :name',
'welcome_with_message' => 'Welcome, :name. :message',The frontend replacement object should use the placeholder names without the : prefix:
__('messages.greeting.name', { name: 'dayle' });
trans('messages.greeting.welcome_with_message', {
name: 'dayle',
message: 'Good to see you',
});Output:
Welcome, dayle
Welcome, dayle. Good to see youLegacy {name} placeholders are still supported for older language files:
'legacy_welcome' => 'Welcome, {name}',trans('messages.greeting.legacy_welcome', { name: 'Taylor' });Output:
Welcome, TaylorLaravel pluralization
Use Laravel-style pluralization strings in your language files:
'apples' => '{0} No apples available|{1} One apple available|[2,*] :count apples available',
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',Then call the pluralization helpers from Vue or React:
transChoice('messages.fruit.apples', 18);
trans_choice('messages.time.minutes_ago', 5);Output:
18 apples available
5 minutes agoTranslation string keys
Laravel also supports using the translation string itself as the key. If no synced key is found, the frontend helper returns the original string unchanged:
__('I love programming.');Output:
I love programming.How it works
When you call syncLangFiles('messages'):
- Laravel reads
lang/{locale}/messages.phpbased onApp::getLocale(). - The package shares that data through Inertia props.
- Your frontend helper reads from
page.props.lang. - Keys like
messages.greeting.nameresolve automatically.
Locale-based loading
The package uses Laravel's current locale, so this structure works out of the box:
lang/
├── en/
│ └── messages.php
└── hi/
└── messages.phpIf App::getLocale() returns hi, the package reads lang/hi/messages.php.