Skip to main content

Use LLM to translate in your shell

Project description

llm-tr: translate text using llm in your terminal

PyPI Changelog Tests License

llm-tr is a plugin for LLM that lets you translate text directly from your terminal using your favorite large language model. It automatically detects the source language and translates to your preferred language, with smart fallbacks and clipboard integration.

Installation

First, install LLM if you haven't already.

Then install llm-tr:

llm install llm-tr

If you want automatic copying of results to your clipboard, install with the pyclip extra:

llm install 'llm-tr[pyclip]'

Usage

Translate any text from your terminal:

llm tr Hola mundo

The plugin will:

  • Detect the source language of the input.
  • Translate to your preferred language (see below for how this is chosen).
  • Print the translation to stdout.
  • If installed with [pyclip], copy the result to your clipboard automatically.

Preferred Language Logic

llm-tr determines your target translation language using the following priority:

  1. Explicit Language (CLI or Environment Variable):

    • Use the --language (or -l) option:
      llm tr -l french Hello world
      
    • Or set the LLM_TR_LANGUAGE environment variable:
      export LLM_TR_LANGUAGE=spanish
      
  2. System Language:

    • If no explicit language is set, llm-tr uses your system locale (e.g., LANG environment variable or OS locale settings).
  3. Fallback to English:

    • If neither of the above is available or recognized, English is used as the default target language.

Note: The plugin will never translate into the detected source language. If your preferred language matches the source, it will use the next available option.

Examples

  • Translate to French explicitly:
    llm tr -l french ¡Viva Perón y que mueran los gorilas!
    
  • Use environment variable for Spanish and quote the input
    export LLM_TR_LANGUAGE=spanish
    llm tr "Proletarier aller Länder, vereinigt euch!"
    
  • Let the system locale decide (e.g., if your system is set to German):
    llm tr Good morning
    
    (If your system language is German, this will translate to German unless the input is already in German.)

Clipboard Integration

If you install llm-tr with [pyclip](https://pypi.org/project/pyclip/` extra (i.e., llm install 'llm-tr[pyclip]'), the translated result will be automatically copied to your clipboard after each translation.

In addition, you can also translate text from your clipboard using the -x (or --paste) option:

llm tr -x

This will read the current clipboard content as the input text to translate, instead of requiring you to type or paste it into the terminal. This is especially useful for quickly translating text you've just copied from another application.

If pyclip is not available or clipboard setup fails, these features are ignored.

System Prompt

llm-tr uses the following system prompt for the LLM:

First, detect the source language of the input text.

You have two preferred languages as target languages: {second_language} and {system_language}. Translate the text into the first preferred language that is different from the detected source language.

Return only the translated text as a string, with no explanations or extra output.

Supported Languages

You can specify your preferred language using either the language name or its code. Supported languages include:

afrikaans (af), arabic (ar), bulgarian (bg), bengali (bn), catalan (ca), czech (cs), danish (da), german (de), greek (el), english (en), spanish (es), estonian (et), persian (fa), finnish (fi), french (fr), hebrew (he), hindi (hi), croatian (hr), hungarian (hu), indonesian (id), italian (it), japanese (ja), korean (ko), lithuanian (lt), latvian (lv), macedonian (mk), malay (ms), norwegian bokmål (nb), dutch (nl), polish (pl), portuguese (pt), romanian (ro), russian (ru), slovak (sk), slovenian (sl), serbian (sr), swedish (sv), thai (th), turkish (tr), ukrainian (uk), vietnamese (vi), chinese (zh).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_tr-0.1.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_tr-0.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file llm_tr-0.1.1.tar.gz.

File metadata

  • Download URL: llm_tr-0.1.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llm_tr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8cd5b6783095837501151a4cbbb1e57a6921e7dd53078d2f210fd9719d100ff8
MD5 47cb034131f64cb36a9b138a7e9f8f15
BLAKE2b-256 d2f87aca21ed4520e48853c414fe442a5d52c1497f9dad3155a1b6284c3cfaf4

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_tr-0.1.1.tar.gz:

Publisher: cd.yml on mgaitan/llm-tr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_tr-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: llm_tr-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llm_tr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07125604264150dc02d1c454b8fbe678f8eee935230015422a4f12379584489e
MD5 a1f7a9de6b8bfa334139c26acca31183
BLAKE2b-256 0b59070063419a577de0449a12329e0fc096f7656bc20851e8fd57df38c97b2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_tr-0.1.1-py3-none-any.whl:

Publisher: cd.yml on mgaitan/llm-tr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page