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

@bablic-io/selector

v1.0.1

Published

Build your own Bablic language selector: list a project's languages and switch between them.

Readme

@bablic-io/selector

Build your own language selector for a website translated with Bablic.

It provides two functions:

  • getLanguages() returns your project's languages and tells you which one the visitor is viewing.
  • setLanguage(value) switches the visitor to another language, using the same mechanism as Bablic's built-in selector (cookie, URL parameter, folder, domain, or browser storage, depending on your project's setup).

You build the selector UI however you like.

The library has no dependencies, is about 7 kB minified, and makes one small request per page.

You still need the Bablic script. This library only lists the languages and switches between them. Bablic's translation script, the snippet from your Bablic dashboard, is what actually translates the page. Keep it on your site: without it, setLanguage() still switches the language, but the page stays untranslated. The selector works independently of the Bablic script, so you can load the two in any order.

Option 1: script tag

Add the script with your project key, then build your selector:

<script src="https://edge.bablic.com/selector.min.js" data-bablic="YOUR_PROJECT_KEY"></script>

<select id="language-selector"></select>

<script>
  const select = document.getElementById('language-selector');

  BablicSelector.getLanguages().then((languages) => {
    languages.forEach(({ value, label, isCurrent }) => {
      const option = document.createElement('option');
      option.value = value;
      option.textContent = label;
      option.selected = isCurrent;
      select.appendChild(option);
    });
  });

  select.addEventListener('change', (e) => {
    BablicSelector.setLanguage(e.target.value);
  });
</script>

Where to put the script tag:

  • Before your own code: selector.min.js must come before any script that uses BablicSelector. We recommend putting it in the <head>.
  • No async: with async, your code may run before BablicSelector exists. defer works only if the code that uses BablicSelector is deferred too, or waits for DOMContentLoaded.
  • type="text/javascript" is optional: browsers treat scripts as JavaScript by default. It does no harm if your CMS adds it.

Option 2: npm package

npm install @bablic-io/selector
import { useEffect, useState } from 'react';
import { getLanguages, setLanguage } from '@bablic-io/selector';

const PROJECT_KEY = 'YOUR_PROJECT_KEY';

function LanguageSelector() {
  const [languages, setLanguages] = useState([]);

  useEffect(() => {
    getLanguages(PROJECT_KEY).then(setLanguages);
  }, []);

  const current = languages.find((language) => language.isCurrent);

  return (
    <select value={current?.value} onChange={(e) => setLanguage(e.target.value)}>
      {languages.map(({ value, label }) => (
        <option key={value} value={value}>{label}</option>
      ))}
    </select>
  );
}

The package is safe to import during server-side rendering. Call getLanguages() in the browser only, for example inside useEffect.

API

getLanguages(projectKey, options?) → Promise<Language[]>

  • projectKey: your Bablic project key. With the script tag, it comes from data-bablic, so BablicSelector.getLanguages() takes no arguments.
  • options.host: the Bablic server to use. The default is https://edge.bablic.com/. With the script tag, it is the server selector.min.js was loaded from.

Each language is:

| Field | Type | Description | |-------------|-----------|-------------| | value | string | Identifies the language. Pass it to setLanguage(). | | label | string | Display name: your custom label, or the language's native name. | | langCode | string | Language code, e.g. FR-FR. | | isDefault | boolean | true for your site's original language. | | isCurrent | boolean | true for the language the visitor is viewing. |

  • Order: languages are sorted by label.
  • Single request: the languages are loaded once per page; later calls reuse them.
  • Empty list: the promise resolves to [] (and logs a warning) when the project isn't available: it isn't published yet, translations are turned off, or your site's domain isn't allowed for the project.
  • Errors: the promise rejects only on network or server errors.

NOTE: You can edit value and label using the Bablic dashboard app at https://dashboard.bablic.com/ .

setLanguage(value)

Switches the visitor to the language with this value. value must be one of the values returned by getLanguages(). Depending on your project's setup, it does one of the following:

  • Cookie: sets the language cookie and reloads the page.
  • URL parameter: updates the language parameter in the address and navigates.
  • Folder: replaces the language folder in the path (e.g. /fr/page → /de/page) and navigates.
  • Domain: moves to the language's domain, keeping the same page.
  • Browser storage: stores the language and reloads the page.

Previewing languages that aren't live yet

Open your page with ?bablic_preview=1. getLanguages() then also returns languages that are still in preview, and works on projects that aren't published yet.