@terai/translator 0.0.12 → 0.0.13

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.cjs ADDED
@@ -0,0 +1,2 @@
1
+ async function e({dictionary:e,projectLocale:t,locale:n,translator:r}){return r({dictionary:e,projectLocale:t,locale:n})}exports.translate=e;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":[],"sources":["../src/translate.ts"],"sourcesContent":["// Types\nimport type { Config, Locale, Dictionary } from '@terai/types'\n\nexport type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {\n\tdictionary: Dictionary\n\tlocale: Locale\n}\n\nexport async function translate({\n\tdictionary,\n\tprojectLocale,\n\tlocale,\n\ttranslator\n}: TranslateOptions): Promise<Dictionary> {\n\treturn translator({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t})\n}\n"],"mappings":"AAQA,eAAsB,EAAU,CAC/B,aACA,gBACA,SACA,cACyC,CACzC,OAAO,EAAW,CACjB,aACA,gBACA,SACA,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { Config, Dictionary, Locale } from "@terai/types";
2
+
3
+ //#region src/translate.d.ts
4
+ type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {
5
+ dictionary: Dictionary;
6
+ locale: Locale;
7
+ };
8
+ declare function translate({
9
+ dictionary,
10
+ projectLocale,
11
+ locale,
12
+ translator
13
+ }: TranslateOptions): Promise<Dictionary>;
14
+ //#endregion
15
+ export { TranslateOptions, translate };
16
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/translate.ts"],"sourcesContent":[],"mappings":";;;KAGY,gBAAA,GAAmB,KAAK;cACvB;EADD,MAAA,EAEH,MAFG;CAAwB;AAAL,iBAKT,SAAA,CALS;EAAA,UAAA;EAAA,aAAA;EAAA,MAAA;EAAA;AAAA,CAAA,EAU5B,gBAV4B,CAAA,EAUT,OAVS,CAUD,UAVC,CAAA"}
package/dist/index.d.mts CHANGED
@@ -1,9 +1,16 @@
1
- import { Config, Dictionary, Locale } from '@terai/types';
1
+ import { Config, Dictionary, Locale } from "@terai/types";
2
2
 
3
+ //#region src/translate.d.ts
3
4
  type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {
4
- dictionary: Dictionary;
5
- locale: Locale;
5
+ dictionary: Dictionary;
6
+ locale: Locale;
6
7
  };
7
- declare function translate({ dictionary, projectLocale, locale, translator }: TranslateOptions): Promise<Dictionary>;
8
-
9
- export { type TranslateOptions, translate };
8
+ declare function translate({
9
+ dictionary,
10
+ projectLocale,
11
+ locale,
12
+ translator
13
+ }: TranslateOptions): Promise<Dictionary>;
14
+ //#endregion
15
+ export { TranslateOptions, translate };
16
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/translate.ts"],"sourcesContent":[],"mappings":";;;KAGY,gBAAA,GAAmB,KAAK;cACvB;EADD,MAAA,EAEH,MAFG;CAAwB;AAAL,iBAKT,SAAA,CALS;EAAA,UAAA;EAAA,aAAA;EAAA,MAAA;EAAA;AAAA,CAAA,EAU5B,gBAV4B,CAAA,EAUT,OAVS,CAUD,UAVC,CAAA"}
package/dist/index.mjs CHANGED
@@ -1,17 +1,2 @@
1
- // src/translate.ts
2
- async function translate({
3
- dictionary,
4
- projectLocale,
5
- locale,
6
- translator
7
- }) {
8
- return translator({
9
- dictionary,
10
- projectLocale,
11
- locale
12
- });
13
- }
14
- export {
15
- translate
16
- };
1
+ async function e({dictionary:e,projectLocale:t,locale:n,translator:r}){return r({dictionary:e,projectLocale:t,locale:n})}export{e as translate};
17
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/translate.ts"],"sourcesContent":["// Types\nimport type { Config, Locale, Dictionary } from '@terai/types'\n\nexport type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {\n\tdictionary: Dictionary\n\tlocale: Locale\n}\n\nexport async function translate({\n\tdictionary,\n\tprojectLocale,\n\tlocale,\n\ttranslator\n}: TranslateOptions): Promise<Dictionary> {\n\treturn translator({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t})\n}\n"],"mappings":";AAQA,eAAsB,UAAU;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAA0C;AACzC,SAAO,WAAW;AAAA,IACjB;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAC;AACF;","names":[]}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/translate.ts"],"sourcesContent":["// Types\nimport type { Config, Locale, Dictionary } from '@terai/types'\n\nexport type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {\n\tdictionary: Dictionary\n\tlocale: Locale\n}\n\nexport async function translate({\n\tdictionary,\n\tprojectLocale,\n\tlocale,\n\ttranslator\n}: TranslateOptions): Promise<Dictionary> {\n\treturn translator({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t})\n}\n"],"mappings":"AAQA,eAAsB,EAAU,CAC/B,aACA,gBACA,SACA,cACyC,CACzC,OAAO,EAAW,CACjB,aACA,gBACA,SACA,CAAC"}
@@ -0,0 +1,3 @@
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`openai`);c=s(c);let l=require(`@google-cloud/translate`);const u=e=>{let t=new c.default(e);return async({dictionary:e,projectLocale:n,locale:r})=>{let i=JSON.stringify(e),a=await t.chat.completions.create({model:`gpt-3.5-turbo-0125`,messages:[{role:`system`,content:[`You will be provided with a JSON in locale: ${n}, and your task is to translate it to locale: ${r}.`,'Do not translate any escaped characters or "${var}" ocurrences, just keep them as they are'].join(`.
2
+ `)},{role:`user`,content:i}],temperature:.4,frequency_penalty:-2,presence_penalty:-2});return JSON.parse(a?.choices?.[0]?.message?.content||`{}`)}},d=e=>{let t=new l.v2.Translate(e);return async({dictionary:e,locale:n})=>{let r={};return await Promise.all(Object.keys(e).map(async i=>{let a=e[i],[o]=await t.translate(a,n);r[i]=o})),r}};exports.createChatGptTranslator=u,exports.createGoogleCloudTranslator=d;
3
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["openai","OpenAI","translator: Translator","v2","translator: Translator","translatedOutput: Dictionary"],"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"sourcesContent":["// Dependencies\nimport OpenAI, { type ClientOptions } from 'openai'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createChatGptTranslator = (openaiOptions: ClientOptions) => {\n\tconst openai = new OpenAI(openaiOptions)\n\n\tconst translator: Translator = async ({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t}) => {\n\t\tconst messagesJson = JSON.stringify(dictionary)\n\n\t\tconst response = await openai.chat.completions.create({\n\t\t\tmodel: 'gpt-3.5-turbo-0125',\n\t\t\tmessages: [\n\t\t\t\t{\n\t\t\t\t\trole: 'system',\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t// 'Act like a professional translator',\n\t\t\t\t\t\t`You will be provided with a JSON in locale: ${projectLocale}, and your task is to translate it to locale: ${locale}.`,\n\t\t\t\t\t\t// 'Keep the same structure and keys, just translate the values.',\n\t\t\t\t\t\t'Do not translate any escaped characters or \"${var}\" ocurrences, just keep them as they are'\n\t\t\t\t\t].join('.\\n')\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\trole: 'user',\n\t\t\t\t\tcontent: messagesJson\n\t\t\t\t}\n\t\t\t],\n\t\t\ttemperature: 0.4,\n\t\t\tfrequency_penalty: -2.0,\n\t\t\tpresence_penalty: -2.0\n\t\t})\n\n\t\treturn JSON.parse(\n\t\t\tresponse?.choices?.[0]?.message?.content || '{}'\n\t\t) as Dictionary\n\t}\n\n\treturn translator\n}\n","// Dependencies\nimport { v2 } from '@google-cloud/translate'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createGoogleCloudTranslator = (options: v2.TranslateConfig) => {\n\tconst translate = new v2.Translate(options)\n\n\tconst translator: Translator = async ({ dictionary, locale }) => {\n\t\tconst translatedOutput: Dictionary = {}\n\n\t\tawait Promise.all(\n\t\t\tObject.keys(dictionary).map(async (key) => {\n\t\t\t\tconst value = dictionary[key]\n\n\t\t\t\tconst [translation] = await translate.translate(value, locale)\n\n\t\t\t\ttranslatedOutput[key] = translation\n\t\t\t})\n\t\t)\n\n\t\treturn translatedOutput\n\t}\n\n\treturn translator\n}\n"],"mappings":"siBAMA,MAAa,EAA2B,GAAiC,CACxE,IAAMA,EAAS,IAAIC,EAAAA,QAAO,EAAc,CAoCxC,OAlC+B,MAAO,CACrC,aACA,gBACA,YACK,CACL,IAAM,EAAe,KAAK,UAAU,EAAW,CAEzC,EAAW,MAAMD,EAAO,KAAK,YAAY,OAAO,CACrD,MAAO,qBACP,SAAU,CACT,CACC,KAAM,SACN,QAAS,CAER,+CAA+C,EAAc,gDAAgD,EAAO,GAEpH,6FACA,CAAC,KAAK;EAAM,CACb,CACD,CACC,KAAM,OACN,QAAS,EACT,CACD,CACD,YAAa,GACb,kBAAmB,GACnB,iBAAkB,GAClB,CAAC,CAEF,OAAO,KAAK,MACX,GAAU,UAAU,IAAI,SAAS,SAAW,KAC5C,GClCU,EAA+B,GAAgC,CAC3E,IAAM,EAAY,IAAIG,EAAAA,GAAG,UAAU,EAAQ,CAkB3C,OAhB+B,MAAO,CAAE,aAAY,YAAa,CAChE,IAAME,EAA+B,EAAE,CAYvC,OAVA,MAAM,QAAQ,IACb,OAAO,KAAK,EAAW,CAAC,IAAI,KAAO,IAAQ,CAC1C,IAAM,EAAQ,EAAW,GAEnB,CAAC,GAAe,MAAM,EAAU,UAAU,EAAO,EAAO,CAE9D,EAAiB,GAAO,GACvB,CACF,CAEM"}
@@ -0,0 +1,12 @@
1
+ import { Translator } from "@terai/types";
2
+ import { ClientOptions } from "openai";
3
+ import { v2 } from "@google-cloud/translate";
4
+
5
+ //#region src/translators/chat-gpt.d.ts
6
+ declare const createChatGptTranslator: (openaiOptions: ClientOptions) => Translator;
7
+ //#endregion
8
+ //#region src/translators/google-cloud.d.ts
9
+ declare const createGoogleCloudTranslator: (options: v2.TranslateConfig) => Translator;
10
+ //#endregion
11
+ export { createChatGptTranslator, createGoogleCloudTranslator };
12
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"sourcesContent":[],"mappings":";;;;;cAMa,yCAA0C,kBAAa;;;cCAvD,uCAAwC,EAAA,CAAG,oBAAe"}
@@ -1,9 +1,12 @@
1
- import { ClientOptions } from 'openai';
2
- import { Translator } from '@terai/types';
3
- import { v2 } from '@google-cloud/translate';
1
+ import { ClientOptions } from "openai";
2
+ import { v2 } from "@google-cloud/translate";
3
+ import { Translator } from "@terai/types";
4
4
 
5
+ //#region src/translators/chat-gpt.d.ts
5
6
  declare const createChatGptTranslator: (openaiOptions: ClientOptions) => Translator;
6
-
7
+ //#endregion
8
+ //#region src/translators/google-cloud.d.ts
7
9
  declare const createGoogleCloudTranslator: (options: v2.TranslateConfig) => Translator;
8
-
10
+ //#endregion
9
11
  export { createChatGptTranslator, createGoogleCloudTranslator };
12
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"sourcesContent":[],"mappings":";;;;;cAMa,yCAA0C,kBAAa;;;cCAvD,uCAAwC,EAAA,CAAG,oBAAe"}
@@ -1,60 +1,3 @@
1
- // src/translators/chat-gpt.ts
2
- import OpenAI from "openai";
3
- var createChatGptTranslator = (openaiOptions) => {
4
- const openai = new OpenAI(openaiOptions);
5
- const translator = async ({
6
- dictionary,
7
- projectLocale,
8
- locale
9
- }) => {
10
- const messagesJson = JSON.stringify(dictionary);
11
- const response = await openai.chat.completions.create({
12
- model: "gpt-3.5-turbo-0125",
13
- messages: [
14
- {
15
- role: "system",
16
- content: [
17
- // 'Act like a professional translator',
18
- `You will be provided with a JSON in locale: ${projectLocale}, and your task is to translate it to locale: ${locale}.`,
19
- // 'Keep the same structure and keys, just translate the values.',
20
- 'Do not translate any escaped characters or "${var}" ocurrences, just keep them as they are'
21
- ].join(".\n")
22
- },
23
- {
24
- role: "user",
25
- content: messagesJson
26
- }
27
- ],
28
- temperature: 0.4,
29
- frequency_penalty: -2,
30
- presence_penalty: -2
31
- });
32
- return JSON.parse(
33
- response?.choices?.[0]?.message?.content || "{}"
34
- );
35
- };
36
- return translator;
37
- };
38
-
39
- // src/translators/google-cloud.ts
40
- import { v2 } from "@google-cloud/translate";
41
- var createGoogleCloudTranslator = (options) => {
42
- const translate = new v2.Translate(options);
43
- const translator = async ({ dictionary, locale }) => {
44
- const translatedOutput = {};
45
- await Promise.all(
46
- Object.keys(dictionary).map(async (key) => {
47
- const value = dictionary[key];
48
- const [translation] = await translate.translate(value, locale);
49
- translatedOutput[key] = translation;
50
- })
51
- );
52
- return translatedOutput;
53
- };
54
- return translator;
55
- };
56
- export {
57
- createChatGptTranslator,
58
- createGoogleCloudTranslator
59
- };
1
+ import e from"openai";import{v2 as t}from"@google-cloud/translate";const n=t=>{let n=new e(t);return async({dictionary:e,projectLocale:t,locale:r})=>{let i=JSON.stringify(e),a=await n.chat.completions.create({model:`gpt-3.5-turbo-0125`,messages:[{role:`system`,content:[`You will be provided with a JSON in locale: ${t}, and your task is to translate it to locale: ${r}.`,'Do not translate any escaped characters or "${var}" ocurrences, just keep them as they are'].join(`.
2
+ `)},{role:`user`,content:i}],temperature:.4,frequency_penalty:-2,presence_penalty:-2});return JSON.parse(a?.choices?.[0]?.message?.content||`{}`)}},r=e=>{let n=new t.Translate(e);return async({dictionary:e,locale:t})=>{let r={};return await Promise.all(Object.keys(e).map(async i=>{let a=e[i],[o]=await n.translate(a,t);r[i]=o})),r}};export{n as createChatGptTranslator,r as createGoogleCloudTranslator};
60
3
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"sourcesContent":["// Dependencies\nimport OpenAI, { type ClientOptions } from 'openai'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createChatGptTranslator = (openaiOptions: ClientOptions) => {\n\tconst openai = new OpenAI(openaiOptions)\n\n\tconst translator: Translator = async ({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t}) => {\n\t\tconst messagesJson = JSON.stringify(dictionary)\n\n\t\tconst response = await openai.chat.completions.create({\n\t\t\tmodel: 'gpt-3.5-turbo-0125',\n\t\t\tmessages: [\n\t\t\t\t{\n\t\t\t\t\trole: 'system',\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t// 'Act like a professional translator',\n\t\t\t\t\t\t`You will be provided with a JSON in locale: ${projectLocale}, and your task is to translate it to locale: ${locale}.`,\n\t\t\t\t\t\t// 'Keep the same structure and keys, just translate the values.',\n\t\t\t\t\t\t'Do not translate any escaped characters or \"${var}\" ocurrences, just keep them as they are'\n\t\t\t\t\t].join('.\\n')\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\trole: 'user',\n\t\t\t\t\tcontent: messagesJson\n\t\t\t\t}\n\t\t\t],\n\t\t\ttemperature: 0.4,\n\t\t\tfrequency_penalty: -2.0,\n\t\t\tpresence_penalty: -2.0\n\t\t})\n\n\t\treturn JSON.parse(\n\t\t\tresponse?.choices?.[0]?.message?.content || '{}'\n\t\t) as Dictionary\n\t}\n\n\treturn translator\n}\n","// Dependencies\nimport { v2 } from '@google-cloud/translate'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createGoogleCloudTranslator = (options: v2.TranslateConfig) => {\n\tconst translate = new v2.Translate(options)\n\n\tconst translator: Translator = async ({ dictionary, locale }) => {\n\t\tconst translatedOutput: Dictionary = {}\n\n\t\tawait Promise.all(\n\t\t\tObject.keys(dictionary).map(async (key) => {\n\t\t\t\tconst value = dictionary[key]\n\n\t\t\t\tconst [translation] = await translate.translate(value, locale)\n\n\t\t\t\ttranslatedOutput[key] = translation\n\t\t\t})\n\t\t)\n\n\t\treturn translatedOutput\n\t}\n\n\treturn translator\n}\n"],"mappings":";AACA,OAAO,YAAoC;AAKpC,IAAM,0BAA0B,CAAC,kBAAiC;AACxE,QAAM,SAAS,IAAI,OAAO,aAAa;AAEvC,QAAM,aAAyB,OAAO;AAAA,IACrC;AAAA,IACA;AAAA,IACA;AAAA,EACD,MAAM;AACL,UAAM,eAAe,KAAK,UAAU,UAAU;AAE9C,UAAM,WAAW,MAAM,OAAO,KAAK,YAAY,OAAO;AAAA,MACrD,OAAO;AAAA,MACP,UAAU;AAAA,QACT;AAAA,UACC,MAAM;AAAA,UACN,SAAS;AAAA;AAAA,YAER,+CAA+C,aAAa,iDAAiD,MAAM;AAAA;AAAA,YAEnH;AAAA,UACD,EAAE,KAAK,KAAK;AAAA,QACb;AAAA,QACA;AAAA,UACC,MAAM;AAAA,UACN,SAAS;AAAA,QACV;AAAA,MACD;AAAA,MACA,aAAa;AAAA,MACb,mBAAmB;AAAA,MACnB,kBAAkB;AAAA,IACnB,CAAC;AAED,WAAO,KAAK;AAAA,MACX,UAAU,UAAU,CAAC,GAAG,SAAS,WAAW;AAAA,IAC7C;AAAA,EACD;AAEA,SAAO;AACR;;;AC3CA,SAAS,UAAU;AAKZ,IAAM,8BAA8B,CAAC,YAAgC;AAC3E,QAAM,YAAY,IAAI,GAAG,UAAU,OAAO;AAE1C,QAAM,aAAyB,OAAO,EAAE,YAAY,OAAO,MAAM;AAChE,UAAM,mBAA+B,CAAC;AAEtC,UAAM,QAAQ;AAAA,MACb,OAAO,KAAK,UAAU,EAAE,IAAI,OAAO,QAAQ;AAC1C,cAAM,QAAQ,WAAW,GAAG;AAE5B,cAAM,CAAC,WAAW,IAAI,MAAM,UAAU,UAAU,OAAO,MAAM;AAE7D,yBAAiB,GAAG,IAAI;AAAA,MACzB,CAAC;AAAA,IACF;AAEA,WAAO;AAAA,EACR;AAEA,SAAO;AACR;","names":[]}
1
+ {"version":3,"file":"index.mjs","names":["translator: Translator","translator: Translator","translatedOutput: Dictionary"],"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"sourcesContent":["// Dependencies\nimport OpenAI, { type ClientOptions } from 'openai'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createChatGptTranslator = (openaiOptions: ClientOptions) => {\n\tconst openai = new OpenAI(openaiOptions)\n\n\tconst translator: Translator = async ({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t}) => {\n\t\tconst messagesJson = JSON.stringify(dictionary)\n\n\t\tconst response = await openai.chat.completions.create({\n\t\t\tmodel: 'gpt-3.5-turbo-0125',\n\t\t\tmessages: [\n\t\t\t\t{\n\t\t\t\t\trole: 'system',\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t// 'Act like a professional translator',\n\t\t\t\t\t\t`You will be provided with a JSON in locale: ${projectLocale}, and your task is to translate it to locale: ${locale}.`,\n\t\t\t\t\t\t// 'Keep the same structure and keys, just translate the values.',\n\t\t\t\t\t\t'Do not translate any escaped characters or \"${var}\" ocurrences, just keep them as they are'\n\t\t\t\t\t].join('.\\n')\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\trole: 'user',\n\t\t\t\t\tcontent: messagesJson\n\t\t\t\t}\n\t\t\t],\n\t\t\ttemperature: 0.4,\n\t\t\tfrequency_penalty: -2.0,\n\t\t\tpresence_penalty: -2.0\n\t\t})\n\n\t\treturn JSON.parse(\n\t\t\tresponse?.choices?.[0]?.message?.content || '{}'\n\t\t) as Dictionary\n\t}\n\n\treturn translator\n}\n","// Dependencies\nimport { v2 } from '@google-cloud/translate'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createGoogleCloudTranslator = (options: v2.TranslateConfig) => {\n\tconst translate = new v2.Translate(options)\n\n\tconst translator: Translator = async ({ dictionary, locale }) => {\n\t\tconst translatedOutput: Dictionary = {}\n\n\t\tawait Promise.all(\n\t\t\tObject.keys(dictionary).map(async (key) => {\n\t\t\t\tconst value = dictionary[key]\n\n\t\t\t\tconst [translation] = await translate.translate(value, locale)\n\n\t\t\t\ttranslatedOutput[key] = translation\n\t\t\t})\n\t\t)\n\n\t\treturn translatedOutput\n\t}\n\n\treturn translator\n}\n"],"mappings":"mEAMA,MAAa,EAA2B,GAAiC,CACxE,IAAM,EAAS,IAAI,EAAO,EAAc,CAoCxC,OAlC+B,MAAO,CACrC,aACA,gBACA,YACK,CACL,IAAM,EAAe,KAAK,UAAU,EAAW,CAEzC,EAAW,MAAM,EAAO,KAAK,YAAY,OAAO,CACrD,MAAO,qBACP,SAAU,CACT,CACC,KAAM,SACN,QAAS,CAER,+CAA+C,EAAc,gDAAgD,EAAO,GAEpH,6FACA,CAAC,KAAK;EAAM,CACb,CACD,CACC,KAAM,OACN,QAAS,EACT,CACD,CACD,YAAa,GACb,kBAAmB,GACnB,iBAAkB,GAClB,CAAC,CAEF,OAAO,KAAK,MACX,GAAU,UAAU,IAAI,SAAS,SAAW,KAC5C,GClCU,EAA+B,GAAgC,CAC3E,IAAM,EAAY,IAAI,EAAG,UAAU,EAAQ,CAkB3C,OAhB+B,MAAO,CAAE,aAAY,YAAa,CAChE,IAAME,EAA+B,EAAE,CAYvC,OAVA,MAAM,QAAQ,IACb,OAAO,KAAK,EAAW,CAAC,IAAI,KAAO,IAAQ,CAC1C,IAAM,EAAQ,EAAW,GAEnB,CAAC,GAAe,MAAM,EAAU,UAAU,EAAO,EAAO,CAE9D,EAAiB,GAAO,GACvB,CACF,CAEM"}
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@terai/translator",
3
3
  "description": "terai core translator",
4
- "version": "0.0.12",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
4
+ "version": "0.0.13",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
8
  "author": "Hugo Corta <corta.hugo@gmail.com>",
9
9
  "files": [
10
10
  "dist"
@@ -15,14 +15,24 @@
15
15
  },
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.d.ts",
19
- "require": "./dist/index.js",
20
- "import": "./dist/index.mjs"
18
+ "import": {
19
+ "types": "./dist/index.d.mts",
20
+ "default": "./dist/index.mjs"
21
+ },
22
+ "require": {
23
+ "types": "./dist/index.d.cts",
24
+ "default": "./dist/index.cjs"
25
+ }
21
26
  },
22
27
  "./translators": {
23
- "types": "./dist/translators/index.d.ts",
24
- "require": "./dist/translators/index.js",
25
- "import": "./dist/translators/index.mjs"
28
+ "import": {
29
+ "types": "./dist/translators/index.d.mts",
30
+ "default": "./dist/translators/index.mjs"
31
+ },
32
+ "require": {
33
+ "types": "./dist/translators/index.d.cts",
34
+ "default": "./dist/translators/index.cjs"
35
+ }
26
36
  }
27
37
  },
28
38
  "dependencies": {
@@ -30,12 +40,12 @@
30
40
  "openai": "^4"
31
41
  },
32
42
  "devDependencies": {
33
- "@terai/runtime": "0.0.12",
34
- "@terai/types": "0.0.12",
35
- "@terai/utils": "0.0.12"
43
+ "@terai/runtime": "0.0.13",
44
+ "@terai/types": "0.0.13",
45
+ "@terai/utils": "0.0.13"
36
46
  },
37
47
  "scripts": {
38
- "dev": "tsup --format=esm,cjs --no-dts --watch",
39
- "build": "tsup"
48
+ "dev": "tsdown --format=esm --no-dts --watch",
49
+ "build": "tsdown"
40
50
  }
41
51
  }
package/dist/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import { Config, Dictionary, Locale } from '@terai/types';
2
-
3
- type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {
4
- dictionary: Dictionary;
5
- locale: Locale;
6
- };
7
- declare function translate({ dictionary, projectLocale, locale, translator }: TranslateOptions): Promise<Dictionary>;
8
-
9
- export { type TranslateOptions, translate };
package/dist/index.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/translate.ts
2
- async function translate({
3
- dictionary,
4
- projectLocale,
5
- locale,
6
- translator
7
- }) {
8
- return translator({
9
- dictionary,
10
- projectLocale,
11
- locale
12
- });
13
- }
14
-
15
-
16
- exports.translate = translate;
17
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/home/runner/work/terai/terai/packages/translator/dist/index.js","../src/translate.ts"],"names":[],"mappings":"AAAA;ACQA,MAAA,SAAsB,SAAA,CAAU;AAAA,EAC/B,UAAA;AAAA,EACA,aAAA;AAAA,EACA,MAAA;AAAA,EACA;AACD,CAAA,EAA0C;AACzC,EAAA,OAAO,UAAA,CAAW;AAAA,IACjB,UAAA;AAAA,IACA,aAAA;AAAA,IACA;AAAA,EACD,CAAC,CAAA;AACF;ADNA;AACE;AACF,8BAAC","file":"/home/runner/work/terai/terai/packages/translator/dist/index.js","sourcesContent":[null,"// Types\nimport type { Config, Locale, Dictionary } from '@terai/types'\n\nexport type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {\n\tdictionary: Dictionary\n\tlocale: Locale\n}\n\nexport async function translate({\n\tdictionary,\n\tprojectLocale,\n\tlocale,\n\ttranslator\n}: TranslateOptions): Promise<Dictionary> {\n\treturn translator({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t})\n}\n"]}
@@ -1,9 +0,0 @@
1
- import { ClientOptions } from 'openai';
2
- import { Translator } from '@terai/types';
3
- import { v2 } from '@google-cloud/translate';
4
-
5
- declare const createChatGptTranslator: (openaiOptions: ClientOptions) => Translator;
6
-
7
- declare const createGoogleCloudTranslator: (options: v2.TranslateConfig) => Translator;
8
-
9
- export { createChatGptTranslator, createGoogleCloudTranslator };
@@ -1,60 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/translators/chat-gpt.ts
2
- var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);
3
- var createChatGptTranslator = (openaiOptions) => {
4
- const openai = new (0, _openai2.default)(openaiOptions);
5
- const translator = async ({
6
- dictionary,
7
- projectLocale,
8
- locale
9
- }) => {
10
- const messagesJson = JSON.stringify(dictionary);
11
- const response = await openai.chat.completions.create({
12
- model: "gpt-3.5-turbo-0125",
13
- messages: [
14
- {
15
- role: "system",
16
- content: [
17
- // 'Act like a professional translator',
18
- `You will be provided with a JSON in locale: ${projectLocale}, and your task is to translate it to locale: ${locale}.`,
19
- // 'Keep the same structure and keys, just translate the values.',
20
- 'Do not translate any escaped characters or "${var}" ocurrences, just keep them as they are'
21
- ].join(".\n")
22
- },
23
- {
24
- role: "user",
25
- content: messagesJson
26
- }
27
- ],
28
- temperature: 0.4,
29
- frequency_penalty: -2,
30
- presence_penalty: -2
31
- });
32
- return JSON.parse(
33
- _optionalChain([response, 'optionalAccess', _ => _.choices, 'optionalAccess', _2 => _2[0], 'optionalAccess', _3 => _3.message, 'optionalAccess', _4 => _4.content]) || "{}"
34
- );
35
- };
36
- return translator;
37
- };
38
-
39
- // src/translators/google-cloud.ts
40
- var _translate = require('@google-cloud/translate');
41
- var createGoogleCloudTranslator = (options) => {
42
- const translate = new _translate.v2.Translate(options);
43
- const translator = async ({ dictionary, locale }) => {
44
- const translatedOutput = {};
45
- await Promise.all(
46
- Object.keys(dictionary).map(async (key) => {
47
- const value = dictionary[key];
48
- const [translation] = await translate.translate(value, locale);
49
- translatedOutput[key] = translation;
50
- })
51
- );
52
- return translatedOutput;
53
- };
54
- return translator;
55
- };
56
-
57
-
58
-
59
- exports.createChatGptTranslator = createChatGptTranslator; exports.createGoogleCloudTranslator = createGoogleCloudTranslator;
60
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/home/runner/work/terai/terai/packages/translator/dist/translators/index.js","../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"names":[],"mappings":"AAAA;ACCA,gFAA2C;AAKpC,IAAM,wBAAA,EAA0B,CAAC,aAAA,EAAA,GAAiC;AACxE,EAAA,MAAM,OAAA,EAAS,IAAI,qBAAA,CAAO,aAAa,CAAA;AAEvC,EAAA,MAAM,WAAA,EAAyB,MAAA,CAAO;AAAA,IACrC,UAAA;AAAA,IACA,aAAA;AAAA,IACA;AAAA,EACD,CAAA,EAAA,GAAM;AACL,IAAA,MAAM,aAAA,EAAe,IAAA,CAAK,SAAA,CAAU,UAAU,CAAA;AAE9C,IAAA,MAAM,SAAA,EAAW,MAAM,MAAA,CAAO,IAAA,CAAK,WAAA,CAAY,MAAA,CAAO;AAAA,MACrD,KAAA,EAAO,oBAAA;AAAA,MACP,QAAA,EAAU;AAAA,QACT;AAAA,UACC,IAAA,EAAM,QAAA;AAAA,UACN,OAAA,EAAS;AAAA;AAAA,YAER,CAAA,4CAAA,EAA+C,aAAa,CAAA,8CAAA,EAAiD,MAAM,CAAA,CAAA,CAAA;AAAA;AAAA,YAEnH;AAAA,UACD,CAAA,CAAE,IAAA,CAAK,KAAK;AAAA,QACb,CAAA;AAAA,QACA;AAAA,UACC,IAAA,EAAM,MAAA;AAAA,UACN,OAAA,EAAS;AAAA,QACV;AAAA,MACD,CAAA;AAAA,MACA,WAAA,EAAa,GAAA;AAAA,MACb,iBAAA,EAAmB,CAAA,CAAA;AAAA,MACnB,gBAAA,EAAkB,CAAA;AAAA,IACnB,CAAC,CAAA;AAED,IAAA,OAAO,IAAA,CAAK,KAAA;AAAA,sBACX,QAAA,2BAAU,OAAA,4BAAA,CAAU,CAAC,CAAA,6BAAG,OAAA,6BAAS,UAAA,GAAW;AAAA,IAC7C,CAAA;AAAA,EACD,CAAA;AAEA,EAAA,OAAO,UAAA;AACR,CAAA;ADPA;AACA;AErCA,oDAAmB;AAKZ,IAAM,4BAAA,EAA8B,CAAC,OAAA,EAAA,GAAgC;AAC3E,EAAA,MAAM,UAAA,EAAY,IAAI,aAAA,CAAG,SAAA,CAAU,OAAO,CAAA;AAE1C,EAAA,MAAM,WAAA,EAAyB,MAAA,CAAO,EAAE,UAAA,EAAY,OAAO,CAAA,EAAA,GAAM;AAChE,IAAA,MAAM,iBAAA,EAA+B,CAAC,CAAA;AAEtC,IAAA,MAAM,OAAA,CAAQ,GAAA;AAAA,MACb,MAAA,CAAO,IAAA,CAAK,UAAU,CAAA,CAAE,GAAA,CAAI,MAAA,CAAO,GAAA,EAAA,GAAQ;AAC1C,QAAA,MAAM,MAAA,EAAQ,UAAA,CAAW,GAAG,CAAA;AAE5B,QAAA,MAAM,CAAC,WAAW,EAAA,EAAI,MAAM,SAAA,CAAU,SAAA,CAAU,KAAA,EAAO,MAAM,CAAA;AAE7D,QAAA,gBAAA,CAAiB,GAAG,EAAA,EAAI,WAAA;AAAA,MACzB,CAAC;AAAA,IACF,CAAA;AAEA,IAAA,OAAO,gBAAA;AAAA,EACR,CAAA;AAEA,EAAA,OAAO,UAAA;AACR,CAAA;AF6BA;AACE;AACA;AACF,6HAAC","file":"/home/runner/work/terai/terai/packages/translator/dist/translators/index.js","sourcesContent":[null,"// Dependencies\nimport OpenAI, { type ClientOptions } from 'openai'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createChatGptTranslator = (openaiOptions: ClientOptions) => {\n\tconst openai = new OpenAI(openaiOptions)\n\n\tconst translator: Translator = async ({\n\t\tdictionary,\n\t\tprojectLocale,\n\t\tlocale\n\t}) => {\n\t\tconst messagesJson = JSON.stringify(dictionary)\n\n\t\tconst response = await openai.chat.completions.create({\n\t\t\tmodel: 'gpt-3.5-turbo-0125',\n\t\t\tmessages: [\n\t\t\t\t{\n\t\t\t\t\trole: 'system',\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t// 'Act like a professional translator',\n\t\t\t\t\t\t`You will be provided with a JSON in locale: ${projectLocale}, and your task is to translate it to locale: ${locale}.`,\n\t\t\t\t\t\t// 'Keep the same structure and keys, just translate the values.',\n\t\t\t\t\t\t'Do not translate any escaped characters or \"${var}\" ocurrences, just keep them as they are'\n\t\t\t\t\t].join('.\\n')\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\trole: 'user',\n\t\t\t\t\tcontent: messagesJson\n\t\t\t\t}\n\t\t\t],\n\t\t\ttemperature: 0.4,\n\t\t\tfrequency_penalty: -2.0,\n\t\t\tpresence_penalty: -2.0\n\t\t})\n\n\t\treturn JSON.parse(\n\t\t\tresponse?.choices?.[0]?.message?.content || '{}'\n\t\t) as Dictionary\n\t}\n\n\treturn translator\n}\n","// Dependencies\nimport { v2 } from '@google-cloud/translate'\n\n// Types\nimport type { Dictionary, Translator } from '@terai/types'\n\nexport const createGoogleCloudTranslator = (options: v2.TranslateConfig) => {\n\tconst translate = new v2.Translate(options)\n\n\tconst translator: Translator = async ({ dictionary, locale }) => {\n\t\tconst translatedOutput: Dictionary = {}\n\n\t\tawait Promise.all(\n\t\t\tObject.keys(dictionary).map(async (key) => {\n\t\t\t\tconst value = dictionary[key]\n\n\t\t\t\tconst [translation] = await translate.translate(value, locale)\n\n\t\t\t\ttranslatedOutput[key] = translation\n\t\t\t})\n\t\t)\n\n\t\treturn translatedOutput\n\t}\n\n\treturn translator\n}\n"]}