i18n-keyless-node 1.12.1 → 1.12.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/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { init, getTranslation, getAllTranslationsForAllLanguages } from "./service";
2
- export { awaitForTranslation } from "i18n-keyless-core";
1
+ export { init, getTranslation, getAllTranslationsForAllLanguages, awaitForTranslation } from "./service";
3
2
  export type { Translations, I18nKeylessNodeConfig, I18nKeylessNodeStore, TranslationOptions, I18nKeylessRequestBody, I18nKeylessAllTranslationsResponse, } from "./types";
4
3
  export { type Lang, type PrimaryLang, type I18nKeylessResponse, queue } from "i18n-keyless-core";
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
- export { init, getTranslation, getAllTranslationsForAllLanguages } from "./service";
2
- export { awaitForTranslation } from "i18n-keyless-core";
1
+ export { init, getTranslation, getAllTranslationsForAllLanguages, awaitForTranslation } from "./service";
3
2
  export { queue } from "i18n-keyless-core";
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-node",
3
3
  "private": false,
4
- "version": "1.12.1",
4
+ "version": "1.12.2",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -15,7 +15,7 @@
15
15
  "postpublish": "rm -rf ./dist && rm *.tgz"
16
16
  },
17
17
  "dependencies": {
18
- "i18n-keyless-core": "1.12.1"
18
+ "i18n-keyless-core": "1.12.2"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@eslint/js": "^9.9.0",
package/dist/service.d.ts CHANGED
@@ -8,3 +8,11 @@ import { I18nKeylessNodeConfig, I18nKeylessNodeStore } from "types";
8
8
  export declare function getAllTranslationsForAllLanguages(store: I18nKeylessNodeStore): Promise<I18nKeylessAllTranslationsResponse | void>;
9
9
  export declare function init(newConfig: I18nKeylessNodeConfig): Promise<I18nKeylessNodeConfig>;
10
10
  export declare function getTranslation(key: string, currentLanguage: Lang, options?: TranslationOptions): string;
11
+ /**
12
+ * Queues a key for translation if not already translated
13
+ * @param key - The text to translate
14
+ * @param store - The translation store
15
+ * @param options - Optional parameters for the translation process
16
+ * @throws Error if config is not initialized
17
+ */
18
+ export declare const awaitForTranslation: (key: string, currentLanguage: Lang, options?: TranslationOptions) => Promise<string | null | undefined>;
package/dist/service.js CHANGED
@@ -113,3 +113,93 @@ export function getTranslation(key, currentLanguage, options) {
113
113
  translations: store.translations[currentLanguage],
114
114
  }, options);
115
115
  }
116
+ /**
117
+ * Queues a key for translation if not already translated
118
+ * @param key - The text to translate
119
+ * @param store - The translation store
120
+ * @param options - Optional parameters for the translation process
121
+ * @throws Error if config is not initialized
122
+ */
123
+ export const awaitForTranslation = new Proxy(async function (key, currentLanguage, options) {
124
+ const config = store.config;
125
+ const translations = store.translations;
126
+ const uniqueId = store.uniqueId;
127
+ if (!config.API_KEY) {
128
+ throw new Error("i18n-keyless: config is not initialized");
129
+ }
130
+ const context = options?.context;
131
+ const debug = options?.debug;
132
+ // if (key.length > 280) {
133
+ // console.error("i18n-keyless: Key length exceeds 280 characters limit:", key);
134
+ // return;
135
+ // }
136
+ if (!key) {
137
+ return null;
138
+ }
139
+ if (debug) {
140
+ console.log("translateKey", key, context, debug);
141
+ }
142
+ const forceTemporaryLang = options?.forceTemporary?.[currentLanguage];
143
+ const translation = context
144
+ ? translations[currentLanguage][`${key}__${context}`]
145
+ : translations[currentLanguage][key];
146
+ if (translation && !forceTemporaryLang) {
147
+ if (debug) {
148
+ console.log("translation exists", `${key}__${context}`);
149
+ }
150
+ return translation;
151
+ }
152
+ try {
153
+ if (config.handleTranslate) {
154
+ await config.handleTranslate?.(key);
155
+ }
156
+ else {
157
+ const body = {
158
+ key,
159
+ context,
160
+ forceTemporary: options?.forceTemporary,
161
+ languages: config.languages.supported,
162
+ primaryLanguage: config.languages.primary,
163
+ };
164
+ const apiUrl = config.API_URL || "https://api.i18n-keyless.com";
165
+ const url = `${apiUrl}/translate?return_translation=true`;
166
+ if (debug) {
167
+ console.log("fetching translation", url, body);
168
+ }
169
+ const response = await api
170
+ .fetchTranslation(url, {
171
+ method: "POST",
172
+ headers: {
173
+ "Content-Type": "application/json",
174
+ Authorization: `Bearer ${config.API_KEY}`,
175
+ unique_id: uniqueId || "",
176
+ Version: packageJson.version,
177
+ },
178
+ body: JSON.stringify(body),
179
+ })
180
+ .then((res) => res);
181
+ if (debug) {
182
+ console.log("response", response);
183
+ }
184
+ if (response.message) {
185
+ console.warn("i18n-keyless: ", response.message);
186
+ }
187
+ return response.data;
188
+ }
189
+ return null;
190
+ }
191
+ catch (error) {
192
+ console.error("i18n-keyless: Error await translating key:", error);
193
+ }
194
+ }, {
195
+ apply(target, thisArg, args) {
196
+ const result = Reflect.apply(target, thisArg, args);
197
+ if (result instanceof Promise) {
198
+ result.catch((error) => {
199
+ console.error("awaitForTranslation was not properly awaited:", error);
200
+ throw error;
201
+ });
202
+ }
203
+ return result;
204
+ },
205
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-node",
3
3
  "private": false,
4
- "version": "1.12.1",
4
+ "version": "1.12.2",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -15,7 +15,7 @@
15
15
  "postpublish": "rm -rf ./dist && rm *.tgz"
16
16
  },
17
17
  "dependencies": {
18
- "i18n-keyless-core": "1.12.1"
18
+ "i18n-keyless-core": "1.12.2"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@eslint/js": "^9.9.0",