Skip to content

React

Use the React entry point to access Laravel translations inside Inertia React pages and components.

The package reads translations from page.props.lang, then gives you two helpers:

  • __() for simple lookups
  • trans() for placeholder replacement

Import the helper

tsx
import { lang } from '@erag/lang-sync-inertia/react'

const { trans, __ } = lang()

Full example

php
<?php

namespace App\Http\Controllers;

use Inertia\Inertia;
use Inertia\Response;

class DashboardController extends Controller
{
    public function index(): Response
    {
        syncLangFiles('auth');

        return Inertia::render('Dashboard');
    }
}
php
return [
    'greeting' => 'Hello!',
    'welcome' => 'Welcome, {name}!',
];
tsx
import { lang } from '@erag/lang-sync-inertia/react'

export default function Dashboard() {
  const { trans, __ } = lang()

  return (
    <section>
      <h1>{__('auth.greeting')}</h1>
      <p>{trans('auth.welcome', { name: 'John' })}</p>
    </section>
  )
}
text
Hello!
Welcome, John!

Component example

tsx
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: 'Amit' })}</p>
    </div>
  )
}

How it works

  1. Laravel loads lang/{locale}/auth.php with syncLangFiles('auth').
  2. Inertia shares that data under page.props.lang.
  3. lang() reads from those props inside your React component.
  4. Keys like auth.greeting and auth.welcome resolve automatically.

trans() vs __()

FunctionBest forDescription
trans()Dynamic valuesUse when passing placeholders like { name }
__()Simple lookupsShortcut for quick string access
tsx
__('auth.greeting')
// Hello!

trans('auth.welcome', { name: 'Amit' })
// Welcome, Amit!

Both helpers support placeholder replacement, but trans() is the clearer choice when replacements are always present.

Placeholder replacements

Always pass an object when you want {name} style placeholders replaced:

tsx
trans('auth.welcome', { name: 'Amit' })
__('auth.welcome', { name: 'Amit' })

If you pass a plain string instead:

tsx
__('auth.welcome', 'Amit')

The placeholder is not replaced.

Access raw Inertia props

If you need the full translation object directly in React:

tsx
import { usePage } from '@inertiajs/react'

const { lang } = usePage().props

Legacy API

The older React helper still works:

tsx
import { reactLang } from '@erag/lang-sync-inertia'

const { trans, __ } = reactLang()

Use @erag/lang-sync-inertia/react for new code.

MIT License. Copyright Er Amit Gupta