vite-plugin-shipi18n 1.0.0 → 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,10 @@
1
+ vite-plugin-shipi18n
2
+ Copyright 2025 Shipi18n
3
+
4
+ This product includes software developed at Shipi18n (https://shipi18n.com).
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
package/README.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # vite-plugin-shipi18n
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/vite-plugin-shipi18n)](https://www.npmjs.com/package/vite-plugin-shipi18n)
4
+ [![npm downloads](https://img.shields.io/npm/dw/vite-plugin-shipi18n)](https://www.npmjs.com/package/vite-plugin-shipi18n)
5
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
+ [![GitHub last commit](https://img.shields.io/github/last-commit/Shipi18n/vite-plugin-shipi18n)](https://github.com/Shipi18n/vite-plugin-shipi18n)
7
+ [![CI](https://github.com/Shipi18n/vite-plugin-shipi18n/actions/workflows/ci.yml/badge.svg)](https://github.com/Shipi18n/vite-plugin-shipi18n/actions)
8
+ [![codecov](https://codecov.io/gh/Shipi18n/vite-plugin-shipi18n/branch/main/graph/badge.svg)](https://codecov.io/gh/Shipi18n/vite-plugin-shipi18n)
9
+
3
10
  > Automatic i18n translation at build time for Vite projects
4
11
 
5
12
  Translate your locale files automatically during the build process using [Shipi18n](https://shipi18n.com) API. No manual copy-pasting, no external tools - just add the plugin and run `npm run build`.
@@ -89,6 +96,42 @@ The plugin will automatically:
89
96
  | `apiUrl` | `string` | Shipi18n production URL | Custom API URL (for self-hosted instances) |
90
97
  | `cache` | `boolean` | `true` | Enable smart caching |
91
98
  | `cacheDir` | `string` | `'node_modules/.cache/vite-plugin-shipi18n'` | Cache directory path |
99
+ | `fallback` | `object` | `{ fallbackToSource: true, regionalFallback: true }` | Fallback options (see below) |
100
+
101
+ ### Fallback Options
102
+
103
+ The plugin includes intelligent fallback handling for missing translations:
104
+
105
+ | Option | Type | Default | Description |
106
+ |--------|------|---------|-------------|
107
+ | `fallback.fallbackToSource` | `boolean` | `true` | Use source content when translation is missing |
108
+ | `fallback.regionalFallback` | `boolean` | `true` | Fall back pt-BR → pt, zh-TW → zh when regional variant missing |
109
+
110
+ **Example with fallback:**
111
+
112
+ ```javascript
113
+ shipi18n({
114
+ apiKey: process.env.VITE_SHIPI18N_API_KEY,
115
+ targetLanguages: ['es', 'pt-BR', 'zh-TW'],
116
+ fallback: {
117
+ fallbackToSource: true, // Use English if translation fails
118
+ regionalFallback: true, // pt-BR falls back to pt if pt-BR fails
119
+ }
120
+ })
121
+ ```
122
+
123
+ **Build output:**
124
+ ```
125
+ 🌍 Shipi18n: Starting translation process...
126
+ Source: public/locales/en
127
+ Target languages: es, pt-BR, zh-TW
128
+ Found 2 source file(s)
129
+ ⏳ translation.json: Translating to 3 language(s)...
130
+ ✓ translation.json: Translation complete
131
+ ℹ️ pt-BR used pt translation (regional fallback)
132
+ ⚠️ zh-TW used source content (fallback)
133
+ ✅ Shipi18n: Translation complete!
134
+ ```
92
135
 
93
136
  ### Full Configuration Example
94
137
 
@@ -297,16 +340,39 @@ Subsequent builds: No, cached translations are used instantly.
297
340
 
298
341
  **Tip**: Cache `node_modules/.cache/vite-plugin-shipi18n/` in your CI/CD for faster builds.
299
342
 
300
- ## License
343
+ ## Documentation & Resources
344
+
345
+ 📚 **Full Documentation:** [shipi18n.com/integrations/react](https://shipi18n.com/integrations/react)
301
346
 
302
- MIT
347
+ | Resource | Link |
348
+ |----------|------|
349
+ | **Getting Started** | [shipi18n.com](https://shipi18n.com) |
350
+ | **API Reference** | [shipi18n.com/api](https://shipi18n.com/api) |
351
+ | **i18next Best Practices** | [shipi18n.com/integrations/react](https://shipi18n.com/integrations/react) |
352
+ | **Blog & Tutorials** | [shipi18n.com/blog](https://shipi18n.com/blog) |
303
353
 
304
- ## Support
354
+ ## Related Packages
355
+
356
+ | Package | Description |
357
+ |---------|-------------|
358
+ | [@shipi18n/api](https://www.npmjs.com/package/@shipi18n/api) | Node.js SDK for programmatic use |
359
+ | [@shipi18n/cli](https://www.npmjs.com/package/@shipi18n/cli) | CLI tool for translating files |
360
+ | [i18next-shipi18n-backend](https://www.npmjs.com/package/i18next-shipi18n-backend) | i18next backend for dynamic loading |
361
+ | [shipi18n-github-action](https://github.com/marketplace/actions/shipi18n-auto-translate) | GitHub Action for CI/CD |
362
+
363
+ ## Examples
364
+
365
+ - [Node.js Example](https://github.com/Shipi18n/shipi18n-nodejs-example) - Basic usage examples
366
+ - [Vue Example](https://github.com/Shipi18n/shipi18n-vue-example) - Vue 3 + vue-i18n integration
367
+
368
+ ## License
305
369
 
306
- - [Documentation](https://shipi18n.com/docs)
307
- - [GitHub Issues](https://github.com/Shipi18n/vite-plugin-shipi18n/issues)
308
- - [Discord Community](https://discord.gg/shipi18n)
370
+ Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE).
309
371
 
310
372
  ---
311
373
 
312
- Built with [Shipi18n](https://shipi18n.com) - Smart translation API for developers
374
+ <p align="center">
375
+ <a href="https://shipi18n.com">shipi18n.com</a> ·
376
+ <a href="https://github.com/Shipi18n">GitHub</a> ·
377
+ <a href="https://shipi18n.com/pricing">Pricing</a>
378
+ </p>
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "vite-plugin-shipi18n",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Vite plugin for automatic i18n translation at build time using Shipi18n API",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "src/index.js",
9
- "README.md"
9
+ "README.md",
10
+ "NOTICE"
10
11
  ],
11
12
  "scripts": {
12
13
  "test": "NODE_OPTIONS='--experimental-vm-modules' jest",
13
- "test:watch": "NODE_OPTIONS='--experimental-vm-modules' jest --watch"
14
+ "test:watch": "NODE_OPTIONS='--experimental-vm-modules' jest --watch",
15
+ "test:coverage": "NODE_OPTIONS='--experimental-vm-modules' jest --coverage"
14
16
  },
15
17
  "keywords": [
16
18
  "vite",
@@ -21,7 +23,7 @@
21
23
  "shipi18n"
22
24
  ],
23
25
  "author": "Shipi18n",
24
- "license": "MIT",
26
+ "license": "Apache-2.0",
25
27
  "repository": {
26
28
  "type": "git",
27
29
  "url": "https://github.com/Shipi18n/vite-plugin-shipi18n.git"
package/src/index.js CHANGED
@@ -14,6 +14,9 @@ import crypto from 'crypto'
14
14
  * @param {string} options.sourceLanguage - Source language code (default: 'en')
15
15
  * @param {boolean} options.cache - Enable caching (default: true)
16
16
  * @param {string} options.cacheDir - Cache directory (default: 'node_modules/.cache/vite-plugin-shipi18n')
17
+ * @param {Object} options.fallback - Fallback options
18
+ * @param {boolean} options.fallback.fallbackToSource - Use source content when translation missing (default: true)
19
+ * @param {boolean} options.fallback.regionalFallback - Enable pt-BR -> pt fallback (default: true)
17
20
  */
18
21
  export default function shipi18nPlugin(options = {}) {
19
22
  const {
@@ -24,9 +27,15 @@ export default function shipi18nPlugin(options = {}) {
24
27
  outputDir = 'public/locales',
25
28
  sourceLanguage = 'en',
26
29
  cache = true,
27
- cacheDir = 'node_modules/.cache/vite-plugin-shipi18n'
30
+ cacheDir = 'node_modules/.cache/vite-plugin-shipi18n',
31
+ fallback = {}
28
32
  } = options
29
33
 
34
+ const {
35
+ fallbackToSource = true,
36
+ regionalFallback = true,
37
+ } = fallback
38
+
30
39
  // Validation
31
40
  if (!apiKey) {
32
41
  throw new Error('vite-plugin-shipi18n: apiKey is required')
@@ -110,6 +119,9 @@ export default function shipi18nPlugin(options = {}) {
110
119
  }
111
120
  }
112
121
 
122
+ // Process regional languages for fallback
123
+ const { processedTargets, regionalMap } = processRegionalLanguages(targetLanguages, regionalFallback)
124
+
113
125
  // Translate if not cached
114
126
  if (!translations) {
115
127
  console.log(` ⏳ ${fileName}: Translating to ${targetLanguages.length} language(s)...`)
@@ -120,18 +132,54 @@ export default function shipi18nPlugin(options = {}) {
120
132
  apiUrl,
121
133
  json: sourceJson,
122
134
  sourceLanguage,
123
- targetLanguages
135
+ targetLanguages: processedTargets
124
136
  })
125
137
 
138
+ // Apply fallback logic
139
+ translations = applyFallbacks(
140
+ translations,
141
+ sourceJson,
142
+ targetLanguages,
143
+ sourceLanguage,
144
+ fallbackToSource,
145
+ regionalFallback,
146
+ regionalMap
147
+ )
148
+
126
149
  // Save to cache
127
150
  if (cache) {
128
151
  fs.writeFileSync(cacheFile, JSON.stringify(translations, null, 2))
129
152
  }
130
153
 
131
154
  console.log(` ✓ ${fileName}: Translation complete`)
155
+
156
+ // Log fallback info if any were used
157
+ if (translations.fallbackInfo && translations.fallbackInfo.used) {
158
+ const fi = translations.fallbackInfo
159
+ if (Object.keys(fi.regionalFallbacks).length > 0) {
160
+ for (const [lang, baseLang] of Object.entries(fi.regionalFallbacks)) {
161
+ console.log(` ℹ️ ${lang} used ${baseLang} translation (regional fallback)`)
162
+ }
163
+ }
164
+ if (fi.languagesFallbackToSource.length > 0) {
165
+ for (const lang of fi.languagesFallbackToSource) {
166
+ console.log(` ⚠️ ${lang} used source content (fallback)`)
167
+ }
168
+ }
169
+ }
132
170
  } catch (error) {
133
171
  console.error(` ❌ ${fileName}: Translation failed - ${error.message}`)
134
- continue
172
+
173
+ // Use source as fallback on error if enabled
174
+ if (fallbackToSource) {
175
+ console.log(` ⚠️ Using source content as fallback for all languages`)
176
+ translations = {}
177
+ for (const lang of targetLanguages) {
178
+ translations[lang] = { ...sourceJson }
179
+ }
180
+ } else {
181
+ continue
182
+ }
135
183
  }
136
184
  }
137
185
 
@@ -187,3 +235,168 @@ async function translateJSON({ apiKey, apiUrl, json, sourceLanguage, targetLangu
187
235
  const data = await response.json()
188
236
  return data.translations || {}
189
237
  }
238
+
239
+ /**
240
+ * Process regional language codes for fallback support
241
+ * @exported for testing
242
+ */
243
+ export function processRegionalLanguages(targetLanguages, regionalFallback) {
244
+ const regionalMap = {}
245
+ const processedTargets = []
246
+ const baseLanguagesAdded = new Set()
247
+
248
+ for (const lang of targetLanguages) {
249
+ if (lang.includes('-') && regionalFallback) {
250
+ const baseLang = lang.split('-')[0]
251
+ regionalMap[lang] = baseLang
252
+
253
+ if (!baseLanguagesAdded.has(baseLang) && !targetLanguages.includes(baseLang)) {
254
+ processedTargets.push(baseLang)
255
+ baseLanguagesAdded.add(baseLang)
256
+ }
257
+ }
258
+
259
+ if (!processedTargets.includes(lang)) {
260
+ processedTargets.push(lang)
261
+ }
262
+ }
263
+
264
+ return { processedTargets, regionalMap }
265
+ }
266
+
267
+ /**
268
+ * Apply fallback logic to translation results
269
+ * @exported for testing
270
+ */
271
+ export function applyFallbacks(result, sourceContent, targetLanguages, sourceLanguage, fallbackToSource, regionalFallback, regionalMap) {
272
+ const fallbackInfo = {
273
+ used: false,
274
+ languagesFallbackToSource: [],
275
+ regionalFallbacks: {},
276
+ keysFallback: {},
277
+ }
278
+
279
+ for (const lang of targetLanguages) {
280
+ const translation = result[lang]
281
+
282
+ // Case 1: Entire language missing
283
+ if (!translation || Object.keys(translation).length === 0) {
284
+ // Try regional fallback first
285
+ if (regionalFallback && regionalMap[lang]) {
286
+ const baseLang = regionalMap[lang]
287
+ const baseTranslation = result[baseLang]
288
+
289
+ if (baseTranslation && Object.keys(baseTranslation).length > 0) {
290
+ result[lang] = { ...baseTranslation }
291
+ fallbackInfo.used = true
292
+ fallbackInfo.regionalFallbacks[lang] = baseLang
293
+ continue
294
+ }
295
+ }
296
+
297
+ // Fall back to source
298
+ if (fallbackToSource) {
299
+ result[lang] = { ...sourceContent }
300
+ fallbackInfo.used = true
301
+ fallbackInfo.languagesFallbackToSource.push(lang)
302
+ }
303
+ continue
304
+ }
305
+
306
+ // Case 2: Check for missing keys
307
+ if (fallbackToSource && typeof translation === 'object') {
308
+ const missingKeys = findMissingKeys(sourceContent, translation)
309
+
310
+ if (missingKeys.length > 0) {
311
+ fallbackInfo.used = true
312
+ fallbackInfo.keysFallback[lang] = missingKeys
313
+
314
+ for (const key of missingKeys) {
315
+ const fallbackValue = getNestedValue(sourceContent, key)
316
+
317
+ // Try regional fallback first
318
+ if (regionalFallback && regionalMap[lang]) {
319
+ const baseLang = regionalMap[lang]
320
+ const baseTranslation = result[baseLang]
321
+ const baseValue = baseTranslation ? getNestedValue(baseTranslation, key) : undefined
322
+
323
+ if (baseValue !== undefined) {
324
+ setNestedValue(translation, key, baseValue)
325
+ continue
326
+ }
327
+ }
328
+
329
+ if (fallbackValue !== undefined) {
330
+ setNestedValue(translation, key, fallbackValue)
331
+ }
332
+ }
333
+ }
334
+ }
335
+ }
336
+
337
+ if (fallbackInfo.used) {
338
+ result.fallbackInfo = fallbackInfo
339
+ }
340
+
341
+ return result
342
+ }
343
+
344
+ /**
345
+ * Find missing keys in translation
346
+ * @exported for testing
347
+ */
348
+ export function findMissingKeys(source, translation, prefix = '') {
349
+ const missing = []
350
+
351
+ for (const key of Object.keys(source)) {
352
+ const fullKey = prefix ? `${prefix}.${key}` : key
353
+ const sourceValue = source[key]
354
+ const translationValue = translation[key]
355
+
356
+ if (translationValue === undefined || translationValue === null || translationValue === '') {
357
+ missing.push(fullKey)
358
+ } else if (
359
+ typeof sourceValue === 'object' &&
360
+ sourceValue !== null &&
361
+ !Array.isArray(sourceValue) &&
362
+ typeof translationValue === 'object' &&
363
+ translationValue !== null
364
+ ) {
365
+ missing.push(...findMissingKeys(sourceValue, translationValue, fullKey))
366
+ }
367
+ }
368
+
369
+ return missing
370
+ }
371
+
372
+ /**
373
+ * Get nested value from object using dot notation
374
+ * @exported for testing
375
+ */
376
+ export function getNestedValue(obj, path) {
377
+ return path.split('.').reduce((current, key) => {
378
+ if (current && typeof current === 'object' && key in current) {
379
+ return current[key]
380
+ }
381
+ return undefined
382
+ }, obj)
383
+ }
384
+
385
+ /**
386
+ * Set nested value in object using dot notation
387
+ * @exported for testing
388
+ */
389
+ export function setNestedValue(obj, path, value) {
390
+ const keys = path.split('.')
391
+ let current = obj
392
+
393
+ for (let i = 0; i < keys.length - 1; i++) {
394
+ const key = keys[i]
395
+ if (!(key in current) || typeof current[key] !== 'object') {
396
+ current[key] = {}
397
+ }
398
+ current = current[key]
399
+ }
400
+
401
+ current[keys[keys.length - 1]] = value
402
+ }