i18n-keyless-core 2.4.2 → 2.5.0

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,4 @@
1
1
  export { AVAILABLE_LANGS, DEFAULT_NAMESPACE } from "./types.ts";
2
2
  export type { Lang, PrimaryLang, Translations, TranslationsUsage, HandleTranslateFunction, GetAllTranslationsFunction, SendTranslationsUsageFunction, GetAllTranslationsForAllLanguagesFunction, LanguagesConfig, LastRefresh, UniqueId, I18nKeylessRequestBody, I18nKeylessResponse, I18nKeylessTranslationsUsageRequestBody, I18nKeylessAllTranslationsResponse, FetchTranslationParams, TranslationOptions } from "./types.ts";
3
- export { getTranslationCore, getAllTranslationsFromLanguage, sendTranslationsUsageToI18nKeyless, getNamespacesToFetchAfterTranslationFinished, resolveNamespace, queue } from "./service.ts";
3
+ export { getTranslationCore, getAllTranslationsFromLanguage, sendTranslationsUsageToI18nKeyless, getNamespacesToFetchAfterTranslationFinished, resolveNamespace, resolveOriginLanguage, queue } from "./service.ts";
4
4
  export { api } from "./api.ts";
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { AVAILABLE_LANGS, DEFAULT_NAMESPACE } from "./types.js";
2
- export { getTranslationCore, getAllTranslationsFromLanguage, sendTranslationsUsageToI18nKeyless, getNamespacesToFetchAfterTranslationFinished, resolveNamespace, queue } from "./service.js";
2
+ export { getTranslationCore, getAllTranslationsFromLanguage, sendTranslationsUsageToI18nKeyless, getNamespacesToFetchAfterTranslationFinished, resolveNamespace, resolveOriginLanguage, queue } from "./service.js";
3
3
  export { api } from "./api.js";
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "2.4.2",
4
+ "version": "2.5.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
package/dist/service.d.ts CHANGED
@@ -6,6 +6,11 @@ export declare const queue: MyPQueue;
6
6
  * `namespace` wins, then the config-level `defaultNamespace`, then `DEFAULT_NAMESPACE`.
7
7
  */
8
8
  export declare function resolveNamespace(options: TranslationOptions | undefined, config: FetchTranslationParams["config"]): string;
9
+ /**
10
+ * Resolves the effective origin language of a key (UGC flow): the per-call `originLanguage`
11
+ * when it exists and differs from the primary language, undefined otherwise (regular flow).
12
+ */
13
+ export declare function resolveOriginLanguage(options: TranslationOptions | undefined, config: Pick<FetchTranslationParams["config"], "languages">): Lang | undefined;
9
14
  /**
10
15
  * Returns the namespaces queued since the last call (with their `unpersisted` flag) and
11
16
  * clears the map.
package/dist/service.js CHANGED
@@ -10,6 +10,17 @@ export const queue = new MyPQueue({ concurrency: 30 });
10
10
  export function resolveNamespace(options, config) {
11
11
  return options?.namespace || config.defaultNamespace || DEFAULT_NAMESPACE;
12
12
  }
13
+ /**
14
+ * Resolves the effective origin language of a key (UGC flow): the per-call `originLanguage`
15
+ * when it exists and differs from the primary language, undefined otherwise (regular flow).
16
+ */
17
+ export function resolveOriginLanguage(options, config) {
18
+ const originLanguage = options?.originLanguage;
19
+ if (!originLanguage || originLanguage === config.languages.primary) {
20
+ return undefined;
21
+ }
22
+ return originLanguage;
23
+ }
13
24
  /**
14
25
  * Scratchpad of namespaces that had at least one missing key queued for translation since
15
26
  * the last bulk fetch (mapped to whether that namespace is `unpersisted`). The queue's
@@ -45,8 +56,13 @@ export function getTranslationCore(key, store, options) {
45
56
  if (!config.API_KEY) {
46
57
  throw new Error("i18n-keyless: config is not initialized");
47
58
  }
59
+ // The language the key is already written in: the primary language, except for UGC
60
+ // (originLanguage). When the current language is that one, the key renders as-is —
61
+ // notably, a UGC key DOES need a lookup/translation when the current language is the
62
+ // primary one (its primary version is an AI translation, not the key itself).
63
+ const sourceLanguage = resolveOriginLanguage(options, config) ?? config.languages.primary;
48
64
  let translation = key;
49
- if (currentLanguage === config.languages.primary) {
65
+ if (currentLanguage === sourceLanguage) {
50
66
  translation = key;
51
67
  }
52
68
  else {
@@ -135,6 +151,7 @@ export function translateKey(key, store, options) {
135
151
  forceTemporary: options?.forceTemporary,
136
152
  languages: config.languages.supported,
137
153
  primaryLanguage: config.languages.primary,
154
+ originLanguage: resolveOriginLanguage(options, config),
138
155
  };
139
156
  const apiUrl = config.API_URL || "https://api.i18n-keyless.com";
140
157
  const url = `${apiUrl}/translate`;
package/dist/types.d.ts CHANGED
@@ -103,6 +103,14 @@ export type TranslationOptions = {
103
103
  * RegEx is `key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))` so you can use use your own syntax.
104
104
  */
105
105
  replace?: Record<string, string>;
106
+ /**
107
+ * The language the text is written in when it differs from the primary language —
108
+ * i.e. user generated content (UGC). The backend translates it into the primary language,
109
+ * keeps the raw text for viewers in that language, and AI-translates all the others.
110
+ * When the current language IS the origin language, the text is rendered as-is (no API call).
111
+ * Omitted or equal to the primary language means the regular flow.
112
+ */
113
+ originLanguage?: Lang;
106
114
  };
107
115
  export interface I18nKeylessRequestBody {
108
116
  key: string;
@@ -111,6 +119,13 @@ export interface I18nKeylessRequestBody {
111
119
  forceTemporary?: TranslationOptions["forceTemporary"];
112
120
  languages: LanguagesConfig["supported"];
113
121
  primaryLanguage: LanguagesConfig["primary"];
122
+ /**
123
+ * Language the `key` text is written in when it differs from the primary language (UGC flow).
124
+ * The backend translates `key` into the primary language, keys the row by that primary text,
125
+ * stores the raw `key` in the origin-language cell, and AI-translates all other languages.
126
+ * Omitted or equal to `primaryLanguage` means the regular flow.
127
+ */
128
+ originLanguage?: Lang;
114
129
  }
115
130
  export interface I18nKeylessTranslationsUsageRequestBody {
116
131
  primaryLanguage: LanguagesConfig["primary"];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "2.4.2",
4
+ "version": "2.5.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",