@erag/lang-sync-inertia

中文文档
3.1.1发布于 2026/11/14TypeScriptES Module零依赖构建来源已验证近 30 天下载 3978

Laravel Inertia.js translation and localization package for Vue 3, React, and Svelte with reactive language switching, translation helpers, nested keys, placeholders, pluralization, and TypeScript support

原始使用文档
文档目录18 节
  1. Features
  2. Requirements
  3. Installation
  4. Usage
  5. Vue 3
  6. React
  7. Svelte
  8. API Reference
  9. __(key, replaces?)
  10. trans(key, replaces)
  11. transChoice(key, count, replaces?)
  12. Laravel Integration
  13. Controller
  14. Language file — resources/lang/en/auth.php
  15. TypeScript Types
  16. API Entry Points
  17. Package Structure
  18. License

@erag/lang-sync-inertia

Sync Laravel translations with Inertia.js for Vue 3, React, and Svelte.

A lightweight frontend companion to eramitgupta/laravel-lang-sync-inertia. It reads translations shared through Inertia's page.props.lang and provides consistent, type-safe helpers for lookups, nested keys, placeholders, pluralization, and locale-aware updates.


Features

  • 🔄 Laravel sync with syncLangFiles() to share selected language files through Inertia
  • 🧩 Vue 3, React 18/19, and Svelte 5 helpers from one package root
  • 🌍 Locale-aware loading from Laravel's active lang/{locale} directory
  • 📁 Single, multiple, and nested files with dot notation such as admin.auth
  • ⚡ Shared translations available through Inertia's reactive page.props.lang
  • 🛠️ Translation helpers: __(), trans(), transChoice(), and trans_choice()
  • 📝 Laravel placeholders with both :name and legacy {name} syntax
  • 🔢 Pluralization with exact values and Laravel intervals such as {0}, {1}, and [2,*]
  • ↩️ Direct string keys with fallback to the original key when a translation is missing
  • 📦 JSON export from PHP language files for frontend-ready static translations
  • ✅ TypeScript support with typed helpers and a lightweight runtime

Requirements

This package requires the Laravel backend package to share translations with the frontend:

composer require erag/laravel-lang-sync-inertia

Installation

npm install @erag/lang-sync-inertia

Usage

Vue 3

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

const { trans, __, transChoice } = vueLang()

Component example:

<script setup lang="ts">
import { vueLang } from '@erag/lang-sync-inertia'

const { trans, __, transChoice } = vueLang()
</script>

<template>
  <h1>{{ __('auth.greeting') }}</h1>
  <p>{{ trans('auth.welcome', { name: 'Amit' }) }}</p>
  <p>{{ transChoice('auth.apples', 3) }}</p>
</template>

React

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

const { trans, __, transChoice } = reactLang()

Component example:

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

export default function Login() {
  const { trans, __, transChoice } = reactLang()

  return (
    <div>
      <h1>{__('auth.greeting')}</h1>
      <p>{trans('auth.welcome', { name: 'Amit' })}</p>
      <p>{transChoice('auth.apples', 3)}</p>
    </div>
  )
}

Svelte

Requires @inertiajs/svelte v3 (Svelte 5).

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

const { trans, __, transChoice } = svelteLang()

Component example:

<script module lang="ts">
import { svelteLang } from '@erag/lang-sync-inertia'

const { trans, __, transChoice } = svelteLang()
</script>

<h1>{__('auth.greeting')}</h1>
<p>{trans('auth.welcome', { name: 'Amit' })}</p>
<p>{transChoice('auth.apples', 3)}</p>

API Reference

__(key, replaces?)

Translates a key, with optional placeholder replacement.

__('auth.login')
// → "Login"

__('auth.welcome', { name: 'Amit' })
// → "Welcome, Amit!"

__('messages.greeting.welcome_with_message', {
  name: 'Amit',
  message: 'Good to see you'
})
// lang/en/messages.php → ['greeting' => ['welcome_with_message' => 'Welcome, :name. :message']]
// → "Welcome, Amit. Good to see you"

If no matching translation key is found, the original key is returned unchanged:

__('I love programming.')
// → "I love programming."

Laravel key example:

return [
    'greeting' => [
        'name' => 'Welcome, :name',
        'welcome_with_message' => 'Welcome, :name. :message',
        'legacy_welcome' => 'Welcome, {name}',
    ],
];
__('messages.greeting.name', { name: 'dayle' })
// → "Welcome, dayle"

trans('messages.greeting.welcome_with_message', {
  name: 'dayle',
  message: 'Good to see you'
})
// → "Welcome, dayle. Good to see you"

Keys containing literal dots — such as English sentences used as translation keys — are supported. A direct lookup is attempted before dot-notation traversal, so they resolve correctly:

// lang/en/messages.php → ['Please proceed with caution, this cannot be undone.' => '...']
__('Please proceed with caution, this cannot be undone.')
// → "..."

trans(key, replaces)

Alias of __() with explicit replacement object — preferred when placeholders are always present.

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

transChoice(key, count, replaces?)

Translates a pluralized key using Laravel-style pluralization strings. The trans_choice() alias is also available.

transChoice('auth.apples', 1)
// → "There is one apple"

transChoice('auth.apples', 5)
// → "There are 5 apples"

trans_choice('auth.notifications', 0)
// → "No notifications"

trans_choice('auth.notifications', 3)
// → "3 notifications"

Laravel language file example:

return [
    'apples' => 'There is one apple|There are :count apples',
    'notifications' => '{0} No notifications|{1} One notification|[2,*] :count notifications',
];

Laravel Integration

Controller

syncLangFiles(['auth', 'dashboard']);
return Inertia::render('Dashboard');

Nested language directories can be referenced from Laravel with dot notation:

syncLangFiles('admin.users');

return Inertia::render('Admin/Users/Index');

For a file like lang/en/admin/users.php, use the full nested key path on the frontend:

__('admin.users.name')

Language file — resources/lang/en/auth.php

return [
    'greeting' => 'Hello!',
    'welcome'  => 'Welcome, :name',
    'apples'   => 'There is one apple|There are :count apples',
];

The package reads from page.props.lang automatically — no extra setup needed on the frontend.


TypeScript Types

type Replaces = Record<string, string | number>
type LangValue = string | { [key: string]: string | LangValue }
type LangObject = Record<string, LangValue>

API Entry Points

Use the framework-specific helper from the package root:

// Vue
import { vueLang } from '@erag/lang-sync-inertia'
const { trans, __ } = vueLang()

// React
import { reactLang } from '@erag/lang-sync-inertia'
const { trans, __ } = reactLang()

// Svelte
import { svelteLang } from '@erag/lang-sync-inertia'
const { trans, __ } = svelteLang()

The legacy lang() imports from the framework-specific paths (/vue, /react, or /svelte) remain supported for backward compatibility.


Package Structure

src/
├── vue/
├── react/
├── svelte/
├── types/
└── index.ts

Code of Conduct · Contributing · Security Policy


License

MIT © Amit Gupta

版本与发布记录

@erag/lang-sync-inertia 共发布 12 个版本,首次发布于 2025/12/05;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.1latest2026/11/14今天
3.1.02026/08/013 个月前
3.0.02026/06/234 个月前
2.0.12026/05/285 个月前
2.0.02026/05/205 个月前
1.6.12026/04/097 个月前

关于 @erag/lang-sync-inertia 的常见问题

@erag/lang-sync-inertia 是什么?

Laravel Inertia.js translation and localization package for Vue 3, React, and Svelte with reactive language switching, translation helpers, nested keys, placeholders, pluralization, and TypeScript support

如何安装 @erag/lang-sync-inertia?

在项目目录运行 npm install @erag/lang-sync-inertia,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(^3.0.0)、react(^18.0.0 || ^19.0.0)、svelte(^4.0.0 || ^5.0.0)、@inertiajs/core(^2.0.0 || ^3.0.0)、@inertiajs/vue3(^2.0.0 || ^3.0.0)、@inertiajs/react(^2.0.0 || ^3.0.0)、@inertiajs/svelte(^2.0.0 || ^3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@erag/lang-sync-inertia 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@erag/lang-sync-inertia 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@erag/lang-sync-inertia 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@erag/lang-sync-inertia 可以商用吗?

@erag/lang-sync-inertia 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@erag/lang-sync-inertia 的下载量有多少?

根据 npm 官方下载统计,@erag/lang-sync-inertia 在 2026/10/14 – 2026/11/12 这 30 天内被下载 3,978 次,日均约 133 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@erag/lang-sync-inertia 还在维护吗?

最新版本 3.1.1 发布于 2026/11/14(今天);至今共发布 12 个版本,其中过去 12 个月发布了 12 个;首次发布于 2025/12/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。