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

@rgglez/astro-i18n-gettext

v0.1.1

Published

Translations (i18n) for Astro using gettext: per-request translator, <T> component, and client-side language switching.

Readme

astro-i18n-gettext

License

gettext translations for Astro: a per-request translator for frontmatter, a <T> component, and client-side language switching without reloading. Built on @rgglez/i18n-gettext, the same core as svelte-i18n-gettext and react-i18n-gettext, so the same .json catalogs work everywhere. Islands written in Svelte or React can use those packages with the same catalogs.

Install

npm install @rgglez/astro-i18n-gettext

Usage

1. Register the catalogs

Create a module and import it from your layout, so it runs before any page renders:

// src/i18n/index.ts
import { defineGettext } from "@rgglez/astro-i18n-gettext";
import es from "./es/messages.json";
import fr from "./fr/messages.json";

export const catalogs = { es, fr };
defineGettext({ catalogs, defaultLang: "en" });

2. Translate on the server

---
import "../i18n";
import { sprintf, useGettext } from "@rgglez/astro-i18n-gettext";
import T from "@rgglez/astro-i18n-gettext/T.astro";

const { _, _n } = useGettext(Astro);
---

<title>{_("Blog")}</title>
<p>{sprintf(_n("One post", "%d posts", count), count)}</p>
<h2><T msgid="Recent posts" /></h2>
<a href="/"><T msgid="Home" ctx="nav" /></a>

useGettext(Astro) picks the language from Astro.currentLocale (Astro's i18n routing), then from a [lang] route parameter, then defaultLang. It creates a translator per call, so concurrent server requests never share a language. Static and server output both work.

<T> renders the translated text inside <span data-msgid data-msgctxt>. The HTML already contains the translation; the attributes only matter for client-side switching. <T> handles singular strings; use _n in the frontmatter for plurals.

3. Switch language on the client (optional)

Astro components render once on the server. To change language without navigating, re-translate the <T> elements from a client store:

<select id="lang">
  <option value="es">Español</option>
  <option value="fr">Français</option>
</select>

<script>
  import { bindGettext, createGettextStore } from "@rgglez/astro-i18n-gettext";
  import { catalogs } from "../i18n";

  const html = document.documentElement;
  const store = createGettextStore({ lang: html.lang, catalogs });
  bindGettext(document, store);
  document.querySelector("#lang")!.addEventListener("change", event => {
    html.lang = (event.target as HTMLSelectElement).value;
    store.setLang(html.lang);
  });
</script>

Only <T> elements (anything with data-msgid) change. Strings translated in frontmatter with _() stay in the server-rendered language, and switching on the client ships every imported catalog to the browser.

API

| Export | Where | Description | | :----- | :---- | :---------- | | defineGettext({ catalogs, defaultLang }) | server | Registers catalogs; returns useGettext | | useGettext(Astro) | server | { _, _n, lang, hasTranslations } for the request | | T.astro | server | <T msgid ctx?> translated, tagged <span> | | createGettextStore, bindGettext(root, store) | client | Live re-translation of [data-msgid] | | createTranslator, sprintf, vsprintf | both | Re-exported from the core |

_(msgid, msgctxt = "app")
_n(msgid, msgidPlural = "", count = 0, msgctxt = "app")

Catalogs

Catalogs are gettext .po files converted to gettext-parser JSON with gettext-po2json and the app context. xgettext does not parse .astro files; extract from the frontmatter and <T msgid="..."> with gettext-extractor (regex support), or keep translatable strings in .ts modules and use xgettext -L TypeScript --keyword=_ --keyword=_n:1,2.

Development

bun install
bun test src
bun run build

Live example

The component can be viewed live here.

You can run it locally from the example directory.

License

Copyright (c) 2026 Rodolfo González González.

Licensed under the Apache-2.0 license. Read the LICENSE file.