Locize CLI
A command-line interface for interacting with the locize localization platform. Currently at version 12.1.1, the `locize-cli` streamlines localization workflows by allowing programmatic management of translation keys and files. It enables tasks such as adding, removing, and retrieving translation entries, as well as downloading published translations in a wide array of formats including JSON, XLIFF, Android, YAML, and more. The project maintains a regular release cadence, with minor patches and dependency updates often occurring on a weekly or bi-weekly basis. A key differentiator is its comprehensive `sync` command, which facilitates bidirectional synchronization between local translation files and the remote locize platform, enabling developers to maintain their existing code setup while keeping translations updated. It also offers a dedicated GitHub Action for seamless integration into CI/CD pipelines, making it an essential tool for automating localization processes for projects utilizing locize. This CLI is primarily used globally and does not provide an API for programmatic JavaScript imports, focusing instead on shell-based execution for routine localization tasks.
Common errors
-
locize: command not found
cause The `locize-cli` package is not installed globally or its installation directory is not in your system's PATH.fixInstall `locize-cli` globally using `npm install -g locize-cli`. Verify your npm global bin directory is in your system's PATH. -
Error: Invalid API Key / Error: Project not found
cause The provided `--api-key` or `--project-id` is incorrect, missing, or does not have the necessary permissions for the operation.fixDouble-check your API Key and Project ID for typos. Ensure the API Key has sufficient permissions for the requested action (e.g., read, write, admin access). -
Error: Cannot find module 'locize-cli'
cause Attempting to use `require()` or `import` with `locize-cli` in a JavaScript/TypeScript file, treating it as a library.fix`locize-cli` is a command-line interface tool meant for shell execution, not programmatic import. Use `npm install -g locize-cli` and run `locize [command]` in your terminal.
Warnings
- gotcha Namespace names become filenames on disk. Using characters such as `< > : " / \ | ? *` in your namespace names can lead to file writing issues on various operating systems.
- gotcha When specifying the translation version for commands like `download`, use `--ver` instead of `--version`. The `--version` flag is reserved for printing the CLI tool's own version number.
- gotcha When using the `sync` command, the CLI operates on published translations by default. If you intend to synchronize with unpublished translations, you must explicitly enable this.
- breaking Versions of `locize-cli` 12.x and above require Node.js version 22 or higher to function correctly.
Install
-
npm install locize-cli -
yarn add locize-cli -
pnpm add locize-cli
Imports
- locize-cli
import { locize } from 'locize-cli'npm install -g locize-cli
Quickstart
// To run these commands, first install the CLI globally:
// npm install -g locize-cli
const API_KEY = process.env.LOCIZE_API_KEY ?? 'YOUR_LOCIZE_API_KEY'; // Replace or set LOCIZE_API_KEY env var
const PROJECT_ID = process.env.LOCIZE_PROJECT_ID ?? 'YOUR_LOCIZE_PROJECT_ID'; // Replace or set LOCIZE_PROJECT_ID env var
console.log('--- Locize CLI Quickstart ---\n');
console.log('To add/update a new translation key (e.g., in English, namespace 'namespace1'):');
console.log(`locize add --api-key ${API_KEY} --project-id ${PROJECT_ID} --language en namespace1 myNewKey "My new value"\n`);
console.log('To remove a translation key:');
console.log(`locize remove --api-key ${API_KEY} --project-id ${PROJECT_ID} --language en namespace1 myNewKey\n`);
console.log('To download current published files for a specific language and namespace to a local directory:');
console.log(`locize download --project-id ${PROJECT_ID} --ver latest --language en --namespace namespace1 --path ./backup\n`);
console.log('To synchronize local translation files with the locize platform (example path, adjust as needed):');
console.log(`locize sync --api-key ${API_KEY} --project-id ${PROJECT_ID} --path './src/locales/{{lng}}/{{ns}}.json' --language 'en' --pull-local-missing true --push-remote-missing true\n`);
console.log('Please ensure your API_KEY and PROJECT_ID are correctly set (preferably via environment variables) before running these commands in your shell/terminal.');