Search by

tooinfinity / lingua

tooinfinity

Zero-config localization for Laravel 12 and 13 with Inertia.js 2 and 3

Package info

github.com/tooinfinity/lingua

pkg:composer/tooinfinity/lingua

Fund package maintenance!

tooinfinity

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.3.0 2026-12-01 00:27 UTC

This package is auto-updated.

Last update: 2026-12-01 01:11:26 UTC


README

Tests PHP Version Laravel Version Latest Stable Version License

⚠️ Under Development: This package is currently under active development and is not ready for production use.

Share Laravel translations with your Inertia.js + React frontend.

Supported Versions

Package Supported versions
Laravel 12.x, 13.x
Inertia.js (Laravel adapter) 2.x, 3.x
PHP 8.4+

Laravel 11 support was dropped in the 0.3.0 release. If you are still on Laravel 11, pin tooinfinity/lingua to ^0.2.0.

Quick Start

1. Install the Laravel package

composer require tooinfinity/lingua
php artisan lingua:install

This installs the Laravel package and publishes its configuration. It does not install a frontend framework. The service provider is auto-discovered, so no manual provider registration is required.

2. Install a frontend adapter (optional)

Lingua is frontend-agnostic. Choose an adapter that matches your frontend stack and install it explicitly:

# React (Inertia.js)
php artisan lingua:install --adapter=react

This installs @tooinfinity/lingua-react using your detected package manager (npm, yarn, pnpm, or bun). If your application does not use a frontend adapter, skip this step — the core package works on its own.

3. Configure locales

Edit config/lingua.php:

'locales' => ['en', 'fr', 'es'],

4. Middleware

The middleware is auto-registered to the web group by default. No action needed for most apps. To disable auto-registration and add manually, see the installation guide.

5. Create translations

lang/
├── en/
│   └── messages.php
└── fr/
    └── messages.php
// lang/en/messages.php
return [
    'welcome' => 'Welcome!',
    'greeting' => 'Hello, :name!',
];

// lang/fr/messages.php
return [
    'welcome' => 'Bienvenue!',
    'greeting' => 'Bonjour, :name!',
];

6. Use in React

See the React adapter documentation for the useTranslations() hook and framework-specific usage.

Documentation

Detailed guides live in docs/:

Guide Covers
Installation and configuration Prerequisites, install command, config defaults, middleware
Locale resolution and fallback Resolver precedence, normalization, fallback, persistence, routes
Translation sources and loading PHP/JSON formats, drivers, eager vs lazy loading, group APIs
Inertia integration and frontend contract Shared page-prop payload, locale switching, lazy loading
Operational guidance Troubleshooting, security constraints, upgrade notes

Architecture

The lingua core package owns locale resolution, translation loading, fallback behavior, and the shared Inertia payload contract. It is intentionally framework-neutral, so installing it never requires React or any other frontend framework.

Adapter Package Install command
React (Inertia.js) @tooinfinity/lingua-react php artisan lingua:install --adapter=react

Framework-specific behavior lives in separate adapters. The Inertia page-prop contract shared by LinguaMiddleware contains no React- or Vue-specific assumptions, so the same payload can be consumed by any future adapter without changing the PHP package.

Locale Switching

The package registers POST /locale (name lingua.locale.update) which validates the locale against the supported locales, persists it, and redirects back. The prefix and middleware are configurable via config('lingua.routes.prefix') and config('lingua.routes.middleware').

A React example (adapter-side):

import { router } from '@inertiajs/react';
import { useTranslations } from '@tooinfinity/lingua-react';

function LocaleSwitcher() {
    const { locale, locales } = useTranslations();

    const switchLocale = (newLocale: string) => {
        router.post('/locale', { locale: newLocale });
    };

    return (
        <div>
            {locales.map((loc) => (
                <button
                    key={loc}
                    onClick={() => switchLocale(loc)}
                    disabled={loc === locale}
                >
                    {loc.toUpperCase()}
                </button>
            ))}
        </div>
    );
}

Translation Groups (Optional)

Load only the translations needed for a specific request by passing group names to the middleware. When no groups are provided, Lingua shares all translations.

Route::middleware(['web', 'lingua:common,validation'])->get('/dashboard', function () {
    // ...
});

You can also load specific groups manually via Lingua::translationsFor(['common', 'validation']).

Facade

use TooInfinity\Lingua\Facades\Lingua;

Lingua::getLocale();           // Get current locale
Lingua::setLocale('fr');       // Set locale (optionally persists cookie)
Lingua::supportedLocales();    // Get supported locales
Lingua::translations();        // Get all translations
Lingua::translationsFor(['common', 'validation']); // Get specific groups
Lingua::translationGroup('auth'); // Get a single group
Lingua::availableGroups();     // List available groups
Lingua::isRtl();               // Is the current locale RTL?
Lingua::getDirection();        // 'ltr' or 'rtl'

Fallback Locale Behavior

When a translation key is missing in the current locale, Lingua automatically fills it from the default locale. This applies to both PHP translation groups and JSON translation files. No fallback merge occurs when the current locale already matches the default locale.

// config/lingua.php
'default' => 'en',
Lingua::setLocale('fr');

// 'logout' comes from the default locale
Lingua::translationGroup('auth');
// ['login' => 'Connexion', 'logout' => 'Logout']

Inertia Page-Prop Contract

The LinguaMiddleware shares a single, framework-neutral payload under the lingua key of every Inertia page:

{
  "locale": "en",
  "locales": ["en", "fr", "es"],
  "translations": { "messages": { "welcome": "Welcome!" } },
  "direction": "ltr",
  "isRtl": false
}

Frontend adapters consume this payload through Inertia's page props. See the Inertia integration guide for the full payload shape and the React adapter documentation for framework-specific consumption.

Translation File Formats

Lingua supports PHP and JSON translation files. Configure the driver in config/lingua.php:

'translation_driver' => 'php', // or 'json'

See the translation sources guide for file layouts, examples, and driver behavior.

Custom Controller

Override the default locale-switching controller in config/lingua.php:

'controller' => \App\Http\Controllers\LocaleController::class,

See the operational guide for an example custom controller.

Upgrading from a previous version

Earlier versions of Lingua installed @tooinfinity/lingua-react unconditionally as part of php artisan lingua:install. If your existing application relies on that behavior, run the install command with the React adapter explicitly:

php artisan lingua:install --adapter=react

This preserves the previous React installation workflow while keeping the core package free of frontend dependencies.

License

MIT License. See LICENSE.md.