A language toolkit for AI developers.
Full documentation coming soon!
In your terminal:
npm i generaltranslation
In your code, import functions directly
import { getLanguageName } from 'generaltranslation'
or, to initialize a GT API client:
import GT from 'generaltranslation'
const gt = new GT()
Returns a language name from a two or three-letter ISO-639 language code, or an array of codes.
const language = getLanguageName('en');
console.log(language) // 'English'
const languages = getLanguageName(['fr', 'es'])
console.log(languages) // ['French', 'Spanish']
Returns an ISO-639 code from a language name or an array of language names.
const code = getLanguageCode('English');
console.log(language) // 'en'
const codes = getLanguageCodes(['French', 'Spanish'])
console.log(codes) // ['fr', 'es']
For this function, you need to sign up for an API key at generaltranslation.com.
There's a small, free allowance to let you test out the API without payment details.
Add the API key to your code like this:
import GT from 'generaltranslation'
const gt = new GT({
apiKey: process.env.GT_API_KEY // looks like 'gtx-XXX'
});
Translates content into the language represented by an ISO-639 language code. Caches by default. Just wrap translate
around your content and go.
All of the following are valid:
const translation = await gt.translate('Tell me a story', 'es'); // returns a string
const first = 'Tell me a story';
const second = ' about a cat'
const translated = await gt.translate([
first, second
], 'es');
To mark text that shouldn't be translated, wrap it in { text: "", translate: false }
. Items marked as translate: false
are never sent to our API. For example:
const prompt = 'Tell me a story about ';
const input = 'gatos con espadas'
const translatedPrompt = await gt.translate([
prompt, { text: input, translate: false }
], 'es');
For type consistency, you can also make everything in the content parameter an object:
const prompt = 'Tell me a story about ';
const input = 'gatos con espadas'
const translatedPrompt = await gt.translate([
{ text: prompt },
{ text: input, translate: false }
], 'es');
This also works:
const translatedPrompt = await gt.translate({ text: 'Tell me a story' }, 'es');
Translates multiple items of content into the language represented by an ISO-639 language code. Caches by default. Just wrap translateMany
around an array of what you want to translate and specify a language. For example:
const requests = ["You say goodbye.", "And I say hello."];
const translationArray = await gt.translateMany(requests, 'es') // returns an array