@ts-intl/translator
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

AI Translator

This repo is to maintain locale files by automatically translate untranslated locales which powered by openai.

Why Use AI

We use AI to translate the locale files, because it's hard to find a human translator who can translate all the locale files of all the projects.

Try it if you have very limited budget (translators and lokalise accounts)!

Install

pnpm i @ts-intl/translator

Usages

Initialize config and git flow files of translator:

npx ts-intl-init-translator

Generate translations manually:

npx ts-intl-translate

or

npx ts-intl-translate --env=production

Coding:

import { translate } from '@ts-intl/translator';

await translate();

Locales Structure Required

.
├── yourRepo
│   ├── yourLocalesPath
│   │   ├── en
│   │   │   └── namespace1.json
│   │   └── [other]
  • yourLocalesPath: The locale directory entry of yourRepo.
  • en (Language Code or Language ID): The English locale entry of project1.
  • [other] (Language Code or Language ID): The [other] (it, zh, it-it, etc...) locale entry of project1.
  • namespace1.json: The namespace (to split large file) entry of project1.

Git Flow

Change the .github/workflows/translator-shared.yml or .github/workflows/translator-flow.yml in your repo if needed.

Add or update your locale files (in [yourLocalesPath]), then commit and push to remote.

Then the automatic translation will be triggered, and you can check the translation status on Github Actions.

The action would add commit feat: [AI Generated] translations to your branch. Which includes the AI generated translations.

Environment Variables

To enable AI translation:

  • Setup process.env.OPENAI_API_KEY.
  • Create .env.development and .env.production with:
OPENAI_API_KEY=you-api-key # sk-xxxx
  • Setup secrets.OPENAI_API_KEY of your Github repo.

ts-intl-translator.config.json

type ProjectConfig

Supported syntax list:

  1. icu, another simple doc from formatjs.

descriptions:

Provide the descriptions of the keys (optional), which will help AI generate more accurate translations.

{
  "ns1.key1.key2.key3": "description of ns1.key1.key2.key3"
}

Sample:

// ./yourLocalesPath/en/ns1.json
{
  "key1": {
    "key2": {
      "key3": "Bob, Hello World!"
    }
  },
  "key4": "Minted is awesome!"
}

When nsDivider is ".", keyDivider is "."

{
  "descriptions": {
    "ns1.key1.key2.key3": "Bob is my best friend",
    "ns1.key4": "Minted is a NFT trading platform"
  }
}

When nsDivider is ":", keyDivider is "."

{
  "descriptions": {
    "ns1:key1.key2.key3": "Bob is my best friend",
    "ns1:key4": "Minted is a NFT trading platform"
  }
}

IMPORTANT

Only auto-translate those missing or empty values, which means manually added values would always reserve.

This strategy allow us to:

  • Add Reserved Words, like "Hello World".
  • Add Proper Nouns, like brands and guide links.
  • Override AI translations.

Package Sidebar

Install

npm i @ts-intl/translator

Weekly Downloads

53

Version

2.0.0

License

MIT

Unpacked Size

63.4 kB

Total Files

73

Last publish

Collaborators

  • hakonleung