GettextTranslator

View Source

CI

GettextTranslator is a robust translation utility for Elixir projects that leverages advanced AI endpoints to provide high-quality translations. The library seamlessly integrates with Elixir's Gettext, enabling you to maintain the integrity of your application's messages while translating them accurately. It supports multiple AI providers, making it adaptable to your preferred translation service.

Changelog: See CHANGELOG.md for detailed changes in each version.

Features

  • Multi-Provider Support: Easily switch between translation endpoints such as Ollama AI and OpenAI.
  • Customizable Configurations: Define your own translation persona, style, and languages to ignore.
  • CLI Integration: Translate Gettext files directly from the command line.
  • Seamless Gettext Integration: Automatically translate files in your Gettext directory.

Installation

Add gettext_translator to your dependencies in your mix.exs:

def deps do
  [
    {:gettext_translator, "~> 0.1.0"}
  ]
end

Then, run:

mix deps.get

Configuration

Configure GettextTranslator in your config/config.exs file. Below are examples using two different AI translation endpoints.

Using Ollama AI

config :gettext_translator,
  endpoint: LangChain.ChatModels.ChatOllamaAI,
  endpoint_model: "llama3.2:latest",
  endpoint_temperature: 0,
  endpoint_config: %{}, # Leave empty if running Ollama locally with the default API endpoint
  persona: "You are a professional translator. Your goal is to translate the message to the target language while preserving meaning and length.",
  style: "Casual, using simple language",
  ignored_languages: ["en"]

Using OpenAI

config :gettext_translator,
  endpoint: LangChain.ChatModels.ChatOpenAI,
  endpoint_model: "gpt-4",
  endpoint_temperature: 0,
  endpoint_config: %{
    "openai_key" => "YOUR_OPENAI_KEY",
    "openai_org_id" => "YOUR_ORG_ID"
  },
  persona: "You are a professional translator. Your goal is to translate the message to the target language while preserving meaning and length.",
  style: "Casual, using simple language",
  ignored_languages: ["en"]

Usage

Running the Translator

Translate Using the Default Gettext Folder

To translate the default Gettext folder (priv/gettext), simply run:

mix gettext_translator.run

Translate Using a Specific Gettext Folder

If your Gettext files are located in a different directory, specify the path:

mix gettext_translator.run path/to/your/gettext

Advanced Examples

Batch Translation Script

For projects with multiple Gettext directories, you can create a custom Elixir module to run translations in batch:

defmodule BatchTranslator do
  def run do
    ["priv/gettext", "lib/my_app/gettext"]
    |> Enum.each(fn path ->
      System.cmd("mix", ["gettext_translator.run", path])
    end)
  end
end

Run the batch translation in IEx:

iex -S mix
iex> BatchTranslator.run()

Overriding Configurations at Runtime

You might want to adjust the translation behavior without changing your config files permanently. Override settings in IEx:

iex> Application.put_env(:gettext_translator, :persona, "Translate concisely and accurately.")

Documentation

Comprehensive documentation is available on HexDocs. It covers detailed configuration options, advanced usage, integration guides, and best practices.

Contributing

We welcome contributions from the community! Please review our CONTRIBUTING.md for guidelines on submitting issues and pull requests.

License

GettextTranslator is released under the Apache License 2.0 License.