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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@celseven/polyglot-js

v1.1.0

Published

JavaScript client for Polyglot API

Readme

Polyglot JS

A JavaScript client for interacting with the Polyglot API. This library provides a simple and intuitive interface for managing translations, languages, and spaces in your Polyglot projects.

Installation

npm install polyglot-js

Usage

Initialize the client

import { PolyglotClient } from "polyglot-js";

const client = new PolyglotClient({
  organizationSlug: "your-organization",
  apiKey: "your-api-key",
});

Working with Languages

// Get all languages
const languages = await client.getLanguages();

// Create a new language
const newLanguage = await client.createLanguage({
  name: "Spanish",
  code: "es",
  isDefault: false,
  isAutoTranslatable: true,
});

// Update a language
await client.updateLanguage("language-id", {
  name: "Spanish (Updated)",
  isAutoTranslatable: false,
});

Working with Spaces

// Get all spaces
const spaces = await client.getSpaces();

// Create a new space
const newSpace = await client.createSpace({
  name: "Website",
  description: "Translations for our website",
});

Working with Translations

// Get all translations for a language and space
const translations = await client.getTranslations({
  languageId: "language-id",
  spaceId: "space-id",
});

// Get a specific translation
const translation = await client.getTranslation({
  languageId: "language-id",
  spaceId: "space-id",
  key: "welcome.message",
});

// Create or update a translation
await client.setTranslation({
  languageId: "language-id",
  spaceId: "space-id",
  key: "welcome.message",
  value: "Welcome to our application!",
});

// Update multiple translations at once
await client.updateTranslations({
  languageId: "language-id",
  spaceId: "space-id",
  translations: {
    "welcome.message": "Welcome to our application!",
    "goodbye.message": "Thank you for using our application!",
  },
});

Using the Translator

The PolyglotTranslator provides a convenient way to load and use translations in your application:

import { PolyglotClient, PolyglotTranslator } from "polyglot-js";

// Create a client
const client = new PolyglotClient({
  organizationSlug: "your-organization",
  apiKey: "your-api-key",
});

// Create a translator
const translator = new PolyglotTranslator({
  client: client,
  defaultLocale: "en",
  autoLoadSpaces: true, // Automatically load spaces when translating
});

// Load a space manually
await translator.loadSpace("common");

// Basic translation
translator.t("common.hello"); // "Hello"

// Translation with placeholder
translator.t("common.welcome", {
  replace: { name: "John" },
}); // "Welcome, John"

// Translation with pluralization
translator.t("common.items", { n: 0 }); // "No items"
translator.t("common.items", { n: 1 }); // "1 item"
translator.t("common.items", { n: 5 }); // "5 items"

// Change locale
await translator.setLocale("fr");

// Get current locale
const currentLocale = translator.getLocale(); // "fr"

Pluralization

The translator supports pluralization with the following format:

"No items|One item|{number} items"

Or the simpler format:

"One item|{number} items"

Placeholders

You can use placeholders in your translations:

"Welcome, {name}!"

And replace them when translating:

translator.t("common.welcome", {
  replace: { name: "John" },
}); // "Welcome, John!"

Error Handling

The client throws descriptive errors when API requests fail:

try {
  await client.getLanguages();
} catch (error) {
  console.error("API Error:", error.message);
  console.error("Status Code:", error.statusCode);
}

Advanced Configuration

const client = new PolyglotClient({
  baseUrl: "https://your-api-url.com",
  organizationSlug: "your-organization",
  apiKey: "your-api-key",
  timeout: 10000, // 10 seconds
  retries: 3, // Retry failed requests 3 times
});

Development

This package is built using Rolldown, a Rust-based JavaScript bundler that's compatible with Rollup's API but offers significantly faster build times.

Building the Package

To build the package, run:

npm run build

Running Tests

This package uses Vitest for testing. Vitest is a modern test runner that's fast and has a Jest-compatible API.

To run the tests:

# Run tests once
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage

The test suite includes comprehensive tests for:

  • The base client functionality
  • Enhanced client with validation and caching
  • Validation utilities
  • Cache implementation

Current test coverage for source files is over 95%, ensuring high reliability of the codebase.

License

MIT