Skip to content

Commit 347d751

Browse files
committed
update: README.md
1 parent 207db22 commit 347d751

File tree

1 file changed

+69
-74
lines changed

1 file changed

+69
-74
lines changed

README.md

Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -50,90 +50,68 @@ php artisan erag:install-lang
5050

5151
This will publish:
5252

53-
*`config/lang-manager.php` — for customizing the language path
53+
*`config/inertia-lang.php` — for customizing the language path
5454
*`resources/js/composables/useLang.ts` — for Vue (if selected)
5555
*`resources/js/hooks/useLang.tsx` — for React (if selected)
5656

5757
During installation, you'll be prompted to choose either **Vue** or **React** for your frontend framework.
5858

5959
---
6060

61-
## 🚀 Usage
61+
## 🚀 Usage Guide: `syncLangFiles()`
6262

63-
### 🔟 Where to Use `syncLangFiles()`?
63+
The `syncLangFiles()` function is a Laravel helper provided by this package. Use it inside your **controller methods** to load translation files and automatically **share them with your Vue or React frontend via Inertia.js**.
6464

65-
Call `syncLangFiles()` **inside your controller method** **before rendering an Inertia view** to load necessary language files and share them with the frontend.
65+
> ✅ Think of `syncLangFiles()` as a bridge between Laravel’s backend translations and your Inertia-powered frontend.
6666
6767
---
6868

69-
### 1️⃣ Load a Single File
69+
### 🧪 How It Works
7070

71-
📍 **Example in Controller:**
71+
Suppose you have the following language file:
7272

73-
```php
74-
use Inertia\Inertia;
75-
76-
public function index()
77-
{
78-
syncLangFiles('auth'); // Load a single language file
79-
80-
return Inertia::render('Dashboard');
81-
}
82-
```
83-
84-
✅ This loads `resources/lang/{locale}/auth.php` and makes translations available to Vue or React components.
85-
86-
---
87-
88-
### 2️⃣ Load Multiple Files
89-
90-
📍 **Example in Controller:**
73+
📁 **`resources/lang/en/auth.php`**
9174

9275
```php
93-
use Inertia\Inertia;
94-
95-
public function profile()
96-
{
97-
syncLangFiles(['auth', 'profile']); // Load multiple files
98-
99-
return Inertia::render('Profile');
100-
}
76+
return [
77+
'welcome' => 'Welcome, {name}!',
78+
'greeting' => 'Hello!',
79+
];
10180
```
10281

103-
✅ This loads both `auth.php` and `profile.php` based on the active locale.
82+
Now, you want to show `auth.welcome` and `auth.greeting` on the frontend using Vue or React.
10483

10584
---
10685

107-
### 3️⃣ Load Based on Condition
86+
### 🔁 Step-by-Step Example
87+
88+
#### 🔹 1. Load Translations in Controller
10889

10990
```php
11091
use Inertia\Inertia;
11192

112-
public function show($type)
93+
public function dashboard()
11394
{
114-
if ($type === 'admin') {
115-
syncLangFiles(['admin', 'auth']);
116-
} else {
117-
syncLangFiles(['user', 'auth']);
118-
}
95+
// Load the auth.php language file
96+
syncLangFiles('auth');
11997

120-
return Inertia::render('UserTypePage');
98+
return Inertia::render('Dashboard');
12199
}
122100
```
123101

124-
This approach allows dynamic loading of translation files based on conditions.
102+
🧠 This loads the file `resources/lang/en/auth.php` based on the current Laravel locale and shares its content with Inertia.
125103

126104
---
127105

128-
## 🖥️ Frontend Usage
106+
### 💡 Frontend Usage
129107

130-
### ✅ Vue Example
108+
#### ✅ Vue Example
131109

132110
```vue
133111
<template>
134112
<div>
135-
<h1>{{ __('auth.name') }}</h1>
136-
<p>{{ trans('auth.greeting', { name: 'Amit' }) }}</p>
113+
<h1>{{ __('auth.greeting') }}</h1>
114+
<p>{{ trans('auth.welcome', { name: 'John' }) }}</p>
137115
</div>
138116
</template>
139117
@@ -144,9 +122,7 @@ const { trans, __ } = useLang()
144122
</script>
145123
```
146124

147-
---
148-
149-
### ✅ React Example
125+
#### ✅ React Example
150126

151127
```tsx
152128
import React from 'react'
@@ -157,15 +133,56 @@ export default function Dashboard() {
157133

158134
return (
159135
<div>
160-
<h1>{__('auth.name')}</h1>
161-
<p>{trans('auth.greeting', { name: 'Amit' })}</p>
136+
<h1>{__('auth.greeting')}</h1>
137+
<p>{trans('auth.welcome', { name: 'John' })}</p>
162138
</div>
163139
)
164140
}
165141
```
166142

167143
---
168144

145+
### 📤 Output on Page
146+
147+
When the above code is rendered, this will be the output:
148+
149+
```
150+
Hello!
151+
Welcome, John!
152+
```
153+
154+
---
155+
156+
### 🧠 Notes on `trans()` vs `__()`
157+
158+
| Function | Use Case | Description |
159+
| --------- | -------- | ------------------------------------------------------------ |
160+
| `trans()` | Advanced | Use when you need to pass dynamic placeholders like `{name}` |
161+
| `__()` | Simple | Shortcut for quick access to translated strings |
162+
163+
✅ You can use them interchangeably for basic translations.
164+
✅ Both support placeholder replacement.
165+
166+
---
167+
168+
### 🛠 Example with Plain String
169+
170+
Sometimes, you might want to append something without a key:
171+
172+
```js
173+
__('auth.welcome', 'Vue Developer')
174+
// Output: "Welcome, {name}! Vue Developer" (if placeholder is not used)
175+
```
176+
177+
But recommended usage is always with an object:
178+
179+
```js
180+
trans('auth.welcome', { name: 'Amit' })
181+
// Output: "Welcome, Amit!"
182+
```
183+
184+
---
185+
169186
## 📡 Access Inertia Shared Props
170187

171188
**Vue:**
@@ -188,26 +205,6 @@ You can directly access the full language object shared by Inertia.
188205

189206
---
190207

191-
## 🧠 Placeholder Support in Translations
192-
193-
### 🔤 Example: `lang/en/auth.php`
194-
195-
```php
196-
return [
197-
'welcome' => 'Welcome, {name}!',
198-
];
199-
```
200-
201-
### Usage in Vue/React:
202-
203-
```js
204-
trans('auth.welcome', { name: 'John' })
205-
__('auth.welcome', { name: 'John' })
206-
__('auth.welcome', 'Vue') // For plain string
207-
```
208-
209-
---
210-
211208
## 🗂️ Translation File Location
212209

213210
Language files are loaded based on the current Laravel locale. By default, Laravel uses `resources/lang/{locale}` structure:
@@ -232,7 +229,7 @@ It dynamically loads `resources/lang/{locale}/auth.php`.
232229

233230
## ⚙️ Configuration
234231

235-
You can customize the language directory by modifying `config/lang-manager.php`:
232+
You can customize the language directory by modifying `config/inertia-lang.php`:
236233

237234
```php
238235
return [
@@ -261,5 +258,3 @@ This package is licensed under the [MIT License](https://opensource.org/licenses
261258

262259
Pull requests and issues are welcome!
263260
Let’s work together to improve localization in Laravel! 💬
264-
265-
---

0 commit comments

Comments
 (0)