Loco CLI
Loco CLI is a command-line interface tool designed to automate the synchronization of project translations with the Loco localization platform (localise.biz). It facilitates both pulling translations from Loco into your local project and pushing local changes back to Loco. The current stable version is 3.4.0, with a fairly active release cadence, frequently adding new features, improving types, and updating dependencies as seen in recent changelogs. A key differentiator is its focus as a Node.js-based solution, offering flexibility in configuration via `package.json` or various `.locorc` files, supporting both JSON and YAML formats, and now providing a dedicated documentation website. It aims to streamline the localization workflow for developers using JavaScript/TypeScript projects.
Common errors
-
Error: Cannot find module 'loco-cli'
cause The `loco-cli` package is not installed or not resolvable in the current project context.fixRun `npm install loco-cli` or `yarn add loco-cli` in your project, or ensure `npx loco-cli` is used for one-off commands if not globally installed. -
Error: Loco API key not found. Please set the LOCO_API_KEY environment variable.
cause The required `LOCO_API_KEY` environment variable, essential for authenticating with the Loco API, has not been set.fixSet the `LOCO_API_KEY` environment variable in your terminal session (e.g., `export LOCO_API_KEY='your_key_here'`) or in your CI/CD configuration. -
TypeError: Cannot read properties of undefined (reading 'pull')
cause This error can occur if you are trying to programmatically access a non-existent or incorrectly imported function from the `loco-cli` package, or if the internal API has changed.fixReview the package's `index.d.ts` file or documentation for available programmatic exports. For typical usage, invoke `loco-cli` via `npx` or directly from the command line.
Warnings
- breaking Version 3.0.0 dropped support for Node.js 12 and upgraded various internal dependencies. Projects running on Node.js 12 will need to upgrade their Node.js runtime to continue using `loco-cli` v3.x.x.
- gotcha The configuration system was migrated to `cosmiconfig` in v2.1.1, introducing support for multiple configuration file formats (e.g., `.locorc`, `.locorc.json`, `loco.config.js`). While older direct CLI arguments might still work, using a dedicated configuration file is the recommended and most flexible approach.
- gotcha As a CLI tool, `loco-cli` heavily relies on environment variables (e.g., `LOCO_API_KEY`) for authentication. Forgetting to set these, or setting them incorrectly, is a common cause of operation failures.
Install
-
npm install loco-cli -
yarn add loco-cli -
pnpm add loco-cli
Imports
- run
const { run } = require('loco-cli');import { run } from 'loco-cli'; - LocoConfig
import type { LocoConfig } from 'loco-cli'; - LocoCommandOptions
import type { LocoCommandOptions } from 'loco-cli';
Quickstart
import { run } from 'loco-cli';
import path from 'node:path';
async function syncTranslations() {
console.log('Starting translation sync...');
try {
// Example: Programmatically run the 'pull' command
// This assumes `run` function can accept arguments programmatically.
// In typical usage, you'd use 'npx loco-cli pull' from the terminal.
await run(['pull', '--config', path.resolve(process.cwd(), '.locorc.js')], {
cwd: process.cwd(),
// Pass environment variables or specific options needed by the CLI
env: { LOCO_API_KEY: process.env.LOCO_API_KEY ?? '', ...process.env }
});
console.log('Translations pulled successfully!');
// Example: Programmatically run the 'push' command with a specific tag
await run(['push', '--tag', 'web-app', '--yes'], {
cwd: process.cwd(),
env: { LOCO_API_KEY: process.env.LOCO_API_KEY ?? '', ...process.env }
});
console.log('Translations pushed successfully!');
} catch (error) {
console.error('Error during translation sync:', error);
process.exit(1);
}
}
syncTranslations();