@erag/lang-sync-inertia

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

Laravel Inertia.js Vue 3 的翻译和本地化软件包 React 和 Svelte 的响应语言切换、翻译帮助器、嵌入键、占位符、复数化和 TypeScript 支持

中文使用文档
文档目录18 节
  1. 特征
  2. 所需资源
  3. 安装
  4. 使用量
  5. 第3组
  6. 反应
  7. 丝绒
  8. API 参考
  9. __(key, replaces?)
  10. trans(key, replaces)
  11. transChoice(key, count, replaces?)
  12. 拉拉维尔一体化
  13. 主计长
  14. 语言文件 - resources/lang/en/auth.php
  15. 类型脚本
  16. API 条目点
  17. 软件包结构
  18. 许可证

@elag/lang-sync-inertia (英语)

与 Inertia.js 同步 Laravel 翻译 Vue 3, React 和 Svelte .

轻量级前端伴 eramitgupta/laravel-lang-sync-inertia它读取通过Inertia共享的翻译 page.props.lang 并提供一致的,类型安全的帮助,用于查找、嵌入式密钥、占位符、复数化和本地化的更新.


特征

  • 🔄 Laravel 同步 与 syncLangFiles() 通过 Inertia 共享选中语言文件
  • 🧩 Vue 3, React 18/19, 和斯维尔特 5 一个软件包根的助手
  • 🌍 本地意识加载 从Laravel的活跃状态 lang/{locale} 目录
  • 📁 单个、多个和嵌入式文件 带有点标记, 如 admin.auth
  • ⚡ 共享翻译 通过Inertia的活性 page.props.lang
  • 🛠️ 翻译助手数字 : __(), (中文(简体) ), trans(), (中文(简体) ), transChoice(),以及 trans_choice()
  • 📝 拉拉维尔占位符 两者 :name 遗产 {name} 语法
  • 🔢 多元化 与精确值和 Laravel 间隔, 例如 {0}, (中文(简体) ), {1},以及 [2,*]
  • ↩️ 直接字符串密钥 当翻译缺失时返回到原始密钥
  • 📦 JSON 导出 从 PHP 语言文件进行前端已准备好的静态翻译
  • ✅ 类型脚本支持 带有打字器和轻量级运行时间

所需资源

此软件包需要 Laravel 后端软件包与前端共享翻译 :

composer require erag/laravel-lang-sync-inertia

安装

npm install @erag/lang-sync-inertia

使用量

第3组

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

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

组件示例 :

<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>

反应

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

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

组件示例 :

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>
  )
}

丝绒

要求数 @inertiajs/svelte v3(斯维尔特5).

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

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

组件示例 :

<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 参考

__(key, replaces?)

翻译密钥,替换可选的占位符.

__('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"

如果找不到匹配的翻译密钥, 则原密钥返回时不变 :

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

Laravel 关键示例 :

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"

包含字面点的密钥——如作为翻译密钥使用的英文句子——得到支持. 在进行点评前,先进行直接检查,以便正确解决:

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

trans(key, replaces)

别名 __() 带有明确的替换对象——在总是有占位符时更可取.

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

transChoice(key, count, replaces?)

使用 Laravel 风格的复数化字符串翻译一个复数化键. 这个 trans_choice() 还有别名.

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 语言文件示例 :

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

拉拉维尔一体化

主计长

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

网格语言目录可以从 Laravel 中以点标记引用 :

syncLangFiles('admin.users');

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

对于一个文件像 lang/en/admin/users.php,使用前端的完整嵌入式密钥路径:

__('admin.users.name')

语言文件 - resources/lang/en/auth.php

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

软件包读取自 page.props.lang 自动——前端不需要额外设置.


类型脚本

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

API 条目点

使用套件根中的特定框架帮助符 :

// 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()

遗产 lang() 从特定框架路径导入(/vue, (中文(简体) ), /react,或 /svelte)继续支持后向相容性.


软件包结构

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

行为守则 • 妇女 捐款 • 妇女 安全政策


许可证

麻省理工学院 阿米特·古普塔

版本与发布记录

@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 Vue 3 的翻译和本地化软件包 React 和 Svelte 的响应语言切换、翻译帮助器、嵌入键、占位符、复数化和 TypeScript 支持

如何安装 @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 和提交记录判断。