@terai/translator 0.0.0-dev-20240302092932 → 0.0.0-dev-20240302165901

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.js CHANGED
@@ -1,2 +1,17 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});async function i({dictionary:t,projectLocale:o,locale:r,translator:a}){return a({dictionary:t,projectLocale:o,locale:r})}exports.translate = i;
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;
2
17
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/translate.ts"],"names":["translate","dictionary","projectLocale","locale","translator"],"mappings":"AAQA,eAAsBA,EAAU,CAC/B,WAAAC,EACA,cAAAC,EACA,OAAAC,EACA,WAAAC,CACD,EAA0C,CACzC,OAAOA,EAAW,CACjB,WAAAH,EACA,cAAAC,EACA,OAAAC,CACD,CAAC,CACF","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"]}
1
+ {"version":3,"sources":["../src/translate.ts"],"names":[],"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","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"]}
package/dist/index.mjs CHANGED
@@ -1,2 +1,17 @@
1
- async function n({dictionary:t,projectLocale:o,locale:r,translator:a}){return a({dictionary:t,projectLocale:o,locale:r})}export{n as translate};
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
+ };
2
17
  //# 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,eAAsBA,EAAU,CAC/B,WAAAC,EACA,cAAAC,EACA,OAAAC,EACA,WAAAC,CACD,EAA0C,CACzC,OAAOA,EAAW,CACjB,WAAAH,EACA,cAAAC,EACA,OAAAC,CACD,CAAC,CACF","names":["translate","dictionary","projectLocale","locale","translator"]}
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,3 +1,60 @@
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; }var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);var u=r=>{let o=new (0, _openai2.default)(r);return async({dictionary:t,projectLocale:n,locale:e})=>{let a=JSON.stringify(t),s=await o.chat.completions.create({model:"gpt-3.5-turbo",messages:[{role:"system",content:[`Translate this JSON (from locale: ${n}} to locale: ${e}`,"Do not translate the keys of the json","Do not translate ${var}, just leave it as it is"].join(`.
2
- `)},{role:"user",content:a}],temperature:.4,frequency_penalty:-2,presence_penalty:-2});return JSON.parse(_optionalChain([s, 'optionalAccess', _ => _.choices, 'optionalAccess', _2 => _2[0], 'optionalAccess', _3 => _3.message, 'optionalAccess', _4 => _4.content])||"{}")}};var _translate = require('@google-cloud/translate');var g=r=>{let o=new _translate.v2.Translate(r);return async({dictionary:t,locale:n})=>{let e={};return await Promise.all(Object.keys(t).map(async a=>{let s=t[a],[i]=await o.translate(s,n);e[a]=i})),e}};exports.createChatGptTranslator = u; exports.createGoogleCloudTranslator = g;
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",
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;
3
60
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"names":["OpenAI","createChatGptTranslator","openaiOptions","openai","dictionary","projectLocale","locale","messagesJson","response","v2","createGoogleCloudTranslator","options","translate","translatedOutput","key","value","translation"],"mappings":"AACA,OAAOA,MAAoC,SAKpC,IAAMC,EAA2BC,GAAiC,CACxE,IAAMC,EAAS,IAAIH,EAAOE,CAAa,EAmCvC,MAjC+B,OAAO,CACrC,WAAAE,EACA,cAAAC,EACA,OAAAC,CACD,IAAM,CACL,IAAMC,EAAe,KAAK,UAAUH,CAAU,EAExCI,EAAW,MAAML,EAAO,KAAK,YAAY,OAAO,CACrD,MAAO,gBACP,SAAU,CACT,CACC,KAAM,SACN,QAAS,CACR,qCAAqCE,CAAa,gBAAgBC,CAAM,GACxE,wCACA,iDACD,EAAE,KAAK;AAAA,CAAK,CACb,EACA,CACC,KAAM,OACN,QAASC,CACV,CACD,EACA,YAAa,GACb,kBAAmB,GACnB,iBAAkB,EACnB,CAAC,EAED,OAAO,KAAK,MACXC,GAAU,UAAU,CAAC,GAAG,SAAS,SAAW,IAC7C,CACD,CAGD,EC1CA,OAAS,MAAAC,MAAU,0BAKZ,IAAMC,EAA+BC,GAAgC,CAC3E,IAAMC,EAAY,IAAIH,EAAG,UAAUE,CAAO,EAkB1C,MAhB+B,OAAO,CAAE,WAAAP,EAAY,OAAAE,CAAO,IAAM,CAChE,IAAMO,EAA+B,CAAC,EAEtC,aAAM,QAAQ,IACb,OAAO,KAAKT,CAAU,EAAE,IAAI,MAAOU,GAAQ,CAC1C,IAAMC,EAAQX,EAAWU,CAAG,EAEtB,CAACE,CAAW,EAAI,MAAMJ,EAAU,UAAUG,EAAOT,CAAM,EAE7DO,EAAiBC,CAAG,EAAIE,CACzB,CAAC,CACF,EAEOH,CACR,CAGD","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',\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`Translate this JSON (from locale: ${projectLocale}} to locale: ${locale}`,\n\t\t\t\t\t\t'Do not translate the keys of the json',\n\t\t\t\t\t\t'Do not translate ${var}, just leave it as it is'\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"]}
1
+ {"version":3,"sources":["../../src/translators/chat-gpt.ts","../../src/translators/google-cloud.ts"],"names":[],"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,YACR;AAAA,YACA,+CAA+C,aAAa,iDAAiD,MAAM;AAAA,YACnH;AAAA,YACA;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","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',\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"]}
@@ -1,3 +1,60 @@
1
- import m from"openai";var f=r=>{let o=new m(r);return async({dictionary:t,projectLocale:n,locale:e})=>{let a=JSON.stringify(t),s=await o.chat.completions.create({model:"gpt-3.5-turbo",messages:[{role:"system",content:[`Translate this JSON (from locale: ${n}} to locale: ${e}`,"Do not translate the keys of the json","Do not translate ${var}, just leave it as it is"].join(`.
2
- `)},{role:"user",content:a}],temperature:.4,frequency_penalty:-2,presence_penalty:-2});return JSON.parse(s?.choices?.[0]?.message?.content||"{}")}};import{v2 as y}from"@google-cloud/translate";var h=r=>{let o=new y.Translate(r);return async({dictionary:t,locale:n})=>{let e={};return await Promise.all(Object.keys(t).map(async a=>{let s=t[a],[p]=await o.translate(s,n);e[a]=p})),e}};export{f as createChatGptTranslator,h as createGoogleCloudTranslator};
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",
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
+ };
3
60
  //# 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',\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`Translate this JSON (from locale: ${projectLocale}} to locale: ${locale}`,\n\t\t\t\t\t\t'Do not translate the keys of the json',\n\t\t\t\t\t\t'Do not translate ${var}, just leave it as it is'\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,OAAOA,MAAoC,SAKpC,IAAMC,EAA2BC,GAAiC,CACxE,IAAMC,EAAS,IAAIH,EAAOE,CAAa,EAmCvC,MAjC+B,OAAO,CACrC,WAAAE,EACA,cAAAC,EACA,OAAAC,CACD,IAAM,CACL,IAAMC,EAAe,KAAK,UAAUH,CAAU,EAExCI,EAAW,MAAML,EAAO,KAAK,YAAY,OAAO,CACrD,MAAO,gBACP,SAAU,CACT,CACC,KAAM,SACN,QAAS,CACR,qCAAqCE,CAAa,gBAAgBC,CAAM,GACxE,wCACA,iDACD,EAAE,KAAK;AAAA,CAAK,CACb,EACA,CACC,KAAM,OACN,QAASC,CACV,CACD,EACA,YAAa,GACb,kBAAmB,GACnB,iBAAkB,EACnB,CAAC,EAED,OAAO,KAAK,MACXC,GAAU,UAAU,CAAC,GAAG,SAAS,SAAW,IAC7C,CACD,CAGD,EC1CA,OAAS,MAAAC,MAAU,0BAKZ,IAAMC,EAA+BC,GAAgC,CAC3E,IAAMC,EAAY,IAAIH,EAAG,UAAUE,CAAO,EAkB1C,MAhB+B,OAAO,CAAE,WAAAE,EAAY,OAAAC,CAAO,IAAM,CAChE,IAAMC,EAA+B,CAAC,EAEtC,aAAM,QAAQ,IACb,OAAO,KAAKF,CAAU,EAAE,IAAI,MAAOG,GAAQ,CAC1C,IAAMC,EAAQJ,EAAWG,CAAG,EAEtB,CAACE,CAAW,EAAI,MAAMN,EAAU,UAAUK,EAAOH,CAAM,EAE7DC,EAAiBC,CAAG,EAAIE,CACzB,CAAC,CACF,EAEOH,CACR,CAGD","names":["OpenAI","createChatGptTranslator","openaiOptions","openai","dictionary","projectLocale","locale","messagesJson","response","v2","createGoogleCloudTranslator","options","translate","dictionary","locale","translatedOutput","key","value","translation"]}
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',\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,YACR;AAAA,YACA,+CAA+C,aAAa,iDAAiD,MAAM;AAAA,YACnH;AAAA,YACA;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":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@terai/translator",
3
3
  "description": "terai core translator",
4
- "version": "0.0.0-dev-20240302092932",
4
+ "version": "0.0.0-dev-20240302165901",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -30,9 +30,9 @@
30
30
  "openai": "^4"
31
31
  },
32
32
  "devDependencies": {
33
- "@terai/runtime": "0.0.0-dev-20240302092932",
34
- "@terai/types": "0.0.0-dev-20240302092932",
35
- "@terai/utils": "0.0.0-dev-20240302092932"
33
+ "@terai/runtime": "0.0.0-dev-20240302165901",
34
+ "@terai/types": "0.0.0-dev-20240302165901",
35
+ "@terai/utils": "0.0.0-dev-20240302165901"
36
36
  },
37
37
  "scripts": {
38
38
  "dev": "tsup --format=esm,cjs --no-dts --watch",