Skip to content

Commit 31f3ac5

Browse files
committed
feat: add hooks React
1 parent 02ab36d commit 31f3ac5

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

resources/js/hooks/useLang.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { usePage } from '@inertiajs/react'
2+
3+
type Replaces = Record<string, string | number>
4+
type LangValue = string | { [key: string]: string | LangValue }
5+
type LangObject = Record<string, LangValue>
6+
7+
export function useLang() {
8+
const { lang } = usePage<{ lang: LangObject }>().props
9+
10+
function trans(key: string, replaces: Replaces | string = {}): string {
11+
const raw = getValueFromKey(key)
12+
if (typeof raw !== 'string') return key
13+
14+
let translated = raw
15+
16+
if (typeof replaces === 'string') {
17+
translated += ' ' + replaces
18+
} else if (typeof replaces === 'object') {
19+
translated = replacePlaceholders(translated, replaces)
20+
}
21+
22+
return translated
23+
}
24+
25+
function __(key: string, replaces: Replaces | string = {}) {
26+
return trans(key, replaces)
27+
}
28+
29+
function replacePlaceholders(text: string, replaces: Replaces): string {
30+
return Object.entries(replaces).reduce(
31+
(acc, [key, val]) => acc.replaceAll(`{${key}}`, String(val)),
32+
text
33+
)
34+
}
35+
36+
function getValueFromKey(key: string): string | undefined {
37+
const segments = key.split('.')
38+
let current: any = lang
39+
40+
for (const segment of segments) {
41+
if (typeof current !== 'object' || current === null) return undefined
42+
current = current[segment]
43+
}
44+
45+
return typeof current === 'string' ? current : undefined
46+
}
47+
48+
return { trans, __ }
49+
}

src/Commands/InstallLang.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,33 @@ public function handle()
1717
'--tag' => 'erag:publish-lang-config',
1818
'--force' => true,
1919
]);
20-
$this->info('configuration published successfully.');
20+
$this->info('Configuration published successfully.');
2121
$this->newLine();
2222

23-
$this->info('📦 Publishing JS composable...');
24-
$this->call('vendor:publish', [
25-
'--tag' => 'erag:publish-lang-composable',
26-
'--force' => true,
27-
]);
28-
$this->info('✅ JS composable published successfully.');
23+
// Ask for frontend framework
24+
$choice = $this->choice(
25+
'🎯 Which frontend framework are you using?',
26+
['Vue.js', 'React.js'],
27+
0
28+
);
29+
30+
if ($choice === 'Vue.js') {
31+
$this->info('📦 Publishing Vue composable...');
32+
$this->call('vendor:publish', [
33+
'--tag' => 'erag:publish-lang-composable-vue',
34+
'--force' => true,
35+
]);
36+
$this->info('✅ Vue composable published successfully.');
37+
}
38+
39+
if ($choice === 'React.js') {
40+
$this->info('📦 Publishing React composable...');
41+
$this->call('vendor:publish', [
42+
'--tag' => 'erag:publish-lang-composable-react',
43+
'--force' => true,
44+
]);
45+
$this->info('✅ React composable published successfully.');
46+
}
2947

3048
$this->newLine();
3149
$this->info('🎉 LaravelLangSyncInertia installation completed!');

src/LaravelLangSyncInertiaServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ protected function publishConfig(): void
5050

5151
$this->publishes([
5252
__DIR__.'/../resources/js/composables/useLang.ts' => resource_path('js/composables/useLang.ts'),
53-
], 'erag:publish-lang-composable');
53+
], 'erag:publish-lang-composable-vue');
54+
55+
56+
$this->publishes([
57+
__DIR__ . '/../resources/js/hooks/useLang.tsx' => resource_path('js/hooks/useLang.tsx'),
58+
], 'erag:publish-lang-composable-react');
5459

5560
}
5661

0 commit comments

Comments
 (0)