@zachhandley/ez-i18n 0.1.2 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,118 @@ export default defineConfig({
59
59
 
60
60
  Create similar files for each locale: `src/i18n/en.json`, `src/i18n/es.json`, etc.
61
61
 
62
+ ### Multi-File Translations
63
+
64
+ ez-i18n supports flexible translation file organization:
65
+
66
+ #### Auto-Discovery (Zero Config)
67
+
68
+ Just put your files in `public/i18n/` and ez-i18n will discover them automatically:
69
+
70
+ ```
71
+ public/i18n/
72
+ en/
73
+ common.json
74
+ auth.json
75
+ es/
76
+ common.json
77
+ auth.json
78
+ ```
79
+
80
+ ```typescript
81
+ // astro.config.ts - locales auto-discovered from folder names!
82
+ ezI18n({
83
+ defaultLocale: 'en',
84
+ // No locales or translations needed - auto-discovered
85
+ })
86
+ ```
87
+
88
+ #### Base Directory
89
+
90
+ Point to a folder and locales are discovered from subfolders:
91
+
92
+ ```typescript
93
+ ezI18n({
94
+ defaultLocale: 'en',
95
+ translations: './src/i18n/', // Discovers en/, es/, fr/ folders
96
+ })
97
+ ```
98
+
99
+ #### Per-Locale with Multiple Formats
100
+
101
+ Mix and match different formats per locale:
102
+
103
+ ```typescript
104
+ ezI18n({
105
+ locales: ['en', 'es', 'fr', 'de'],
106
+ defaultLocale: 'en',
107
+ translations: {
108
+ en: './src/i18n/en.json', // Single file
109
+ es: './src/i18n/es/', // Folder (all JSONs merged)
110
+ fr: './src/i18n/fr/**/*.json', // Glob pattern
111
+ de: ['./src/i18n/de/common.json', // Array of files
112
+ './src/i18n/de/auth.json'],
113
+ },
114
+ })
115
+ ```
116
+
117
+ #### Merge Order
118
+
119
+ When using multiple files per locale, files are merged **alphabetically by filename**. Later files override earlier ones for conflicting keys.
120
+
121
+ ```
122
+ en/
123
+ 01-common.json # Loaded first
124
+ 02-features.json # Loaded second, overrides common
125
+ 99-overrides.json # Loaded last, highest priority
126
+ ```
127
+
128
+ #### Path-Based Namespacing
129
+
130
+ When using folder-based translation organization, ez-i18n automatically creates namespaces from your file paths. This is **enabled by default** when using folder-based config.
131
+
132
+ **Example:**
133
+
134
+ ```
135
+ public/i18n/
136
+ en/
137
+ auth/
138
+ login.json # { "title": "Sign In", "button": "Log In" }
139
+ signup.json # { "title": "Create Account" }
140
+ common.json # { "welcome": "Welcome" }
141
+ ```
142
+
143
+ Access translations using dot notation that mirrors the folder structure:
144
+
145
+ ```typescript
146
+ $t('auth.login.title') // "Sign In"
147
+ $t('auth.login.button') // "Log In"
148
+ $t('auth.signup.title') // "Create Account"
149
+ $t('common.welcome') // "Welcome"
150
+ ```
151
+
152
+ **Disable path-based namespacing:**
153
+
154
+ If you prefer to manage namespaces manually within your JSON files, you can disable this feature:
155
+
156
+ ```typescript
157
+ ezI18n({
158
+ defaultLocale: 'en',
159
+ translations: './src/i18n/',
160
+ pathBasedNamespacing: false, // Disable automatic path namespacing
161
+ })
162
+ ```
163
+
164
+ With `pathBasedNamespacing: false`, the file structure is ignored and keys are used directly from each JSON file.
165
+
166
+ #### Cache File
167
+
168
+ A `.ez-i18n.json` cache file is generated to speed up subsequent builds. Add it to `.gitignore`:
169
+
170
+ ```gitignore
171
+ .ez-i18n.json
172
+ ```
173
+
62
174
  ### Layout Setup
63
175
 
64
176
  Add the `EzI18nHead` component to your layout's head for automatic hydration:
@@ -172,6 +284,10 @@ function MyComponent() {
172
284
  - **Vue integration** - Global `$t()`, `$locale`, `$setLocale` in templates
173
285
  - **React integration** - `useI18n()` hook for React components
174
286
  - **Middleware included** - Auto-detects locale from cookie, query param, or Accept-Language header
287
+ - **Multi-file support** - Organize translations in folders, use globs, or arrays
288
+ - **Auto-discovery** - Automatic locale detection from folder structure
289
+ - **Path-based namespacing** - Automatic namespacing from folder structure (`auth/login.json` becomes `auth.login.*`)
290
+ - **HMR in dev** - Hot reload translation changes without restart
175
291
 
176
292
  ## Locale Detection Priority
177
293
 
@@ -188,10 +304,17 @@ Astro integration function.
188
304
 
189
305
  | Option | Type | Required | Description |
190
306
  |--------|------|----------|-------------|
191
- | `locales` | `string[]` | Yes | Supported locale codes |
307
+ | `locales` | `string[]` | No | Supported locale codes (auto-discovered if not provided) |
192
308
  | `defaultLocale` | `string` | Yes | Fallback locale |
193
309
  | `cookieName` | `string` | No | Cookie name (default: `'ez-locale'`) |
194
- | `translations` | `Record<string, string>` | No | Paths to translation files (auto-loaded) |
310
+ | `translations` | `string \| Record<string, TranslationPath>` | No | Base directory or per-locale paths (default: `./public/i18n/`) |
311
+ | `pathBasedNamespacing` | `boolean` | No | Auto-namespace translations from folder paths (default: `true` for folder-based config) |
312
+
313
+ **TranslationPath** can be:
314
+ - Single file: `'./src/i18n/en.json'`
315
+ - Folder: `'./src/i18n/en/'`
316
+ - Glob: `'./src/i18n/en/**/*.json'`
317
+ - Array: `['./common.json', './auth.json']`
195
318
 
196
319
  ### `EzI18nHead`
197
320
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AstroIntegration } from 'astro';
2
- import { E as EzI18nConfig } from './types-DwCG8sp8.js';
3
- export { T as TranslateFunction } from './types-DwCG8sp8.js';
2
+ import { E as EzI18nConfig } from './types-Cd9e7Lkc.js';
3
+ export { T as TranslateFunction } from './types-Cd9e7Lkc.js';
4
4
 
5
5
  /**
6
6
  * ez-i18n Astro integration