slang_gpt 0.11.0

SDKdartflutter
Platformandroidioswindowslinuxmacos

Use GPT to automatically translate at compile time. A tool for slang.

slang_gpt

Use GPT to automatically translate your app at compile time.

This is library is intended to be used with slang.

Currently, only the OpenAI API is supported.

Motivation

Google Translate and other translation services are great, but they are not perfect.

One of the biggest issues is that they are not context aware. For example, the word "bank" can be translated to "Bank" or "Ufer" in German depending on the context.

With GPT and some prompt engineering, we can get context aware translations.

Getting Started

# pubspec.yaml
dependencies:
  slang: <version>

dev_dependencies:
  slang_gpt: <version>

Add the gpt entry to build.yaml or slang.yaml.

# existing config
base_locale: fr
fallback_strategy: base_locale
input_directory: lib/i18n
input_file_pattern: .i18n.json
output_directory: lib/i18n

# add this
gpt:
  model: gpt-4
  description: |
    "River Adventure" is a game where you need to cross a river by jumping on stones.
    The game is over when you either fall into the water or reach the other side.

Let's run this:

dart run slang_gpt --target=fr --api-key=<api-key>

Configuration

KeyTypeUsageRequiredDefault
modelStringModel nameYES
max_input_lengthintMax input characters per requestNO(inferred by model)
temperaturedoubleTemperature parameter for GPTNO(API default)
descriptionStringApp descriptionYES
excludesListList of locales to excludeNO[]

Command line arguments

ArgumentDescriptionRequiredDefault
--target=Target languageNO(all existing locales)
--api-key=API keyYES
-f / --fullSkip partial translationNO(partial translation)
-d / --debugWrite chat to fileNO(no chat output)
--outdir=Output directoryNO*(using config)

Models

Model nameProviderContext lengthCost per 1k input tokenCost per 1k output token
gpt-3.5-turboOpen AI4096$0.0005$0.0015
gpt-3.5-turbo-16kOpen AI16384$0.003$0.004
gpt-4Open AI8192$0.03$0.06
gpt-4-turboOpen AI64000$0.01$0.03
gpt-4oOpen AI128000$0.005$0.015
gpt-4o-miniOpen AI128000$0.00015$0.0006

1k tokens = 750 words (English)

GPT context length

Each model has a different context length. Try to avoid exceeding it as the model starts to "forget".

Luckily, slang_gpt supports splitting the input into multiple requests.

The max_input_length is optional and defaults to some heuristic.

If you work with less common languages and the model starts to forget, try to reduce the max_input_length.

Alternatively, you can also use a model with a larger context length.

Partial translation

By default, slang_gpt will only translate missing keys to reduce costs.

You may add the --full flag to translate all keys.

dart run slang_gpt --target=fr --full --api-key=<api-key>

To avoid a specific subset of keys from being translated, you may add the ignoreGpt modifier to the key:

{
  "key1": "This will be translated",
  "key2(ignoreGpt)": {
    "key3": "This will be ignored"
  }
}

Target language

By default, slang_gpt will translate to all existing locales.

You may add the --target flag to translate to a specific locale. This may be useful if you want to translate to a new locale.

Additionally, you may also use predefined language sets (keep in mind that English must be the base locale):

By GDP (Gross Domestic Product):

FlagLanguages
--target=gdp-3["zh-Hans", "es", "ja"]
--target=gdp-5["zh-Hans", "es", "ja", "de", "fr"]
--target=gdp-10["zh-Hans", "es", "ja", "de", "fr", "pt", "ar", "it", "ru", "ko"]

By region and population:

FlagLanguages
--target=eu-3["de", "fr", "it"]
--target=eu-5["de", "fr", "it", "es", "pl"]
--target=eu-10["de", "fr", "it", "es", "pl", "ro", "nl", "cs", "el", "sv"]