@kivacore/i18n 0.0.0 → 0.2.2
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 +390 -1
- package/dist/core-index.d.ts +27 -0
- package/dist/core-index.js +308 -0
- package/dist/core-index.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +414 -0
- package/dist/index.js.map +1 -0
- package/dist/locale-storage-DfFDJf0I.d.ts +141 -0
- package/dist/rn/index.d.ts +119 -0
- package/dist/rn/index.js +582 -0
- package/dist/rn/index.js.map +1 -0
- package/dist/types-Bj3Uk0wv.d.ts +77 -0
- package/dist/vue/index.d.ts +138 -0
- package/dist/vue/index.js +381 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +65 -3
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { defineComponent, ref, shallowRef, provide, onMounted, watch, onUnmounted, inject, computed } from 'vue';
|
|
2
|
+
|
|
3
|
+
// src/vue/index.ts
|
|
4
|
+
|
|
5
|
+
// src/core/interpolation.ts
|
|
6
|
+
function interpolate(template, params) {
|
|
7
|
+
if (!params) return template;
|
|
8
|
+
return template.replace(
|
|
9
|
+
/\{\{(\w+)\}\}|\{(\w+)\}/g,
|
|
10
|
+
(match, doubleBraceKey, singleBraceKey) => {
|
|
11
|
+
const key = doubleBraceKey ?? singleBraceKey;
|
|
12
|
+
if (key === void 0) return match;
|
|
13
|
+
const value = params[key];
|
|
14
|
+
return value !== void 0 ? String(value) : match;
|
|
15
|
+
}
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/core/locale-storage.ts
|
|
20
|
+
var DEFAULT_MAX_AGE = 365 * 24 * 60 * 60;
|
|
21
|
+
var CookieStorageAdapter = class {
|
|
22
|
+
constructor(cookieKey = "docyrus-locale") {
|
|
23
|
+
this.cookieKey = cookieKey;
|
|
24
|
+
}
|
|
25
|
+
cookieKey;
|
|
26
|
+
getLocale() {
|
|
27
|
+
if (typeof document === "undefined") return null;
|
|
28
|
+
const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${escapeRegex(this.cookieKey)}=([^;]*)`));
|
|
29
|
+
return match ? decodeURIComponent(match[1]) : null;
|
|
30
|
+
}
|
|
31
|
+
setLocale(locale) {
|
|
32
|
+
if (typeof document === "undefined") return;
|
|
33
|
+
document.cookie = `${this.cookieKey}=${encodeURIComponent(locale)};path=/;max-age=${DEFAULT_MAX_AGE};SameSite=Lax`;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
function escapeRegex(str) {
|
|
37
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/core/translate-value.ts
|
|
41
|
+
function translateValue(value, t) {
|
|
42
|
+
if (!value.startsWith("translate.")) return value;
|
|
43
|
+
const raw = value.slice(10);
|
|
44
|
+
const key = `common.${raw.toLowerCase().replace(/\s+/g, "-")}`;
|
|
45
|
+
return t(key) === key ? raw : t(key);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/core/i18n-manager.ts
|
|
49
|
+
var I18nManager = class {
|
|
50
|
+
status = "loading";
|
|
51
|
+
locale;
|
|
52
|
+
translations = {};
|
|
53
|
+
apiTranslations = {};
|
|
54
|
+
error = null;
|
|
55
|
+
listeners = /* @__PURE__ */ new Set();
|
|
56
|
+
abortController = null;
|
|
57
|
+
client;
|
|
58
|
+
getAccessToken;
|
|
59
|
+
apiUrl;
|
|
60
|
+
translationsEndpoint;
|
|
61
|
+
staticTranslations;
|
|
62
|
+
storage;
|
|
63
|
+
mergeStrategy;
|
|
64
|
+
fallback;
|
|
65
|
+
disableApiFetch;
|
|
66
|
+
userLanguageEndpoint;
|
|
67
|
+
constructor(config) {
|
|
68
|
+
this.client = config.client ?? null;
|
|
69
|
+
this.getAccessToken = config.getAccessToken;
|
|
70
|
+
this.apiUrl = config.apiUrl;
|
|
71
|
+
this.translationsEndpoint = config.translationsEndpoint ?? "v1/tenant/translations";
|
|
72
|
+
this.staticTranslations = config.staticTranslations ?? {};
|
|
73
|
+
this.storage = config.storage ?? new CookieStorageAdapter(config.cookieKey ?? "docyrus-locale");
|
|
74
|
+
this.mergeStrategy = config.mergeStrategy ?? "api-first";
|
|
75
|
+
this.fallback = config.fallback ?? "key";
|
|
76
|
+
this.disableApiFetch = config.disableApiFetch ?? false;
|
|
77
|
+
this.userLanguageEndpoint = config.userLanguageEndpoint ?? "v1/users/me";
|
|
78
|
+
this.locale = this.storage.getLocale();
|
|
79
|
+
}
|
|
80
|
+
getStatus() {
|
|
81
|
+
return this.status;
|
|
82
|
+
}
|
|
83
|
+
getLocale() {
|
|
84
|
+
return this.locale;
|
|
85
|
+
}
|
|
86
|
+
getTranslations() {
|
|
87
|
+
return this.translations;
|
|
88
|
+
}
|
|
89
|
+
getError() {
|
|
90
|
+
return this.error;
|
|
91
|
+
}
|
|
92
|
+
subscribe(listener) {
|
|
93
|
+
this.listeners.add(listener);
|
|
94
|
+
return () => this.listeners.delete(listener);
|
|
95
|
+
}
|
|
96
|
+
notify() {
|
|
97
|
+
for (const listener of this.listeners) {
|
|
98
|
+
listener({
|
|
99
|
+
status: this.status,
|
|
100
|
+
locale: this.locale,
|
|
101
|
+
translations: this.translations,
|
|
102
|
+
error: this.error
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Initialize the manager.
|
|
108
|
+
* 1. Apply static translations immediately (no loading flash).
|
|
109
|
+
* 2. Fetch from API if enabled and auth is available.
|
|
110
|
+
*/
|
|
111
|
+
async initialize() {
|
|
112
|
+
if (Object.keys(this.staticTranslations).length > 0) {
|
|
113
|
+
this.translations = { ...this.staticTranslations };
|
|
114
|
+
this.status = "ready";
|
|
115
|
+
this.notify();
|
|
116
|
+
}
|
|
117
|
+
if (!this.disableApiFetch && (this.client || this.getAccessToken)) {
|
|
118
|
+
await this.fetchTranslations();
|
|
119
|
+
} else if (this.status !== "ready") {
|
|
120
|
+
this.status = "ready";
|
|
121
|
+
this.notify();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Fetch translations from the API.
|
|
126
|
+
* Uses RestApiClient.get() if client is available, otherwise raw fetch with getAccessToken.
|
|
127
|
+
*/
|
|
128
|
+
async fetchTranslations() {
|
|
129
|
+
this.abortController?.abort();
|
|
130
|
+
this.abortController = new AbortController();
|
|
131
|
+
try {
|
|
132
|
+
let apiData;
|
|
133
|
+
if (this.client) {
|
|
134
|
+
apiData = await this.client.get(
|
|
135
|
+
this.translationsEndpoint
|
|
136
|
+
);
|
|
137
|
+
} else if (this.getAccessToken && this.apiUrl) {
|
|
138
|
+
const token = await this.getAccessToken();
|
|
139
|
+
if (!token) {
|
|
140
|
+
throw new Error("getAccessToken returned null \u2014 cannot fetch translations");
|
|
141
|
+
}
|
|
142
|
+
const url = `${this.apiUrl.replace(/\/$/, "")}/${this.translationsEndpoint}`;
|
|
143
|
+
const res = await fetch(url, {
|
|
144
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
145
|
+
signal: this.abortController.signal
|
|
146
|
+
});
|
|
147
|
+
if (!res.ok) {
|
|
148
|
+
throw new Error(`Translation fetch failed: ${res.status} ${res.statusText}`);
|
|
149
|
+
}
|
|
150
|
+
apiData = await res.json();
|
|
151
|
+
} else {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
this.apiTranslations = apiData.data;
|
|
155
|
+
this.mergeTranslations();
|
|
156
|
+
this.error = null;
|
|
157
|
+
this.status = "ready";
|
|
158
|
+
this.notify();
|
|
159
|
+
} catch (err) {
|
|
160
|
+
if (err.name === "AbortError") return;
|
|
161
|
+
this.error = err instanceof Error ? err : new Error(String(err));
|
|
162
|
+
if (Object.keys(this.translations).length > 0) {
|
|
163
|
+
this.notify();
|
|
164
|
+
} else {
|
|
165
|
+
this.status = "error";
|
|
166
|
+
this.notify();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Translate a key.
|
|
172
|
+
* - `t('key')` — returns translated value or key itself
|
|
173
|
+
* - `t('key', 'Fallback')` — returns translated value or fallback string
|
|
174
|
+
* - `t('key', { name: 'Ali' })` — returns interpolated value with params
|
|
175
|
+
*/
|
|
176
|
+
t(key, fallbackOrParams) {
|
|
177
|
+
const isParams = typeof fallbackOrParams === "object";
|
|
178
|
+
const params = isParams ? fallbackOrParams : void 0;
|
|
179
|
+
const fallbackStr = typeof fallbackOrParams === "string" ? fallbackOrParams : void 0;
|
|
180
|
+
const value = this.translations[key];
|
|
181
|
+
if (value !== void 0) {
|
|
182
|
+
return interpolate(value, params);
|
|
183
|
+
}
|
|
184
|
+
if (fallbackStr !== void 0) return fallbackStr;
|
|
185
|
+
if (this.fallback === "key") return interpolate(key, params);
|
|
186
|
+
if (this.fallback === "empty") return "";
|
|
187
|
+
return this.fallback(key);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Translate API response values with "translate." prefix.
|
|
191
|
+
* e.g. tv('translate.Active') → 'Aktif'
|
|
192
|
+
*/
|
|
193
|
+
tv(value) {
|
|
194
|
+
return translateValue(value, (key, params) => this.t(key, params));
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Change the active locale.
|
|
198
|
+
* Persists to storage, updates API (PATCH users/me), and triggers translation refetch.
|
|
199
|
+
*/
|
|
200
|
+
async setLocale(locale) {
|
|
201
|
+
if (locale === this.locale) return;
|
|
202
|
+
this.locale = locale;
|
|
203
|
+
this.storage.setLocale(locale);
|
|
204
|
+
this.notify();
|
|
205
|
+
const promises = [];
|
|
206
|
+
if (this.userLanguageEndpoint !== false && (this.client || this.getAccessToken)) {
|
|
207
|
+
promises.push(this.persistUserLanguage(locale));
|
|
208
|
+
}
|
|
209
|
+
if (!this.disableApiFetch && (this.client || this.getAccessToken)) {
|
|
210
|
+
promises.push(this.fetchTranslations());
|
|
211
|
+
}
|
|
212
|
+
await Promise.all(promises);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Persist user language preference to API.
|
|
216
|
+
* PATCH {userLanguageEndpoint} { language: locale }
|
|
217
|
+
*/
|
|
218
|
+
async persistUserLanguage(locale) {
|
|
219
|
+
if (this.userLanguageEndpoint === false) return;
|
|
220
|
+
try {
|
|
221
|
+
if (this.client) {
|
|
222
|
+
await this.client.patch(this.userLanguageEndpoint, { language: locale });
|
|
223
|
+
} else if (this.getAccessToken && this.apiUrl) {
|
|
224
|
+
const token = await this.getAccessToken();
|
|
225
|
+
if (!token) return;
|
|
226
|
+
const url = `${this.apiUrl.replace(/\/$/, "")}/${this.userLanguageEndpoint}`;
|
|
227
|
+
await fetch(url, {
|
|
228
|
+
method: "PATCH",
|
|
229
|
+
headers: {
|
|
230
|
+
Authorization: `Bearer ${token}`,
|
|
231
|
+
"Content-Type": "application/json"
|
|
232
|
+
},
|
|
233
|
+
body: JSON.stringify({ language: locale })
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
} catch {
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Update config at runtime.
|
|
241
|
+
* Useful when client transitions from null → RestApiClient (auth loading complete).
|
|
242
|
+
*/
|
|
243
|
+
updateConfig(updates) {
|
|
244
|
+
let shouldFetch = false;
|
|
245
|
+
if (updates.client !== void 0 && updates.client !== this.client) {
|
|
246
|
+
const hadNoClient = !this.client;
|
|
247
|
+
this.client = updates.client;
|
|
248
|
+
if (hadNoClient && this.client && !this.disableApiFetch) {
|
|
249
|
+
shouldFetch = true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (updates.staticTranslations !== void 0) {
|
|
253
|
+
this.staticTranslations = updates.staticTranslations;
|
|
254
|
+
this.mergeTranslations();
|
|
255
|
+
this.notify();
|
|
256
|
+
}
|
|
257
|
+
if (shouldFetch) {
|
|
258
|
+
this.fetchTranslations();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/** Cleanup: abort in-flight requests + clear listeners. */
|
|
262
|
+
destroy() {
|
|
263
|
+
this.abortController?.abort();
|
|
264
|
+
this.listeners.clear();
|
|
265
|
+
}
|
|
266
|
+
mergeTranslations() {
|
|
267
|
+
if (this.mergeStrategy === "api-first") {
|
|
268
|
+
this.translations = { ...this.staticTranslations, ...this.apiTranslations };
|
|
269
|
+
} else {
|
|
270
|
+
this.translations = { ...this.apiTranslations, ...this.staticTranslations };
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// src/vue/index.ts
|
|
276
|
+
var DOCYRUS_I18N_KEY = /* @__PURE__ */ Symbol("docyrus-i18n");
|
|
277
|
+
function useDocyrusI18n() {
|
|
278
|
+
const context = inject(DOCYRUS_I18N_KEY);
|
|
279
|
+
if (!context) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
"useDocyrusI18n() must be used within a <DocyrusI18nProvider>. Wrap your app with <DocyrusI18nProvider> to provide i18n context."
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
return context;
|
|
285
|
+
}
|
|
286
|
+
function useTranslation() {
|
|
287
|
+
const context = useDocyrusI18n();
|
|
288
|
+
return {
|
|
289
|
+
t: computed(() => context.t),
|
|
290
|
+
isLoading: computed(() => context.isLoading)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
var DocyrusI18nProvider = defineComponent({
|
|
294
|
+
name: "DocyrusI18nProvider",
|
|
295
|
+
props: {
|
|
296
|
+
client: { type: Object, default: null },
|
|
297
|
+
getAccessToken: { type: Function, default: void 0 },
|
|
298
|
+
apiUrl: { type: String, default: void 0 },
|
|
299
|
+
translationsEndpoint: { type: String, default: void 0 },
|
|
300
|
+
staticTranslations: { type: Object, default: void 0 },
|
|
301
|
+
cookieKey: { type: String, default: void 0 },
|
|
302
|
+
mergeStrategy: { type: String, default: void 0 },
|
|
303
|
+
fallback: { type: [String, Function], default: void 0 },
|
|
304
|
+
disableApiFetch: { type: Boolean, default: void 0 }
|
|
305
|
+
},
|
|
306
|
+
setup(props, { slots }) {
|
|
307
|
+
const status = ref("loading");
|
|
308
|
+
const locale = ref(null);
|
|
309
|
+
const translations = shallowRef({});
|
|
310
|
+
const error = shallowRef(null);
|
|
311
|
+
let manager = null;
|
|
312
|
+
const setLocale = (newLocale) => {
|
|
313
|
+
manager?.setLocale(newLocale);
|
|
314
|
+
};
|
|
315
|
+
const refetch = async () => {
|
|
316
|
+
await manager?.fetchTranslations();
|
|
317
|
+
};
|
|
318
|
+
const t = (key, fallbackOrParams) => {
|
|
319
|
+
return manager?.t(key, fallbackOrParams) ?? (typeof fallbackOrParams === "string" ? fallbackOrParams : key);
|
|
320
|
+
};
|
|
321
|
+
const tv = (value) => {
|
|
322
|
+
return manager?.tv(value) ?? value;
|
|
323
|
+
};
|
|
324
|
+
const i18nState = {
|
|
325
|
+
get status() {
|
|
326
|
+
return status.value;
|
|
327
|
+
},
|
|
328
|
+
get locale() {
|
|
329
|
+
return locale.value;
|
|
330
|
+
},
|
|
331
|
+
get translations() {
|
|
332
|
+
return translations.value;
|
|
333
|
+
},
|
|
334
|
+
get error() {
|
|
335
|
+
return error.value;
|
|
336
|
+
},
|
|
337
|
+
get isLoading() {
|
|
338
|
+
return status.value === "loading";
|
|
339
|
+
},
|
|
340
|
+
t,
|
|
341
|
+
tv,
|
|
342
|
+
setLocale,
|
|
343
|
+
refetch
|
|
344
|
+
};
|
|
345
|
+
provide(DOCYRUS_I18N_KEY, i18nState);
|
|
346
|
+
onMounted(() => {
|
|
347
|
+
const config = {
|
|
348
|
+
client: props.client,
|
|
349
|
+
getAccessToken: props.getAccessToken,
|
|
350
|
+
apiUrl: props.apiUrl,
|
|
351
|
+
translationsEndpoint: props.translationsEndpoint,
|
|
352
|
+
staticTranslations: props.staticTranslations,
|
|
353
|
+
cookieKey: props.cookieKey,
|
|
354
|
+
mergeStrategy: props.mergeStrategy,
|
|
355
|
+
fallback: props.fallback,
|
|
356
|
+
disableApiFetch: props.disableApiFetch
|
|
357
|
+
};
|
|
358
|
+
manager = new I18nManager(config);
|
|
359
|
+
locale.value = manager.getLocale();
|
|
360
|
+
manager.subscribe((state) => {
|
|
361
|
+
status.value = state.status;
|
|
362
|
+
locale.value = state.locale;
|
|
363
|
+
translations.value = state.translations;
|
|
364
|
+
error.value = state.error;
|
|
365
|
+
});
|
|
366
|
+
manager.initialize();
|
|
367
|
+
});
|
|
368
|
+
watch(() => props.client, (newClient) => {
|
|
369
|
+
manager?.updateConfig({ client: newClient });
|
|
370
|
+
});
|
|
371
|
+
onUnmounted(() => {
|
|
372
|
+
manager?.destroy();
|
|
373
|
+
manager = null;
|
|
374
|
+
});
|
|
375
|
+
return () => slots.default?.();
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
export { DocyrusI18nProvider, useDocyrusI18n, useTranslation };
|
|
380
|
+
//# sourceMappingURL=index.js.map
|
|
381
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/interpolation.ts","../../src/core/locale-storage.ts","../../src/core/translate-value.ts","../../src/core/i18n-manager.ts","../../src/vue/index.ts"],"names":[],"mappings":";;;;;AAeO,SAAS,WAAA,CACd,UACA,MAAA,EACQ;AACR,EAAA,IAAI,CAAC,QAAQ,OAAO,QAAA;AAGpB,EAAA,OAAO,QAAA,CAAS,OAAA;AAAA,IACd,0BAAA;AAAA,IACA,CAAC,KAAA,EAAO,cAAA,EAAoC,cAAA,KAAuC;AACjF,MAAA,MAAM,MAAM,cAAA,IAAkB,cAAA;AAE9B,MAAA,IAAI,GAAA,KAAQ,QAAW,OAAO,KAAA;AAE9B,MAAA,MAAM,KAAA,GAAQ,OAAO,GAAG,CAAA;AAExB,MAAA,OAAO,KAAA,KAAU,MAAA,GAAY,MAAA,CAAO,KAAK,CAAA,GAAI,KAAA;AAAA,IAC/C;AAAA,GACF;AACF;;;AChCA,IAAM,eAAA,GAAkB,GAAA,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e,KAAA,CAAM,aAAA;AAAA,QACrB,UAAU,KAAA,CAAM,QAAA;AAAA,QAChB,iBAAiB,KAAA,CAAM;AAAA,OACzB;AAEA,MAAA,OAAA,GAAU,IAAI,YAAY,MAAM,CAAA;AAChC,MAAA,MAAA,CAAO,KAAA,GAAQ,QAAQ,SAAA,EAAU;AAEjC,MAAA,OAAA,CAAQ,SAAA,CAAU,CAAC,KAAA,KAAU;AAC3B,QAAA,MAAA,CAAO,QAAQ,KAAA,CAAM,MAAA;AACrB,QAAA,MAAA,CAAO,QAAQ,KAAA,CAAM,MAAA;AACrB,QAAA,YAAA,CAAa,QAAQ,KAAA,CAAM,YAAA;AAC3B,QAAA,KAAA,CAAM,QAAQ,KAAA,CAAM,KAAA;AAAA,MACtB,CAAC,CAAA;AAED,MAAA,OAAA,CAAQ,UAAA,EAAW;AAAA,IACrB,CAAC,CAAA;AAGD,IAAA,KAAA,CAAM,MAAM,KAAA,CAAM,MAAA,EAAQ,CAAC,SAAA,KAAc;AACvC,MAAA,OAAA,EAAS,YAAA,CAAa,EAAE,MAAA,EAAQ,SAAA,EAAW,CAAA;AAAA,IAC7C,CAAC,CAAA;AAED,IAAA,WAAA,CAAY,MAAM;AAChB,MAAA,OAAA,EAAS,OAAA,EAAQ;AACjB,MAAA,OAAA,GAAU,IAAA;AAAA,IACZ,CAAC,CAAA;AAED,IAAA,OAAO,MAAM,MAAM,OAAA,IAAU;AAAA,EAC/B;AACF,CAAC","file":"index.js","sourcesContent":["/**\n * Interpolate placeholders in a translation string.\n *\n * Supports three formats (checked in order):\n * 1. {{variable}} — double-brace (static translations convention)\n * 2. {variable} — single-brace named (API convention: \"Talk to {name} AI\")\n * 3. {0}, {1} — single-brace positional (API convention: \"Add New {0}\")\n *\n * Positional placeholders use numeric keys: params = { '0': 'User' }\n *\n * Examples:\n * interpolate('Hello, {{name}}!', { name: 'Ali' }) → 'Hello, Ali!'\n * interpolate('Talk to {name} AI', { name: 'Docyrus' }) → 'Talk to Docyrus AI'\n * interpolate('Add New {0}', { '0': 'User' }) → 'Add New User'\n */\nexport function interpolate(\n template: string,\n params?: Record<string, string | number>\n): string {\n if (!params) return template;\n\n // Match both {{key}} and {key} — double-brace first, then single-brace\n return template.replace(\n /\\{\\{(\\w+)\\}\\}|\\{(\\w+)\\}/g,\n (match, doubleBraceKey: string | undefined, singleBraceKey: string | undefined) => {\n const key = doubleBraceKey ?? singleBraceKey;\n\n if (key === undefined) return match;\n\n const value = params[key];\n\n return value !== undefined ? String(value) : match;\n }\n );\n}\n","import { type I18nStorageAdapter } from '../types';\n\nconst DEFAULT_MAX_AGE = 365 * 24 * 60 * 60; // 1 year in seconds\n\n/**\n * Cookie-based storage adapter for web applications.\n * SSR-safe: no-ops on server where `document` is unavailable.\n */\nexport class CookieStorageAdapter implements I18nStorageAdapter {\n constructor(private cookieKey: string = 'docyrus-locale') {}\n getLocale(): string | null {\n if (typeof document === 'undefined') return null;\n\n const match = document.cookie.match(new RegExp(`(?:^|;\\\\s*)${escapeRegex(this.cookieKey)}=([^;]*)`));\n\n return match ? decodeURIComponent(match[1]) : null;\n }\n setLocale(locale: string): void {\n if (typeof document === 'undefined') return;\n\n document.cookie = `${this.cookieKey}=${encodeURIComponent(locale)};path=/;max-age=${DEFAULT_MAX_AGE};SameSite=Lax`;\n }\n}\n\n/**\n * In-memory storage adapter. Useful for SSR or testing.\n */\nexport class MemoryStorageAdapter implements I18nStorageAdapter {\n private locale: string | null = null;\n getLocale(): string | null {\n return this.locale;\n }\n setLocale(locale: string): void {\n this.locale = locale;\n }\n}\n\n// Legacy function exports for backward compatibility\nexport function getLocale(cookieKey: string): string | null {\n return new CookieStorageAdapter(cookieKey).getLocale();\n}\n\nexport function setLocale(cookieKey: string, locale: string): void {\n new CookieStorageAdapter(cookieKey).setLocale(locale);\n}\n\nfunction escapeRegex(str: string): string {\n return str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n","/**\n * Translate API response values that start with \"translate.\" prefix.\n *\n * API responses may contain translatable values like \"translate.Active\",\n * \"translate.Pending\", etc. This function strips the prefix and looks up\n * the translation key.\n *\n * Examples:\n * translateValue('translate.Active', t) → t('common.active') → 'Aktif'\n * translateValue('Regular text', t) → 'Regular text' (unchanged)\n * translateValue('translate.In Progress', t) → t('common.in-progress') → 'Devam Ediyor'\n */\nexport function translateValue(\n value: string,\n t: (key: string, fallbackOrParams?: string | Record<string, string | number>) => string\n): string {\n if (!value.startsWith('translate.')) return value;\n\n const raw = value.slice(10); // Remove \"translate.\" prefix\n const key = `common.${raw.toLowerCase().replace(/\\s+/g, '-')}`;\n\n return t(key) === key ? raw : t(key);\n}\n","import { type RestApiClient } from '@kivacore/api-client';\n\nimport {\n type DocyrusI18nConfig,\n type FallbackStrategy,\n type I18nStatus,\n type I18nStorageAdapter,\n type TranslationDictionary,\n type TranslationsResponse\n} from '../types';\n\nimport { interpolate } from './interpolation';\nimport { CookieStorageAdapter } from './locale-storage';\nimport { translateValue } from './translate-value';\n\nexport type I18nStateListener = (state: {\n status: I18nStatus;\n locale: string | null;\n translations: TranslationDictionary;\n error: Error | null;\n}) => void;\n\n/**\n * Framework-agnostic translation engine.\n *\n * Manages locale persistence (pluggable storage adapter), translation fetching\n * (API + static merge), and provides the `t()` / `tv()` functions for translation.\n *\n * Storage adapters:\n * - Web: CookieStorageAdapter (default)\n * - React Native: provide MMKV/AsyncStorage-backed adapter via `config.storage`\n * - SSR/Testing: MemoryStorageAdapter\n */\nexport class I18nManager {\n private status: I18nStatus = 'loading';\n private locale: string | null;\n private translations: TranslationDictionary = {};\n private apiTranslations: TranslationDictionary = {};\n private error: Error | null = null;\n private listeners: Set<I18nStateListener> = new Set();\n private abortController: AbortController | null = null;\n private client: RestApiClient | null;\n private getAccessToken: (() => Promise<string | null> | string | null) | undefined;\n private apiUrl: string | undefined;\n private translationsEndpoint: string;\n private staticTranslations: TranslationDictionary;\n private storage: I18nStorageAdapter;\n private mergeStrategy: 'api-first' | 'static-first';\n private fallback: FallbackStrategy;\n private disableApiFetch: boolean;\n private userLanguageEndpoint: string | false;\n constructor(config: DocyrusI18nConfig) {\n this.client = config.client ?? null;\n this.getAccessToken = config.getAccessToken;\n this.apiUrl = config.apiUrl;\n this.translationsEndpoint = config.translationsEndpoint ?? 'v1/tenant/translations';\n this.staticTranslations = config.staticTranslations ?? {};\n this.storage = config.storage ?? new CookieStorageAdapter(config.cookieKey ?? 'docyrus-locale');\n this.mergeStrategy = config.mergeStrategy ?? 'api-first';\n this.fallback = config.fallback ?? 'key';\n this.disableApiFetch = config.disableApiFetch ?? false;\n this.userLanguageEndpoint = config.userLanguageEndpoint ?? 'v1/users/me';\n\n // Read locale from storage\n this.locale = this.storage.getLocale();\n }\n getStatus(): I18nStatus {\n return this.status;\n }\n getLocale(): string | null {\n return this.locale;\n }\n getTranslations(): TranslationDictionary {\n return this.translations;\n }\n getError(): Error | null {\n return this.error;\n }\n subscribe(listener: I18nStateListener): () => void {\n this.listeners.add(listener);\n\n return () => this.listeners.delete(listener);\n }\n private notify(): void {\n for (const listener of this.listeners) {\n listener({\n status: this.status,\n locale: this.locale,\n translations: this.translations,\n error: this.error\n });\n }\n }\n /**\n * Initialize the manager.\n * 1. Apply static translations immediately (no loading flash).\n * 2. Fetch from API if enabled and auth is available.\n */\n async initialize(): Promise<void> {\n // Apply static translations immediately\n if (Object.keys(this.staticTranslations).length > 0) {\n this.translations = { ...this.staticTranslations };\n this.status = 'ready';\n this.notify();\n }\n\n // Fetch from API\n if (!this.disableApiFetch && (this.client || this.getAccessToken)) {\n await this.fetchTranslations();\n } else if (this.status !== 'ready') {\n // No static translations and no API fetch — mark ready with empty translations\n this.status = 'ready';\n this.notify();\n }\n }\n /**\n * Fetch translations from the API.\n * Uses RestApiClient.get() if client is available, otherwise raw fetch with getAccessToken.\n */\n async fetchTranslations(): Promise<void> {\n // Abort any in-flight request\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n try {\n let apiData: TranslationsResponse;\n\n if (this.client) {\n apiData = await this.client.get<TranslationsResponse>(\n this.translationsEndpoint\n );\n } else if (this.getAccessToken && this.apiUrl) {\n const token = await this.getAccessToken();\n\n if (!token) {\n throw new Error('getAccessToken returned null — cannot fetch translations');\n }\n\n const url = `${this.apiUrl.replace(/\\/$/, '')}/${this.translationsEndpoint}`;\n const res = await fetch(url, {\n headers: { Authorization: `Bearer ${token}` },\n signal: this.abortController.signal\n });\n\n if (!res.ok) {\n throw new Error(`Translation fetch failed: ${res.status} ${res.statusText}`);\n }\n\n apiData = await res.json() as TranslationsResponse;\n } else {\n // No auth available yet — skip silently\n return;\n }\n\n this.apiTranslations = apiData.data;\n this.mergeTranslations();\n this.error = null;\n this.status = 'ready';\n this.notify();\n } catch (err) {\n if ((err as Error).name === 'AbortError') return;\n\n this.error = err instanceof Error ? err : new Error(String(err));\n\n // If we already have static translations, stay 'ready' with error\n if (Object.keys(this.translations).length > 0) {\n this.notify();\n } else {\n this.status = 'error';\n this.notify();\n }\n }\n }\n /**\n * Translate a key.\n * - `t('key')` — returns translated value or key itself\n * - `t('key', 'Fallback')` — returns translated value or fallback string\n * - `t('key', { name: 'Ali' })` — returns interpolated value with params\n */\n t(key: string, fallbackOrParams?: string | Record<string, string | number>): string {\n const isParams = typeof fallbackOrParams === 'object';\n const params = isParams ? fallbackOrParams : undefined;\n const fallbackStr = typeof fallbackOrParams === 'string' ? fallbackOrParams : undefined;\n\n const value = this.translations[key];\n\n if (value !== undefined) {\n return interpolate(value, params);\n }\n\n // Explicit fallback string takes priority\n if (fallbackStr !== undefined) return fallbackStr;\n\n // Configured fallback strategy\n if (this.fallback === 'key') return interpolate(key, params);\n if (this.fallback === 'empty') return '';\n\n return this.fallback(key);\n }\n /**\n * Translate API response values with \"translate.\" prefix.\n * e.g. tv('translate.Active') → 'Aktif'\n */\n tv(value: string): string {\n return translateValue(value, (key, params) => this.t(key, params));\n }\n /**\n * Change the active locale.\n * Persists to storage, updates API (PATCH users/me), and triggers translation refetch.\n */\n async setLocale(locale: string): Promise<void> {\n if (locale === this.locale) return;\n\n this.locale = locale;\n this.storage.setLocale(locale);\n this.notify();\n\n // Persist language preference to API + refetch translations in parallel\n const promises: Promise<void>[] = [];\n\n if (this.userLanguageEndpoint !== false && (this.client || this.getAccessToken)) {\n promises.push(this.persistUserLanguage(locale));\n }\n\n if (!this.disableApiFetch && (this.client || this.getAccessToken)) {\n promises.push(this.fetchTranslations());\n }\n\n await Promise.all(promises);\n }\n /**\n * Persist user language preference to API.\n * PATCH {userLanguageEndpoint} { language: locale }\n */\n private async persistUserLanguage(locale: string): Promise<void> {\n if (this.userLanguageEndpoint === false) return;\n\n try {\n if (this.client) {\n await this.client.patch(this.userLanguageEndpoint, { language: locale });\n } else if (this.getAccessToken && this.apiUrl) {\n const token = await this.getAccessToken();\n\n if (!token) return;\n\n const url = `${this.apiUrl.replace(/\\/$/, '')}/${this.userLanguageEndpoint}`;\n\n await fetch(url, {\n method: 'PATCH',\n headers: {\n Authorization: `Bearer ${token}`,\n 'Content-Type': 'application/json'\n },\n body: JSON.stringify({ language: locale })\n });\n }\n } catch {\n // Language persist is best-effort — don't block locale switch on failure\n }\n }\n /**\n * Update config at runtime.\n * Useful when client transitions from null → RestApiClient (auth loading complete).\n */\n updateConfig(updates: Partial<Pick<DocyrusI18nConfig, 'client' | 'staticTranslations'>>): void {\n let shouldFetch = false;\n\n if (updates.client !== undefined && updates.client !== this.client) {\n const hadNoClient = !this.client;\n\n this.client = updates.client;\n\n // Client became available — fetch translations\n if (hadNoClient && this.client && !this.disableApiFetch) {\n shouldFetch = true;\n }\n }\n\n if (updates.staticTranslations !== undefined) {\n this.staticTranslations = updates.staticTranslations;\n this.mergeTranslations();\n this.notify();\n }\n\n if (shouldFetch) {\n this.fetchTranslations();\n }\n }\n /** Cleanup: abort in-flight requests + clear listeners. */\n destroy(): void {\n this.abortController?.abort();\n this.listeners.clear();\n }\n private mergeTranslations(): void {\n if (this.mergeStrategy === 'api-first') {\n this.translations = { ...this.staticTranslations, ...this.apiTranslations };\n } else {\n this.translations = { ...this.apiTranslations, ...this.staticTranslations };\n }\n }\n}\n","/* eslint-disable @eslint-react/no-unnecessary-use-prefix */\nimport { type RestApiClient } from '@kivacore/api-client';\n\nimport {\n ref,\n shallowRef,\n computed,\n defineComponent,\n inject,\n provide,\n watch,\n onMounted,\n onUnmounted,\n type InjectionKey,\n type PropType\n} from 'vue';\n\nimport {\n type DocyrusI18nConfig,\n type FallbackStrategy,\n type I18nStatus,\n type TranslationDictionary\n} from '../types';\n\nimport { I18nManager } from '../core/i18n-manager';\n\n// ─── Internal reactive i18n state ────────────────────────────────\n\ninterface DocyrusI18nState {\n status: I18nStatus;\n locale: string | null;\n translations: TranslationDictionary;\n error: Error | null;\n isLoading: boolean;\n t: (key: string, fallbackOrParams?: string | Record<string, string | number>) => string;\n tv: (value: string) => string;\n setLocale: (locale: string) => void;\n refetch: () => Promise<void>;\n}\n\nconst DOCYRUS_I18N_KEY: InjectionKey<DocyrusI18nState> = Symbol('docyrus-i18n');\n\n// ─── Composables ─────────────────────────────────────────────────\n\n/**\n * Vue composable to access full Docyrus i18n state.\n * Must be used within a `<DocyrusI18nProvider>`.\n *\n * Returns: { t, locale, setLocale, status, isLoading, translations, error, refetch }\n */\nexport function useDocyrusI18n(): DocyrusI18nState {\n const context = inject(DOCYRUS_I18N_KEY);\n\n if (!context) {\n throw new Error(\n 'useDocyrusI18n() must be used within a <DocyrusI18nProvider>. '\n + 'Wrap your app with <DocyrusI18nProvider> to provide i18n context.'\n );\n }\n\n return context;\n}\n\n/**\n * Lightweight composable for translation only.\n * Returns just `t` and `isLoading`.\n */\nexport function useTranslation() {\n const context = useDocyrusI18n();\n\n return {\n t: computed(() => context.t),\n isLoading: computed(() => context.isLoading)\n };\n}\n\n// ─── Components ──────────────────────────────────────────────────\n\n/**\n * Vue provider component that wraps your app with Docyrus i18n context.\n *\n * Usage:\n * ```vue\n * <template>\n * <DocyrusI18nProvider :client=\"client\">\n * <App />\n * </DocyrusI18nProvider>\n * </template>\n *\n * <script setup>\n * import { DocyrusI18nProvider } from '@kivacore/i18n/vue';\n * </script>\n * ```\n */\nexport const DocyrusI18nProvider = defineComponent({\n name: 'DocyrusI18nProvider',\n props: {\n client: { type: Object as PropType<RestApiClient | null>, default: null },\n getAccessToken: { type: Function as PropType<() => Promise<string | null> | string | null>, default: undefined },\n apiUrl: { type: String, default: undefined },\n translationsEndpoint: { type: String, default: undefined },\n staticTranslations: { type: Object as PropType<TranslationDictionary>, default: undefined },\n cookieKey: { type: String, default: undefined },\n mergeStrategy: { type: String as PropType<'api-first' | 'static-first'>, default: undefined },\n fallback: { type: [String, Function] as PropType<FallbackStrategy>, default: undefined },\n disableApiFetch: { type: Boolean, default: undefined }\n },\n setup(props, { slots }) {\n const status = ref<I18nStatus>('loading');\n const locale = ref<string | null>(null);\n const translations = shallowRef<TranslationDictionary>({});\n const error = shallowRef<Error | null>(null);\n\n let manager: I18nManager | null = null;\n\n const setLocale = (newLocale: string) => {\n manager?.setLocale(newLocale);\n };\n\n const refetch = async () => {\n await manager?.fetchTranslations();\n };\n\n const t = (key: string, fallbackOrParams?: string | Record<string, string | number>) => {\n return manager?.t(key, fallbackOrParams) ?? (typeof fallbackOrParams === 'string' ? fallbackOrParams : key);\n };\n\n const tv = (value: string) => {\n return manager?.tv(value) ?? value;\n };\n\n const i18nState: DocyrusI18nState = {\n get status() { return status.value; },\n get locale() { return locale.value; },\n get translations() { return translations.value; },\n get error() { return error.value; },\n get isLoading() { return status.value === 'loading'; },\n t,\n tv,\n setLocale,\n refetch\n };\n\n provide(DOCYRUS_I18N_KEY, i18nState);\n\n onMounted(() => {\n const config: DocyrusI18nConfig = {\n client: props.client,\n getAccessToken: props.getAccessToken,\n apiUrl: props.apiUrl,\n translationsEndpoint: props.translationsEndpoint,\n staticTranslations: props.staticTranslations,\n cookieKey: props.cookieKey,\n mergeStrategy: props.mergeStrategy,\n fallback: props.fallback,\n disableApiFetch: props.disableApiFetch\n };\n\n manager = new I18nManager(config);\n locale.value = manager.getLocale();\n\n manager.subscribe((state) => {\n status.value = state.status;\n locale.value = state.locale;\n translations.value = state.translations;\n error.value = state.error;\n });\n\n manager.initialize();\n });\n\n // Watch client prop for null → value transition\n watch(() => props.client, (newClient) => {\n manager?.updateConfig({ client: newClient });\n });\n\n onUnmounted(() => {\n manager?.destroy();\n manager = null;\n });\n\n return () => slots.default?.();\n }\n});\n\n// ─── Type re-exports ─────────────────────────────────────────────\n\nexport type {\n DocyrusI18nState,\n DocyrusI18nConfig,\n I18nStatus,\n TranslationDictionary,\n FallbackStrategy\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,11 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kivacore/i18n",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./core": {
|
|
14
|
+
"import": "./dist/core-index.js",
|
|
15
|
+
"types": "./dist/core-index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./rn": {
|
|
18
|
+
"import": "./dist/rn/index.js",
|
|
19
|
+
"types": "./dist/rn/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./vue": {
|
|
22
|
+
"import": "./dist/vue/index.js",
|
|
23
|
+
"types": "./dist/vue/index.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"author": "Docyrus",
|
|
5
30
|
"license": "MIT",
|
|
6
31
|
"repository": {
|
|
7
32
|
"type": "git",
|
|
8
33
|
"url": "https://github.com/Docyrus/docyrus-devkit.git",
|
|
9
34
|
"directory": "packages/i18n"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@kivacore/api-client": "0.3.7",
|
|
38
|
+
"@types/node": "26.6.6",
|
|
39
|
+
"@types/react": "19.3.0",
|
|
40
|
+
"jsdom": "31.0.1",
|
|
41
|
+
"react": "19.3.1",
|
|
42
|
+
"tsup": "9.0.0",
|
|
43
|
+
"typescript": "7.0.2",
|
|
44
|
+
"vitest": "5.1.0",
|
|
45
|
+
"vue": "3.5.45"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@kivacore/api-client": ">=0.1.0",
|
|
49
|
+
"react": ">=19.0.0",
|
|
50
|
+
"vue": ">=3.5.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"@kivacore/api-client": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"react": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"vue": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup",
|
|
65
|
+
"dev": "tsup --watch",
|
|
66
|
+
"lint": "eslint src",
|
|
67
|
+
"format": "eslint src --fix",
|
|
68
|
+
"typecheck": "eslint src && tsc --noEmit",
|
|
69
|
+
"test": "vitest run --passWithNoTests",
|
|
70
|
+
"test:watch": "vitest",
|
|
71
|
+
"deploy": "pnpm run build && pnpm publish --access public"
|
|
10
72
|
}
|
|
11
|
-
}
|
|
73
|
+
}
|