Skip to content

Commit 514fb9b

Browse files
committed
feat: implement useLocalizer composable for translation management
- Added useLocalizer composable to handle translations in Vue 3 applications. - Replaced useTranslation with useLocalizer in tests and components. - Updated Vite plugin to support new translation generation logic. - Enhanced translation handling with fallback and placeholder support. - Improved test coverage for translation functionalities. - Updated ESLint configuration to accommodate new plugin structure. - Refactored project structure to separate src and composables. - Bumped version to 0.0.2 and updated package description.
1 parent 2e0541a commit 514fb9b

17 files changed

+927
-240
lines changed

.npmignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
node_modules
22
dist
3+
src
4+
composables
5+
index.ts
6+
vite-plugin.ts
7+
__tests__
8+
*.test.ts
9+
*.test.tsx
10+
vitest.config.ts
11+
tsconfig.json
12+
tsup.config.ts
13+
eslint.config.js
14+
eslint.config.min.js
15+
.prettierrc.json
16+
.prettierignore
17+
.github
18+
.gitignore
19+
package-lock.json

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,47 @@
22

33
All notable changes to `@devwizard/laravel-localizer-vue` will be documented in this file.
44

5+
## v0.0.7 - 2025-11-09
6+
7+
### 🔄 Major Update - Alignment with React Version
8+
9+
- **Breaking Change**: Renamed `useTranslation` to `useLocalizer` for consistency with React version
10+
- No backward compatibility - all imports must be updated
11+
- Function signature and API remain the same
12+
13+
- **File Structure**: Reorganized to match React package structure
14+
- Created `src/` directory for all source files
15+
- Moved `composables/useTranslation.ts` to `src/useLocalizer.ts`
16+
- Renamed `vite-plugin.ts` to `src/vite-plugin-laravel-localizer.ts`
17+
18+
- **Translation Loading**: Updated to use `window.localizer` object (consistent with React version)
19+
- Changed from `window.__LARAVEL_LOCALIZER_TRANSLATIONS__` to `window.localizer.translations`
20+
- Better integration with bootstrap.ts initialization
21+
- Synchronous translation loading for improved performance
22+
23+
- **Vite Plugin Improvements**: Updated to match React implementation
24+
- Simplified plugin structure using `minimatch` for pattern matching
25+
- Better error handling and logging
26+
- Non-blocking command execution
27+
- Changed from `watch` option to `patterns` option for consistency
28+
- Removed debounce functionality for faster regeneration
29+
- Uses `handleHotUpdate` hook instead of `configureServer`
30+
31+
- **Dependencies**: Added `minimatch` for glob pattern matching
32+
33+
- **Documentation**: Updated README to match React version structure
34+
- Added bootstrap.ts initialization section
35+
- Updated Vite plugin options documentation
36+
- Improved setup instructions
37+
- All examples now use `useLocalizer`
38+
39+
### 🐛 Bug Fixes
40+
41+
- Fixed translation initialization warnings
42+
- Improved error messages for missing translations
43+
44+
---
45+
546
## v0.0.1 - 2025-11-09
647

748
### 🎉 Initial Beta Release

README.md

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# @devwizard/laravel-localizer-vue
22

3-
🌍 Vue 3 integration for Laravel Localizer with Vite plugin, `useTranslation` composable, and automatic TypeScript generation.
3+
🌍 Vue 3 integration for Laravel Localizer with Vite plugin, `useLocalizer` composable, and automatic TypeScript generation.
44

55
## Features
66

77
-**Automatic Generation**: Vite plugin watches for language file changes and regenerates TypeScript files
88
-**Type-Safe**: Full TypeScript support with auto-generated types
9-
-**Vue 3 Composition API**: Intuitive `useTranslation` composable for Vue components
9+
-**Vue 3 Composition API**: Intuitive `useLocalizer` composable for Vue components
10+
-**Customizable Path**: By default reads from `@/lang` folder, customizable via options
1011
-**Laravel-Compatible**: Matches Laravel's translation API (`__`, `trans`, `choice`)
1112
-**Inertia.js Integration**: Seamlessly works with Inertia.js page props
1213
-**RTL Support**: Built-in right-to-left language support
@@ -26,12 +27,42 @@ yarn add @devwizard/laravel-localizer-vue
2627

2728
```bash
2829
composer require devwizardhq/laravel-localizer
29-
php artisan localizer:install --framework=vue
30+
php artisan localizer:install
3031
```
3132

33+
The install command will:
34+
- ✅ Publish configuration files
35+
- ✅ Create default locale files
36+
- ✅ Install npm package (optional)
37+
- ✅ Generate initial TypeScript files
38+
39+
**Note:** You'll need to manually add the bootstrap setup (see step 1 below).
40+
3241
## Setup
3342

34-
### 1. Add Vite Plugin
43+
### 1. Initialize Translations in Bootstrap
44+
45+
Add this to your `resources/js/bootstrap.ts`:
46+
47+
```typescript
48+
import { translations } from '@/lang';
49+
50+
declare global {
51+
interface Window {
52+
localizer?: {
53+
translations: Record<string, Record<string, string>>;
54+
};
55+
}
56+
}
57+
58+
window.localizer = {
59+
translations,
60+
};
61+
```
62+
63+
This makes translations available globally and synchronously.
64+
65+
### 2. Add Vite Plugin
3566

3667
Update your `vite.config.ts`:
3768

@@ -47,10 +78,15 @@ export default defineConfig({
4778
debug: true, // Enable debug logging (optional)
4879
}),
4980
],
81+
resolve: {
82+
alias: {
83+
'@': '/resources/js',
84+
},
85+
},
5086
});
5187
```
5288

53-
### 2. Generate Translation Files
89+
### 3. Generate Translation Files
5490

5591
```bash
5692
php artisan localizer:generate --all
@@ -64,9 +100,9 @@ This creates TypeScript files in `resources/js/lang/` directory.
64100

65101
```vue
66102
<script setup lang="ts">
67-
import { useTranslation } from '@devwizard/laravel-localizer-vue';
103+
import { useLocalizer } from '@devwizard/laravel-localizer-vue';
68104
69-
const { __ } = useTranslation();
105+
const { __ } = useLocalizer();
70106
</script>
71107
72108
<template>
@@ -81,9 +117,9 @@ const { __ } = useTranslation();
81117

82118
```vue
83119
<script setup lang="ts">
84-
import { useTranslation } from '@devwizard/laravel-localizer-vue';
120+
import { useLocalizer } from '@devwizard/laravel-localizer-vue';
85121
86-
const { __ } = useTranslation();
122+
const { __ } = useLocalizer();
87123
const userName = 'John';
88124
</script>
89125
@@ -99,9 +135,9 @@ const userName = 'John';
99135

100136
```vue
101137
<script setup lang="ts">
102-
import { useTranslation } from '@devwizard/laravel-localizer-vue';
138+
import { useLocalizer } from '@devwizard/laravel-localizer-vue';
103139
104-
const { choice } = useTranslation();
140+
const { choice } = useLocalizer();
105141
const itemCount = ref(5);
106142
</script>
107143
@@ -114,9 +150,9 @@ const itemCount = ref(5);
114150

115151
```vue
116152
<script setup lang="ts">
117-
import { useTranslation } from '@devwizard/laravel-localizer-vue';
153+
import { useLocalizer } from '@devwizard/laravel-localizer-vue';
118154
119-
const { dir, locale } = useTranslation();
155+
const { dir, locale } = useLocalizer();
120156
</script>
121157
122158
<template>
@@ -130,9 +166,9 @@ const { dir, locale } = useTranslation();
130166

131167
```vue
132168
<script setup lang="ts">
133-
import { useTranslation } from '@devwizard/laravel-localizer-vue';
169+
import { useLocalizer } from '@devwizard/laravel-localizer-vue';
134170
135-
const { __, has } = useTranslation();
171+
const { __, has } = useLocalizer();
136172
</script>
137173
138174
<template>
@@ -146,9 +182,9 @@ const { __, has } = useTranslation();
146182

147183
```vue
148184
<script setup lang="ts">
149-
import { useTranslation } from '@devwizard/laravel-localizer-vue';
185+
import { useLocalizer } from '@devwizard/laravel-localizer-vue';
150186
151-
const { translations, locale } = useTranslation();
187+
const { translations, locale } = useLocalizer();
152188
153189
// translations and locale are reactive ComputedRefs
154190
// They automatically update when the locale changes
@@ -164,7 +200,7 @@ const { translations, locale } = useTranslation();
164200

165201
## API Reference
166202

167-
### `useTranslation()`
203+
### `useLocalizer()`
168204

169205
Returns an object with the following properties and methods:
170206

@@ -185,17 +221,14 @@ Returns an object with the following properties and methods:
185221

186222
```typescript
187223
laravelLocalizer({
224+
// Watch patterns for language file changes (relative to project root)
225+
patterns: ['lang/**', 'resources/lang/**'],
226+
188227
// Command to run when lang files change
189228
command: 'php artisan localizer:generate --all',
190229

191-
// Watch paths for changes
192-
watch: ['lang/**', 'resources/lang/**'],
193-
194230
// Enable debug logging
195231
debug: false,
196-
197-
// Debounce delay in milliseconds
198-
debounce: 300,
199232
});
200233
```
201234

0 commit comments

Comments
 (0)