@terai/translator 0.0.0-dev-20231215105236

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/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hugo Corta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
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 };
@@ -0,0 +1,9 @@
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 ADDED
@@ -0,0 +1,2 @@
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;
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/translate.ts"],"names":["translate","dictionary","projectLocale","locale","translator"],"mappings":"AAQA,eAAsBA,EAAU,CAC9B,WAAAC,EACA,cAAAC,EACA,OAAAC,EACA,WAAAC,CACF,EAA0C,CACxC,OAAOA,EAAW,CAChB,WAAAH,EACA,cAAAC,EACA,OAAAC,CACF,CAAC,CACH","sourcesContent":["// Types\nimport type { Config, Locale, Dictionary } from '@terai/types'\n\nexport type TranslateOptions = Pick<Config, 'translator' | 'projectLocale'> & {\n dictionary: Dictionary\n locale: Locale\n}\n\nexport async function translate({\n dictionary,\n projectLocale,\n locale,\n translator\n}: TranslateOptions): Promise<Dictionary> {\n return translator({\n dictionary,\n projectLocale,\n locale\n })\n}\n"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ async function n({dictionary:t,projectLocale:o,locale:r,translator:a}){return a({dictionary:t,projectLocale:o,locale:r})}export{n as translate};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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 dictionary: Dictionary\n locale: Locale\n}\n\nexport async function translate({\n dictionary,\n projectLocale,\n locale,\n translator\n}: TranslateOptions): Promise<Dictionary> {\n return translator({\n dictionary,\n projectLocale,\n locale\n })\n}\n"],"mappings":"AAQA,eAAsBA,EAAU,CAC9B,WAAAC,EACA,cAAAC,EACA,OAAAC,EACA,WAAAC,CACF,EAA0C,CACxC,OAAOA,EAAW,CAChB,WAAAH,EACA,cAAAC,EACA,OAAAC,CACF,CAAC,CACH","names":["translate","dictionary","projectLocale","locale","translator"]}
@@ -0,0 +1,9 @@
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 };
@@ -0,0 +1,9 @@
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 };
@@ -0,0 +1,3 @@
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;
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,CACvE,IAAMC,EAAS,IAAIH,EAAOE,CAAa,EAmCvC,MAjC+B,OAAO,CACpC,WAAAE,EACA,cAAAC,EACA,OAAAC,CACF,IAAM,CACJ,IAAMC,EAAe,KAAK,UAAUH,CAAU,EAExCI,EAAW,MAAML,EAAO,KAAK,YAAY,OAAO,CACpD,MAAO,gBACP,SAAU,CACR,CACE,KAAM,SACN,QAAS,CACP,qCAAqCE,CAAa,gBAAgBC,CAAM,GACxE,wCACA,iDACF,EAAE,KAAK;AAAA,CAAK,CACd,EACA,CACE,KAAM,OACN,QAASC,CACX,CACF,EACA,YAAa,GACb,kBAAmB,GACnB,iBAAkB,EACpB,CAAC,EAED,OAAO,KAAK,MACVC,GAAU,UAAU,CAAC,GAAG,SAAS,SAAW,IAC9C,CACF,CAGF,EC1CA,OAAS,MAAAC,MAAU,0BAKZ,IAAMC,EAA+BC,GAAgC,CAC1E,IAAMC,EAAY,IAAIH,EAAG,UAAUE,CAAO,EAkB1C,MAhB+B,OAAO,CAAE,WAAAP,EAAY,OAAAE,CAAO,IAAM,CAC/D,IAAMO,EAA+B,CAAC,EAEtC,aAAM,QAAQ,IACZ,OAAO,KAAKT,CAAU,EAAE,IAAI,MAAMU,GAAO,CACvC,IAAMC,EAAQX,EAAWU,CAAG,EAEtB,CAACE,CAAW,EAAI,MAAMJ,EAAU,UAAUG,EAAOT,CAAM,EAE7DO,EAAiBC,CAAG,EAAIE,CAC1B,CAAC,CACH,EAEOH,CACT,CAGF","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 const openai = new OpenAI(openaiOptions)\n\n const translator: Translator = async ({\n dictionary,\n projectLocale,\n locale\n }) => {\n const messagesJson = JSON.stringify(dictionary)\n\n const response = await openai.chat.completions.create({\n model: 'gpt-3.5-turbo',\n messages: [\n {\n role: 'system',\n content: [\n `Translate this JSON (from locale: ${projectLocale}} to locale: ${locale}`,\n 'Do not translate the keys of the json',\n 'Do not translate ${var}, just leave it as it is'\n ].join('.\\n')\n },\n {\n role: 'user',\n content: messagesJson\n }\n ],\n temperature: 0.4,\n frequency_penalty: -2.0,\n presence_penalty: -2.0\n })\n\n return JSON.parse(\n response?.choices?.[0]?.message?.content || '{}'\n ) as Dictionary\n }\n\n return 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 const translate = new v2.Translate(options)\n\n const translator: Translator = async ({ dictionary, locale }) => {\n const translatedOutput: Dictionary = {}\n\n await Promise.all(\n Object.keys(dictionary).map(async key => {\n const value = dictionary[key]\n\n const [translation] = await translate.translate(value, locale)\n\n translatedOutput[key] = translation\n })\n )\n\n return translatedOutput\n }\n\n return translator\n}\n"]}
@@ -0,0 +1,3 @@
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};
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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 const openai = new OpenAI(openaiOptions)\n\n const translator: Translator = async ({\n dictionary,\n projectLocale,\n locale\n }) => {\n const messagesJson = JSON.stringify(dictionary)\n\n const response = await openai.chat.completions.create({\n model: 'gpt-3.5-turbo',\n messages: [\n {\n role: 'system',\n content: [\n `Translate this JSON (from locale: ${projectLocale}} to locale: ${locale}`,\n 'Do not translate the keys of the json',\n 'Do not translate ${var}, just leave it as it is'\n ].join('.\\n')\n },\n {\n role: 'user',\n content: messagesJson\n }\n ],\n temperature: 0.4,\n frequency_penalty: -2.0,\n presence_penalty: -2.0\n })\n\n return JSON.parse(\n response?.choices?.[0]?.message?.content || '{}'\n ) as Dictionary\n }\n\n return 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 const translate = new v2.Translate(options)\n\n const translator: Translator = async ({ dictionary, locale }) => {\n const translatedOutput: Dictionary = {}\n\n await Promise.all(\n Object.keys(dictionary).map(async key => {\n const value = dictionary[key]\n\n const [translation] = await translate.translate(value, locale)\n\n translatedOutput[key] = translation\n })\n )\n\n return translatedOutput\n }\n\n return translator\n}\n"],"mappings":"AACA,OAAOA,MAAoC,SAKpC,IAAMC,EAA2BC,GAAiC,CACvE,IAAMC,EAAS,IAAIH,EAAOE,CAAa,EAmCvC,MAjC+B,OAAO,CACpC,WAAAE,EACA,cAAAC,EACA,OAAAC,CACF,IAAM,CACJ,IAAMC,EAAe,KAAK,UAAUH,CAAU,EAExCI,EAAW,MAAML,EAAO,KAAK,YAAY,OAAO,CACpD,MAAO,gBACP,SAAU,CACR,CACE,KAAM,SACN,QAAS,CACP,qCAAqCE,CAAa,gBAAgBC,CAAM,GACxE,wCACA,iDACF,EAAE,KAAK;AAAA,CAAK,CACd,EACA,CACE,KAAM,OACN,QAASC,CACX,CACF,EACA,YAAa,GACb,kBAAmB,GACnB,iBAAkB,EACpB,CAAC,EAED,OAAO,KAAK,MACVC,GAAU,UAAU,CAAC,GAAG,SAAS,SAAW,IAC9C,CACF,CAGF,EC1CA,OAAS,MAAAC,MAAU,0BAKZ,IAAMC,EAA+BC,GAAgC,CAC1E,IAAMC,EAAY,IAAIH,EAAG,UAAUE,CAAO,EAkB1C,MAhB+B,OAAO,CAAE,WAAAE,EAAY,OAAAC,CAAO,IAAM,CAC/D,IAAMC,EAA+B,CAAC,EAEtC,aAAM,QAAQ,IACZ,OAAO,KAAKF,CAAU,EAAE,IAAI,MAAMG,GAAO,CACvC,IAAMC,EAAQJ,EAAWG,CAAG,EAEtB,CAACE,CAAW,EAAI,MAAMN,EAAU,UAAUK,EAAOH,CAAM,EAE7DC,EAAiBC,CAAG,EAAIE,CAC1B,CAAC,CACH,EAEOH,CACT,CAGF","names":["OpenAI","createChatGptTranslator","openaiOptions","openai","dictionary","projectLocale","locale","messagesJson","response","v2","createGoogleCloudTranslator","options","translate","dictionary","locale","translatedOutput","key","value","translation"]}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@terai/translator",
3
+ "description": "terai core translator",
4
+ "version": "0.0.0-dev-20231215105236",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "author": "Hugo Corta <corta.hugo@gmail.com>",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "sideEffects": false,
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "require": "./dist/index.js",
20
+ "import": "./dist/index.mjs"
21
+ },
22
+ "./translators": {
23
+ "types": "./dist/translators/index.d.ts",
24
+ "require": "./dist/translators/index.js",
25
+ "import": "./dist/translators/index.mjs"
26
+ }
27
+ },
28
+ "dependencies": {
29
+ "@google-cloud/translate": "^8",
30
+ "openai": "^4"
31
+ },
32
+ "devDependencies": {
33
+ "@terai/runtime": "0.0.0-dev-20231215105236",
34
+ "@terai/types": "0.0.0-dev-20231215105236",
35
+ "@terai/utils": "0.0.0-dev-20231215105236"
36
+ },
37
+ "scripts": {
38
+ "dev": "tsup --format=esm,cjs --no-dts --watch",
39
+ "build": "tsup"
40
+ }
41
+ }