@kopynator/core 1.0.2 → 1.0.9
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/dist/index.d.mts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +26 -13
- package/dist/index.mjs +26 -13
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -2,6 +2,8 @@ interface KopyConfig {
|
|
|
2
2
|
apiKey: string;
|
|
3
3
|
projectId?: string;
|
|
4
4
|
baseUrl?: string;
|
|
5
|
+
/** Base URL for local assets (e.g. https://yoursite.com). Required for SSR so the server can fetch /assets/i18n/*.json */
|
|
6
|
+
localBaseUrl?: string;
|
|
5
7
|
defaultLocale?: string;
|
|
6
8
|
mode?: 'local' | 'live' | 'hybrid';
|
|
7
9
|
languages?: string[];
|
|
@@ -65,7 +67,9 @@ declare class Kopynator {
|
|
|
65
67
|
getCurrentLocale(): string;
|
|
66
68
|
/**
|
|
67
69
|
* Load translations for a specific locale.
|
|
68
|
-
*
|
|
70
|
+
* - local: only from /assets/i18n
|
|
71
|
+
* - live: only from API (no local fetch)
|
|
72
|
+
* - hybrid: local as base, then API as override
|
|
69
73
|
*/
|
|
70
74
|
loadLocale(locale: string): Promise<void>;
|
|
71
75
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ interface KopyConfig {
|
|
|
2
2
|
apiKey: string;
|
|
3
3
|
projectId?: string;
|
|
4
4
|
baseUrl?: string;
|
|
5
|
+
/** Base URL for local assets (e.g. https://yoursite.com). Required for SSR so the server can fetch /assets/i18n/*.json */
|
|
6
|
+
localBaseUrl?: string;
|
|
5
7
|
defaultLocale?: string;
|
|
6
8
|
mode?: 'local' | 'live' | 'hybrid';
|
|
7
9
|
languages?: string[];
|
|
@@ -65,7 +67,9 @@ declare class Kopynator {
|
|
|
65
67
|
getCurrentLocale(): string;
|
|
66
68
|
/**
|
|
67
69
|
* Load translations for a specific locale.
|
|
68
|
-
*
|
|
70
|
+
* - local: only from /assets/i18n
|
|
71
|
+
* - live: only from API (no local fetch)
|
|
72
|
+
* - hybrid: local as base, then API as override
|
|
69
73
|
*/
|
|
70
74
|
loadLocale(locale: string): Promise<void>;
|
|
71
75
|
/**
|
package/dist/index.js
CHANGED
|
@@ -46,7 +46,7 @@ function flatten(obj, prefix = "") {
|
|
|
46
46
|
var KopyFetcher = class {
|
|
47
47
|
constructor(config) {
|
|
48
48
|
this.config = config;
|
|
49
|
-
this.baseUrl = config.baseUrl || "https://api.kopynator.com";
|
|
49
|
+
this.baseUrl = config.baseUrl || "https://api.kopynator.com/api";
|
|
50
50
|
}
|
|
51
51
|
baseUrl;
|
|
52
52
|
async fetchTranslations(locale) {
|
|
@@ -79,7 +79,9 @@ var KopyFetcher = class {
|
|
|
79
79
|
}
|
|
80
80
|
async fetchLocal(locale) {
|
|
81
81
|
try {
|
|
82
|
-
const
|
|
82
|
+
const base = (this.config.localBaseUrl || "").replace(/\/$/, "");
|
|
83
|
+
const path = `/assets/i18n/${locale}.json`;
|
|
84
|
+
const url = base ? `${base}${path}` : path;
|
|
83
85
|
const response = await fetch(url);
|
|
84
86
|
if (!response.ok) {
|
|
85
87
|
throw new Error(`Local translations not found: ${response.statusText}`);
|
|
@@ -202,28 +204,39 @@ var Kopynator = class {
|
|
|
202
204
|
}
|
|
203
205
|
/**
|
|
204
206
|
* Load translations for a specific locale.
|
|
205
|
-
*
|
|
207
|
+
* - local: only from /assets/i18n
|
|
208
|
+
* - live: only from API (no local fetch)
|
|
209
|
+
* - hybrid: local as base, then API as override
|
|
206
210
|
*/
|
|
207
211
|
async loadLocale(locale) {
|
|
208
212
|
if (this.translations[locale] && Object.keys(this.translations[locale]).length > 0) {
|
|
209
213
|
return;
|
|
210
214
|
}
|
|
211
215
|
this.translations[locale] = {};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
+
if (this.config.mode === "live") {
|
|
217
|
+
try {
|
|
218
|
+
const remoteData = await this.fetcher.fetchTranslations(locale);
|
|
219
|
+
if (remoteData && Object.keys(remoteData).length > 0) {
|
|
220
|
+
this.translations[locale] = { ...remoteData };
|
|
221
|
+
}
|
|
222
|
+
} catch (e) {
|
|
216
223
|
}
|
|
217
|
-
|
|
224
|
+
return;
|
|
218
225
|
}
|
|
219
|
-
if (this.config.mode
|
|
226
|
+
if (this.config.mode === "local" || this.config.mode === "hybrid") {
|
|
227
|
+
try {
|
|
228
|
+
const localData = await this.fetcher.fetchLocal(locale);
|
|
229
|
+
if (localData && Object.keys(localData).length > 0) {
|
|
230
|
+
this.translations[locale] = { ...localData };
|
|
231
|
+
}
|
|
232
|
+
} catch (e) {
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (this.config.mode === "hybrid") {
|
|
220
236
|
try {
|
|
221
237
|
const remoteData = await this.fetcher.fetchTranslations(locale);
|
|
222
238
|
if (remoteData && Object.keys(remoteData).length > 0) {
|
|
223
|
-
this.translations[locale] = {
|
|
224
|
-
...this.translations[locale],
|
|
225
|
-
...remoteData
|
|
226
|
-
};
|
|
239
|
+
this.translations[locale] = { ...this.translations[locale], ...remoteData };
|
|
227
240
|
}
|
|
228
241
|
} catch (e) {
|
|
229
242
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -15,7 +15,7 @@ function flatten(obj, prefix = "") {
|
|
|
15
15
|
var KopyFetcher = class {
|
|
16
16
|
constructor(config) {
|
|
17
17
|
this.config = config;
|
|
18
|
-
this.baseUrl = config.baseUrl || "https://api.kopynator.com";
|
|
18
|
+
this.baseUrl = config.baseUrl || "https://api.kopynator.com/api";
|
|
19
19
|
}
|
|
20
20
|
baseUrl;
|
|
21
21
|
async fetchTranslations(locale) {
|
|
@@ -48,7 +48,9 @@ var KopyFetcher = class {
|
|
|
48
48
|
}
|
|
49
49
|
async fetchLocal(locale) {
|
|
50
50
|
try {
|
|
51
|
-
const
|
|
51
|
+
const base = (this.config.localBaseUrl || "").replace(/\/$/, "");
|
|
52
|
+
const path = `/assets/i18n/${locale}.json`;
|
|
53
|
+
const url = base ? `${base}${path}` : path;
|
|
52
54
|
const response = await fetch(url);
|
|
53
55
|
if (!response.ok) {
|
|
54
56
|
throw new Error(`Local translations not found: ${response.statusText}`);
|
|
@@ -171,28 +173,39 @@ var Kopynator = class {
|
|
|
171
173
|
}
|
|
172
174
|
/**
|
|
173
175
|
* Load translations for a specific locale.
|
|
174
|
-
*
|
|
176
|
+
* - local: only from /assets/i18n
|
|
177
|
+
* - live: only from API (no local fetch)
|
|
178
|
+
* - hybrid: local as base, then API as override
|
|
175
179
|
*/
|
|
176
180
|
async loadLocale(locale) {
|
|
177
181
|
if (this.translations[locale] && Object.keys(this.translations[locale]).length > 0) {
|
|
178
182
|
return;
|
|
179
183
|
}
|
|
180
184
|
this.translations[locale] = {};
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
+
if (this.config.mode === "live") {
|
|
186
|
+
try {
|
|
187
|
+
const remoteData = await this.fetcher.fetchTranslations(locale);
|
|
188
|
+
if (remoteData && Object.keys(remoteData).length > 0) {
|
|
189
|
+
this.translations[locale] = { ...remoteData };
|
|
190
|
+
}
|
|
191
|
+
} catch (e) {
|
|
185
192
|
}
|
|
186
|
-
|
|
193
|
+
return;
|
|
187
194
|
}
|
|
188
|
-
if (this.config.mode
|
|
195
|
+
if (this.config.mode === "local" || this.config.mode === "hybrid") {
|
|
196
|
+
try {
|
|
197
|
+
const localData = await this.fetcher.fetchLocal(locale);
|
|
198
|
+
if (localData && Object.keys(localData).length > 0) {
|
|
199
|
+
this.translations[locale] = { ...localData };
|
|
200
|
+
}
|
|
201
|
+
} catch (e) {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (this.config.mode === "hybrid") {
|
|
189
205
|
try {
|
|
190
206
|
const remoteData = await this.fetcher.fetchTranslations(locale);
|
|
191
207
|
if (remoteData && Object.keys(remoteData).length > 0) {
|
|
192
|
-
this.translations[locale] = {
|
|
193
|
-
...this.translations[locale],
|
|
194
|
-
...remoteData
|
|
195
|
-
};
|
|
208
|
+
this.translations[locale] = { ...this.translations[locale], ...remoteData };
|
|
196
209
|
}
|
|
197
210
|
} catch (e) {
|
|
198
211
|
}
|