i18n-keyless-core 1.10.4 → 1.10.6

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.ts CHANGED
@@ -1,2 +1,2 @@
1
- export type { I18nConfig, Lang, PrimaryLang, Translations, TranslationStore, TranslationStoreState, MinimalTranslationStore, OmitCurrentLanguageTranslationStore, NodeConfig, I18nKeylessRequestBody, I18nKeylessResponse, I18nKeylessAllTranslationsResponse, TranslationOptions, } from "./types";
2
- export { getTranslationCore, getAllTranslationsFromLanguage, getAllTranslationsForAllLanguages, validateLanguage, queue, } from "./service";
1
+ export type { Lang, PrimaryLang, Translations, HandleTranslateFunction, GetAllTranslationsFunction, GetAllTranslationsForAllLanguagesFunction, LanguagesConfig, LastRefresh, UniqueId, I18nKeylessRequestBody, I18nKeylessResponse, I18nKeylessAllTranslationsResponse, FetchTranslationParams, TranslationOptions, } from "./types";
2
+ export { getTranslationCore, getAllTranslationsFromLanguage, queue } from "./service";
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { getTranslationCore, getAllTranslationsFromLanguage, getAllTranslationsForAllLanguages, validateLanguage, queue, } from "./service";
1
+ export { getTranslationCore, getAllTranslationsFromLanguage, queue } from "./service";
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.10.4",
4
+ "version": "1.10.6",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -11,7 +11,8 @@
11
11
  ],
12
12
  "scripts": {
13
13
  "prepublishOnly": "rm -rf ./dist && tsc --project tsconfig.json && npm pack",
14
- "test": "echo 'no test for core'"
14
+ "test": "echo 'no test for core'",
15
+ "postpublish": "rm -rf ./dist && rm *.tgz"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@types/node": "^22.5.5",
package/dist/service.d.ts CHANGED
@@ -1,14 +1,6 @@
1
- import type { I18nConfig, Lang, OmitCurrentLanguageTranslationStore, MinimalTranslationStore, TranslationOptions, I18nKeylessResponse } from "./types";
1
+ import type { Lang, TranslationOptions, I18nKeylessResponse, FetchTranslationParams } from "./types";
2
2
  import MyPQueue from "./my-pqueue";
3
3
  export declare const queue: MyPQueue;
4
- /**
5
- * Validates the language against the supported languages
6
- * @param lang - The language to validate
7
- * @param config - The configuration object
8
- * @returns The validated language or the fallback language if not supported
9
- * @throws Error if config is not initialized
10
- */
11
- export declare function validateLanguage(lang: I18nConfig["languages"]["supported"][number], config: I18nConfig): Lang | undefined;
12
4
  /**
13
5
  * Gets a translation for the specified key from the store
14
6
  * @param key - The translation key (text in primary language)
@@ -17,7 +9,7 @@ export declare function validateLanguage(lang: I18nConfig["languages"]["supporte
17
9
  * @returns The translated text or the original key if not found
18
10
  * @throws Error if config is not initialized
19
11
  */
20
- export declare function getTranslationCore(key: string, store: MinimalTranslationStore, options?: TranslationOptions): string;
12
+ export declare function getTranslationCore(key: string, store: FetchTranslationParams, options?: TranslationOptions): string;
21
13
  /**
22
14
  * Queues a key for translation if not already translated
23
15
  * @param key - The text to translate
@@ -25,18 +17,11 @@ export declare function getTranslationCore(key: string, store: MinimalTranslatio
25
17
  * @param options - Optional parameters for the translation process
26
18
  * @throws Error if config is not initialized
27
19
  */
28
- export declare function translateKey(key: string, store: MinimalTranslationStore, options?: TranslationOptions): void;
29
- /**
30
- * Fetches all translations for a target language
31
- * @param targetLanguage - The language code to fetch translations for
32
- * @param store - The translation store
33
- * @returns Promise resolving to the translation response or void if failed
34
- */
35
- export declare function getAllTranslationsFromLanguage(targetLanguage: Lang, store: MinimalTranslationStore): Promise<I18nKeylessResponse | void>;
20
+ export declare function translateKey(key: string, store: FetchTranslationParams, options?: TranslationOptions): void;
36
21
  /**
37
22
  * Fetches all translations for a target language
38
23
  * @param targetLanguage - The language code to fetch translations for
39
24
  * @param store - The translation store
40
25
  * @returns Promise resolving to the translation response or void if failed
41
26
  */
42
- export declare function getAllTranslationsForAllLanguages(store: OmitCurrentLanguageTranslationStore): Promise<I18nKeylessResponse | void>;
27
+ export declare function getAllTranslationsFromLanguage(targetLanguage: Lang, store: FetchTranslationParams): Promise<I18nKeylessResponse | void>;
package/dist/service.js CHANGED
@@ -2,22 +2,6 @@ import MyPQueue from "./my-pqueue";
2
2
  import packageJson from "./package.json";
3
3
  import { api } from "./api";
4
4
  export const queue = new MyPQueue({ concurrency: 5 });
5
- /**
6
- * Validates the language against the supported languages
7
- * @param lang - The language to validate
8
- * @param config - The configuration object
9
- * @returns The validated language or the fallback language if not supported
10
- * @throws Error if config is not initialized
11
- */
12
- export function validateLanguage(lang, config) {
13
- if (!config) {
14
- throw new Error(`i18n-keyless: config is not initialized validating language`);
15
- }
16
- if (!config.languages.supported.includes(lang)) {
17
- return config.languages.fallback;
18
- }
19
- return lang;
20
- }
21
5
  /**
22
6
  * Gets a translation for the specified key from the store
23
7
  * @param key - The translation key (text in primary language)
@@ -30,7 +14,7 @@ export function getTranslationCore(key, store, options) {
30
14
  const currentLanguage = store.currentLanguage;
31
15
  const config = store.config;
32
16
  const translations = store.translations;
33
- if (!config) {
17
+ if (!config.API_KEY) {
34
18
  throw new Error("i18n-keyless: config is not initialized");
35
19
  }
36
20
  if (currentLanguage === config.languages.primary) {
@@ -59,7 +43,7 @@ export function translateKey(key, store, options) {
59
43
  const config = store.config;
60
44
  const translations = store.translations;
61
45
  const uniqueId = store.uniqueId;
62
- if (!config) {
46
+ if (!config.API_KEY) {
63
47
  throw new Error("i18n-keyless: config is not initialized");
64
48
  }
65
49
  const context = options?.context;
@@ -144,7 +128,7 @@ export async function getAllTranslationsFromLanguage(targetLanguage, store) {
144
128
  const config = store.config;
145
129
  const lastRefresh = store.lastRefresh;
146
130
  const uniqueId = store.uniqueId;
147
- if (!config) {
131
+ if (!config.API_KEY) {
148
132
  console.error("i18n-keyless: No config found");
149
133
  return;
150
134
  }
@@ -177,46 +161,3 @@ export async function getAllTranslationsFromLanguage(targetLanguage, store) {
177
161
  console.error("i18n-keyless: fetch all translations error:", error);
178
162
  }
179
163
  }
180
- /**
181
- * Fetches all translations for a target language
182
- * @param targetLanguage - The language code to fetch translations for
183
- * @param store - The translation store
184
- * @returns Promise resolving to the translation response or void if failed
185
- */
186
- export async function getAllTranslationsForAllLanguages(store) {
187
- const config = store.config;
188
- const lastRefresh = store.lastRefresh;
189
- const uniqueId = store.uniqueId;
190
- if (!config) {
191
- console.error("i18n-keyless: No config found");
192
- return;
193
- }
194
- // if (config.languages.primary === targetLanguage) {
195
- // return;
196
- // }
197
- try {
198
- const response = config.getAllTranslations
199
- ? await config.getAllTranslations()
200
- : await api
201
- .fetchAllTranslationsForAllLanguages(`${config.API_URL || "https://api.i18n-keyless.com"}/translate/?last_refresh=${lastRefresh}`, {
202
- method: "GET",
203
- headers: {
204
- "Content-Type": "application/json",
205
- Authorization: `Bearer ${config.API_KEY}`,
206
- Version: packageJson.version,
207
- unique_id: uniqueId || "",
208
- },
209
- })
210
- .then((res) => res);
211
- if (!response.ok) {
212
- throw new Error(response.error);
213
- }
214
- if (response.message) {
215
- console.warn("i18n-keyless: ", response.message);
216
- }
217
- return response;
218
- }
219
- catch (error) {
220
- console.error("i18n-keyless: fetch all translations error:", error);
221
- }
222
- }
package/dist/types.d.ts CHANGED
@@ -1,169 +1,36 @@
1
1
  export type PrimaryLang = "fr" | "en";
2
2
  export type Lang = "fr" | "en" | "nl" | "it" | "de" | "es" | "pl" | "pt" | "ro" | "sv" | "tr" | "ja" | "cn" | "ru" | "ko" | "ar";
3
3
  export type Translations = Record<string, string>;
4
- type GetStorageFunction = (key: string) => string | null | undefined | Promise<string | null | undefined>;
5
- type SetStorageFunction = (key: string, value: string) => void | Promise<void>;
6
- type RemoveStorageFunction = (key: string) => void | Promise<void>;
7
- type ClearStorageFunction = () => void | Promise<void>;
8
- export interface I18nConfig {
9
- /**
10
- * The API key for the i18n-keyless API
11
- *
12
- * contact @ambroselli_io on X/Twitter or by mail at arnaud.ambroselli.io@gmail.com for getting one
13
- */
14
- API_KEY: string;
15
- /**
16
- * Your own API URL for the i18n-keyless API
17
- *
18
- * You'll need to implement two routes on your server
19
- * - GET /translate/:lang
20
- * - POST /translate -- with a body of { key: string }
21
- */
22
- API_URL?: string;
23
- /**
24
- * The languages config
25
- *
26
- * primary: the language used by the developer
27
- * supported: the languages supported for the user
28
- * fallback: if the user's langauge is not supported, the fallback language will be used
29
- * initWithDefault: the language to use when the app is initialized for the first time
30
- */
31
- languages: {
32
- /**
33
- * the language used by the developer
34
- */
35
- primary: PrimaryLang;
36
- /**
37
- * the languages supported for the user.
38
- * For now we support:
39
- * fr, nl, it, de, es, pl, pt, ro, sv, tr, ja, cn, ru, ko, ar
40
- *
41
- * If you need more, please reach out to @ambroselli_io on X/Twitter or by mail at arnaud.ambroselli.io@gmail.com
42
- */
43
- supported: Lang[];
44
- /**
45
- * if the user's langauge is not supported, the fallback language will be used
46
- */
47
- fallback?: Lang;
48
- /**
49
- * the language to use when the app is initialized
50
- */
51
- initWithDefault?: Lang;
52
- };
53
- /**
54
- * if true, everytime a primary key is not found
55
- * there will be a call to POST /translate -- with a body of { key: string }
56
- * which should handle adding the key to the translations and, if needed,
57
- * translate the key to all the languages supported by the user
58
- *
59
- * Two scenarios
60
- * 1. Enable it in dev mode only: you'll may add some useless key, but you are 100% sure all the translations are up to date
61
- * 2. Enable it in prod mode only: you take a risk taht the translations is not available when required for the first user demanding
62
- * Best take: enable it all the time
63
- */
64
- addMissingTranslations?: boolean;
65
- /**
66
- * called right after the store is initialized, maybe to hide screensplash. or init specific default langauge for dayjs, or whatever
67
- */
68
- onInit?: (lang: Lang) => void;
69
- /**
70
- * if true, all the logs will be displayed in the console
71
- */
72
- debug?: boolean;
73
- /**
74
- * called everytime the language is set, maybe to set also the locale to dayjs or whatever
75
- */
76
- onSetLanguage?: (lang: Lang) => void;
77
- /**
78
- * if this function exists, it will be called instead of the API call
79
- * if this function doesn't exist, the default behavior is to call the API
80
- * therefore you would need either to
81
- * - use this `handleTranslate` function to handle the translation with your own API
82
- * - not use this `handleTranslate` function, and use the built in API call with API_KEY filled
83
- * - not use this `handleTranslate` function nor API_KEY key, and provide your own API_URL
84
- */
85
- handleTranslate?: (key: string) => Promise<{
86
- ok: boolean;
87
- message: string;
88
- }>;
89
- /**
90
- * if this function exists, it will be called instead of the API call
91
- * if this function doesn't exist, the default behavior is to call the API, with the API_KEY
92
- * therefore you need either to
93
- * - use this `getAllTranslations` function to handle the translation with your own API
94
- * - not use this `getAllTranslations` function, and use the built in API call with the API_KEY filled
95
- * - not use this `getAllTranslations` function nor API_KEY key, and provide your own API_URL
96
- */
97
- getAllTranslations?: () => Promise<I18nKeylessResponse>;
4
+ export type HandleTranslateFunction = (key: string) => Promise<{
5
+ ok: boolean;
6
+ message: string;
7
+ }>;
8
+ export type GetAllTranslationsFunction = () => Promise<I18nKeylessResponse>;
9
+ export type GetAllTranslationsForAllLanguagesFunction = () => Promise<I18nKeylessAllTranslationsResponse>;
10
+ export type LastRefresh = string | null;
11
+ export type UniqueId = string | null;
12
+ export type LanguagesConfig = {
98
13
  /**
99
- * if this function exists, it will be called instead of the API call
100
- * if this function doesn't exist, the default behavior is to call the API, with the API_KEY
101
- * therefore you need either to
102
- * - use this `getAllTranslationsForAllLanguages` function to handle the translation with your own API
103
- * - not use this `getAllTranslationsForAllLanguages` function, and use the built in API call with the API_KEY filled
104
- * - not use this `getAllTranslationsForAllLanguages` function nor API_KEY key, and provide your own API_URL
14
+ * the language used by the developer
105
15
  */
106
- getAllTranslationsForAllLanguages?: () => Promise<I18nKeylessAllTranslationsResponse>;
16
+ primary: PrimaryLang;
107
17
  /**
108
- * the storage to use for the translations
18
+ * the languages supported for the user.
19
+ * For now we support:
20
+ * fr, nl, it, de, es, pl, pt, ro, sv, tr, ja, cn, ru, ko, ar
109
21
  *
110
- * you can use react-native-mmkv, @react-native-async-storage/async-storage, or window.localStorage, or idb-keyval for IndexedDB, or any storage that has a getItem, setItem, removeItem, or get, set, and remove method
22
+ * If you need more, please reach out to @ambroselli_io on X/Twitter or by mail at arnaud.ambroselli.io@gmail.com
111
23
  */
112
- storage: {
113
- getItem?: GetStorageFunction;
114
- get?: GetStorageFunction;
115
- getString?: GetStorageFunction;
116
- setItem?: SetStorageFunction;
117
- set?: SetStorageFunction;
118
- removeItem?: RemoveStorageFunction;
119
- remove?: RemoveStorageFunction;
120
- delete?: RemoveStorageFunction;
121
- del?: RemoveStorageFunction;
122
- clear?: ClearStorageFunction;
123
- clearAll?: ClearStorageFunction;
124
- } & {
125
- getString?: GetStorageFunction;
126
- get?: GetStorageFunction;
127
- getItem?: GetStorageFunction;
128
- } & {
129
- setItem?: SetStorageFunction;
130
- set?: SetStorageFunction;
131
- } & {
132
- removeItem?: RemoveStorageFunction;
133
- remove?: RemoveStorageFunction;
134
- delete?: RemoveStorageFunction;
135
- del?: RemoveStorageFunction;
136
- } & ({
137
- clear: ClearStorageFunction;
138
- clearAll?: never;
139
- } | {
140
- clearAll: ClearStorageFunction;
141
- clear?: never;
142
- });
143
- }
144
- export type NodeConfig = Omit<I18nConfig, "storage">;
145
- export interface TranslationStoreState {
24
+ supported: Lang[];
146
25
  /**
147
- * the unique id of the consumer of i18n-keyless API, to help identify the usage API side
26
+ * if the user's langauge is not supported, the fallback language will be used
148
27
  */
149
- uniqueId: string | null;
28
+ fallback?: Lang;
150
29
  /**
151
- * the last refresh of the translations, to only fetch the new ones if any
30
+ * the language to use when the app is initialized
152
31
  */
153
- lastRefresh: string | null;
154
- /**
155
- * the translations fetched from i18n-keyless' API
156
- */
157
- translations: Translations;
158
- /**
159
- * the current language of the user
160
- */
161
- currentLanguage: Lang;
162
- /**
163
- * i18n-keyless' config
164
- */
165
- config: I18nConfig | null;
166
- }
32
+ initWithDefault?: Lang;
33
+ };
167
34
  export type TranslationOptions = {
168
35
  /**
169
36
  * The context of the translation.
@@ -182,28 +49,19 @@ export type TranslationOptions = {
182
49
  */
183
50
  forceTemporary?: Partial<Record<Lang, string>>;
184
51
  };
185
- interface TranslationStoreActions {
186
- setTranslations: (translations: I18nKeylessResponse | void) => void;
187
- setLanguage: (lang: Lang) => void;
188
- }
189
- export type TranslationStore = TranslationStoreState & TranslationStoreActions;
190
- export interface MinimalTranslationStore extends Omit<TranslationStore, "setLanguage" | "storage" | "config"> {
191
- config: NodeConfig | null;
192
- }
193
- export type OmitCurrentLanguageTranslationStore = Omit<MinimalTranslationStore, "currentLanguage">;
194
52
  export interface I18nKeylessRequestBody {
195
53
  key: string;
196
54
  context?: string;
197
55
  forceTemporary?: TranslationOptions["forceTemporary"];
198
- languages: I18nConfig["languages"]["supported"];
199
- primaryLanguage: I18nConfig["languages"]["primary"];
56
+ languages: LanguagesConfig["supported"];
57
+ primaryLanguage: LanguagesConfig["primary"];
200
58
  }
201
59
  export interface I18nKeylessResponse {
202
60
  ok: boolean;
203
61
  data: {
204
62
  translations: Translations;
205
- uniqueId: string | null;
206
- lastRefresh: string | null;
63
+ uniqueId: UniqueId;
64
+ lastRefresh: LastRefresh;
207
65
  };
208
66
  error: string;
209
67
  message: string;
@@ -212,10 +70,25 @@ export interface I18nKeylessAllTranslationsResponse {
212
70
  ok: boolean;
213
71
  data: {
214
72
  translations: Record<Lang, Translations>;
215
- uniqueId: string | null;
216
- lastRefresh: string | null;
73
+ uniqueId: UniqueId;
74
+ lastRefresh: LastRefresh;
217
75
  };
218
76
  error: string;
219
77
  message: string;
220
78
  }
221
- export {};
79
+ export type FetchTranslationParams = {
80
+ uniqueId: UniqueId;
81
+ lastRefresh: LastRefresh;
82
+ currentLanguage: Lang;
83
+ config: {
84
+ API_KEY: string;
85
+ API_URL?: string;
86
+ languages: LanguagesConfig;
87
+ addMissingTranslations?: boolean;
88
+ debug?: boolean;
89
+ handleTranslate?: HandleTranslateFunction;
90
+ getAllTranslations?: GetAllTranslationsFunction;
91
+ getAllTranslationsForAllLanguages?: GetAllTranslationsForAllLanguagesFunction;
92
+ };
93
+ translations: Translations;
94
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.10.4",
4
+ "version": "1.10.6",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -11,7 +11,8 @@
11
11
  ],
12
12
  "scripts": {
13
13
  "prepublishOnly": "rm -rf ./dist && tsc --project tsconfig.json && npm pack",
14
- "test": "echo 'no test for core'"
14
+ "test": "echo 'no test for core'",
15
+ "postpublish": "rm -rf ./dist && rm *.tgz"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@types/node": "^22.5.5",