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.

Maintainers

Package info

github.com/eramitgupta/laravel-lang-sync-inertia

pkg:composer/erag/laravel-lang-sync-inertia

Statistics

Installs: 13 337

Dependents: 0

Suggesters: 0

Stars: 40

Open Issues: 0

v2.2.0 2026-03-30 06:34 UTC

README

Screenshot

Packagist License Latest Stable Version Total Downloads

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 .php lang 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 .php lang 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:

  1. Laravel reads resources/lang/{locale}/auth.php based on App::getLocale()
  2. The translation array is passed to Inertia as a shared prop under page.props.lang
  3. The frontend helper (lang()) reads from page.props.lang and resolves keys like auth.greeting
  4. 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. ๐Ÿ’ฌ