codecng/laravel-inertia-translations
最新稳定版本:1.2.1
Composer 安装命令:
composer require codecng/laravel-inertia-translations
包简介
Provides translations for an Inertia Based Project
README 文档
README
A zero-configuration Laravel package that automatically exports your Laravel translations for use with Inertia.js. Supports both React and Vue, with full TypeScript support!
Installation
composer require codecng/laravel-inertia-translations
That's it! No additional configuration needed.
Usage
Whenever you add or modify translations in your Laravel application, simply run:
php artisan translate
This command will:
- Check if language files exist (if not, it will publish them automatically)
- Process all your translation files (both JSON and PHP)
- Generate JSON translation files in
resources/js/lang/ - Create appropriate utility files based on your stack (React/Vue + TypeScript)
What Gets Processed
- ✅ JSON files in
lang/directory - ✅ PHP files in language subdirectories
- ✅ Automatically merges all translations by locale
Generated Files Structure
resources/js/
├── lang/
│ ├── en.json
│ ├── es.json
│ └── fr.json
└── lib/
└── translations.(js|ts|jsx|tsx) # Based on your stack
Framework Support
React
import { __ } from '@/lib/translations' function Welcome() { return ( <div> <h1>{__('welcome.title')}</h1> <p>{__('welcome.message')}</p> <p>{__('Users')}</p> </div> ) }
Vue
<script setup> import { __ } from '@/lib/translations' </script> <template> <div> <h1>{{ __('welcome.title') }}</h1> <p>{{ __('welcome.message') }}</p> </div> </template>
Inertia Setup
Make sure to include the current language in your Inertia shared props (in your HandleInertiaRequests middleware):
public function share(Request $request): array { return array_merge(parent::share($request), [ 'language' => request()->user()->language ?? app()->getLocale(), ]); }
Type Support
When using TypeScript, you get full type support for your translation keys:
// The __ function is fully typed __('welcome.title') // ✓ Valid __('invalid.key') // ✗ TypeScript error
Benefits
- 🚀 Zero configuration required
- 🔄 Simple one-command updates
- 🛠 Works with both JSON and PHP translation files
- 💪 Full TypeScript support
- ⚡️ Supports both React and Vue
- 🔍 Automatic type generation for translation keys
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 480
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-13