sematico / laravel-inertia-i18n
Bridge Laravel JSON translations with React apps built on Inertia.js
Package info
github.com/alessandrotesoro/laravel-inertia-i18n
Language:TypeScript
pkg:composer/sematico/laravel-inertia-i18n
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/contracts: ^11.0||^12.0||^13.0
- inertiajs/inertia-laravel: ^3.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^3.8.4||^4.0
- pestphp/pest-plugin-arch: ^3.1.1||^4.0
- pestphp/pest-plugin-laravel: ^3.2||^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
This package is auto-updated.
Last update: 2026-08-24 04:50:00 UTC
README
Share Laravel JSON translations with an Inertia.js React application. The Composer package loads locale files and exposes them as Inertia props; the React package provides translation hooks and component interpolation.
| Package | Install from |
|---|---|
sematico/laravel-inertia-i18n |
Packagist |
@sematico/laravel-inertia-i18n-react |
npm |
Note
The React package reads an i18n prop shared by the Laravel service provider. Keep <I18nProvider> inside your Inertia app tree.
Requirements
- PHP 8.2 or newer
- Laravel 11, 12, or 13
inertiajs/inertia-laravel3.x- React 19 and
@inertiajs/react3.x
Installation
Install the backend package:
composer require sematico/laravel-inertia-i18n
The service provider registers itself through Laravel package discovery. Publish the configuration file only when you need to change its defaults:
php artisan vendor:publish --tag=inertia-i18n-config
Install the React package:
npm install @sematico/laravel-inertia-i18n-react
Backend setup
Create JSON files in Laravel's lang directory. The file name is the locale:
{
"Welcome!": "Welcome!",
"Hello, :name!": "Hello, :name!",
"One item|:count items": "One item|:count items"
}
By default, the package shares this payload as page.props.i18n:
[
'locale' => 'en',
'fallbackLocale' => 'en',
'translations' => [/* ... */],
]
For locale switching, add the middleware to the routes that should accept the _locale query parameter:
use Sematico\InertiaI18n\Http\Middleware\HandleLocale; Route::middleware([HandleLocale::class])->group(function () { // localized routes });
HandleLocale validates locale names and stores an accepted locale in the session. Set supported_locales in the config file to restrict the accepted list.
React setup
Mount the provider once around the part of the tree that uses translations:
import type { ReactNode } from "react"; import { I18nProvider } from "@sematico/laravel-inertia-i18n-react"; export function AppShell({ children }: { children: ReactNode }) { return <I18nProvider>{children}</I18nProvider>; }
Use t, read the active locale, or request a locale reload:
import { useTranslation } from "@sematico/laravel-inertia-i18n-react"; export function Greeting() { const { t, locale, setLocale } = useTranslation(); return ( <div> <h1>{t("Welcome!")}</h1> <p>{t("Hello, :name!", { name: "Alex" })}</p> <p>Current locale: {locale}</p> <button type="button" onClick={() => setLocale("es")}> Español </button> </div> ); }
t() supports Laravel-style plural forms and interpolation:
t("One item|:count items", { count: 3 }); t("{0} None|{1} One|[2,*] :count items", { count: 3 });
Use Trans when translated text contains named component tags:
import { Trans } from "@sematico/laravel-inertia-i18n-react"; <Trans i18nKey="Read the <link>docs</link>." components={{ link: <a href="/docs" /> }} />;
Configuration
The published config/inertia-i18n.php file supports:
| Option | Default | Purpose |
|---|---|---|
auto_share |
true |
Share translations through the service provider |
prop_name |
i18n |
Name of the Inertia prop |
lang_path |
null |
Custom directory containing {locale}.json files |
supported_locales |
null |
Optional allow-list for HandleLocale |
Set auto_share to false when you want to call translationProps() from the SharesTranslations trait in your own Inertia middleware. If prop_name is changed, pass the same value to <I18nProvider propName="translations">.
Public API
I18nProviderreads the configured translation payload from Inertia page props.useTranslation()returnst,locale, andsetLocale.Transsupportsi18nKey,values,count,components, andas.InertiaI18n::translations()loads the current locale and merges missing keys from the Laravel fallback locale.InertiaI18n::toInertiaPayload()returns the locale, fallback locale, and translations.SharesTranslationsexposes the same payload for manual sharing.
Development
composer install
composer validate --strict
composer test
composer analyse
composer format -- --test
pnpm install --frozen-lockfile
pnpm typecheck
pnpm test:js
pnpm lint
pnpm build
npm pack --dry-run
The package build writes distributable files to packages/react/dist. The published npm package contains that build output plus the project README and license file.