npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ollama-i18n

v1.1.3

Published

CLI tool for automated translation of i18n locale files using Ollama

Downloads

241

Readme

ollama-i18n

A tiny CLI tool for automated translation of i18n locale files using Ollama models.

Why?

Translating i18n files locally with "small" LLMs has a few benefits:

  • No API usage costs and API keys required - translate as much as you want
  • Local inference is quick, especially for short strings (typical in i18n files)
  • Small LLMs (3-7B parameters) are surprisingly capable at translating simple UI texts correctly
  • Switching between different models using Ollama is very easy

Prerequisites

  • Node.js >= 18
  • Ollama installed and running locally
  • A locale directory containing JSON translation files

Installation

To install the CLI tool globally using npm:

npm install -g ollama-i18n

Usage

To translate all available locales:

ollama-i18n --source en --dir ./locales

This will:

  1. Use en.json as the source reference file
  2. Find all other JSON files in the ./locales directory
  3. Translate any missing translations to the target language
  4. Keep any existing translations (default)

Note: All locale files must exist in the provided directory (they can be empty) when running without --target (-t), while using --target will create the locale file if it doesn't exist yet.

Options

Options:
  -d, --dir <path>    Directory containing the locale files (required)
  -s, --source <locale>  Source language file name without extension (required)
  -t, --target <locale>  Target language file name without extension (optional)
  -m, --model <name>   Ollama model to use (default: "llama3.2:3b")
  --no-cache          Translate all keys and ignore existing translations
  -h, --help          Show help information
  -v, --version       Show version number

Examples

Translate to a specific language:

ollama-i18n -s en -t fr -d ./locales

Use a different Ollama model:

ollama-i18n -s en -d ./locales -m mistral

Retranslate all keys (do not use the cache):

ollama-i18n -s en -d ./locales --no-cache

Using as a Pre-commit Hook

You can use ollama-i18n to check for missing translations before each commit.

Setup

  1. Install required dependencies:
npm install --save-dev husky lint-staged ollama-i18n
  1. Initialize husky:
npx husky install
npm pkg set scripts.prepare="husky install"
  1. Add to your package.json:
{
  "lint-staged": {
    "locales/*.json": "ollama-i18n -s en -d ./locales"
  }
}
  1. Then, add the pre-commit hook:
npx husky add .husky/pre-commit "npx lint-staged"

Now, whenever you commit changes to your locale files, ollama-i18n will automatically check for and translate any missing translations.

Locale File Structure

The CLI tool expects your JSON files with the language code to have a certain structure.

E.g. using the command ollama-i18n -s en -d ./locales:

locales/
  ├── en.json     # Source file
  ├── fr.json     # French translations
  ├── de.json     # German translations
  └── es.json     # Spanish translations

Example file content:

{
  "common": {
    "save": "Save",
    "cancel": "Cancel"
  },
  "validation": {
    "required": "{field} is required",
    "minLength": "{field} must be at least {min} characters"
  }
}

Development

To run the CLI locally using a ./locales directory with two locales, en.json and de.json, run the following command:

npm run dev -- --dir ./locales -s en -t de