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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wluwd/t-react

v0.0.2

Published

> Seamless React integration for [`@wluwd/t`](https://github.com/wluwd/t) 🌐

Downloads

754

Readme

t-react

Seamless React integration for @wluwd/t 🌐

Install

Using your favorite package manager, install: @wluwd/t-react @wluwd/t-utils.

[!NOTE] You're not required to install @wluwd/t-utils, but we recommend it as a helpful starting point for additional utilities and features.

The React adapter has two peer dependencies which you need to install: the first is react itself, the second is jotai.

[!NOTE] We will likely transition from Jotai to Nano Stores once use lands in a stable release.

Usage

// translations.ts
import { defineTranslationsConfig } from "@wluwd/t-react";
import { browser, lazyTranslations, formatter } from "@wluwd/t-utils";

export const {
	setLocale,
	getLocale,
	useLocale,
	getTranslations,
	useTranslations,
	t,
} = defineTranslationsConfig(
	{
		"en-US": lazyTranslations(() => import("./en-us.ts")),
		"it-IT": lazyTranslations(() => import("./it-it.ts")),
	},
	{
		formatter,
		localeFrom: [browser(), "en-US"],
	},
);
// article/published-by.tsx
import { useTranslations, t } from "../translations.ts";

const PublishedBy = ({ name }) => {
	const heading = useTranslations("article.heading");

	return <span>{t(heading.publishedBy, { name })}</span>;
};

API

defineTranslationsConfig(loaders, options, lazy?)

Creates the functions and hooks needed to localize your React application.

loaders

Extends: Record<Locale, () => Promise<Translations>>

An object whose keys will be used as the available locales and their values as loaders.

We recommend using the UTS 35 definition of Unicode Locale Identifier for keys as this allows an easier integration with negotiators.

options

  • formatter

    Extends: (translation: string, data: any) => string

    A function that replaces placeholders within a string using values from data.

  • localeFrom

    Extends: [] | [...LocaleNegotiator[], Locale]

    An empty tuple, or a tuple with zero or more LocaleNegotiators and, as the last element, one of loaders' keys.

    The negotiators are evaluated sequentially and the loop stops as soon as one returns a locale.

  • cache

    Extends: Partial<Record<Locale, Translations>>

    An object that can have loaders' keys and the loaded translations for that locale.

    Instead of loading the translations when one of these locales is active, the value from this object will be used.

lazy

Extends: boolean | undefined

When this is set to true, initiates the translator in lazy mode.

Alongside the other functions and hooks, defineTranslationsConfig will return an init function with the following signature:

type Init = (negotiators?: LocaleNegotiators<Locale>) => void;

You have to call this function manually before using any of the other functions.

It's possible to pass a list of locale negotiators that will override the ones provided in options, if no negotiators are provided here, the ones provided in options will be used.

This is useful when a user's preferred locale comes from an HTTP request or a DB query.


Other than the init function described above, defineTranslationsConfig returns the following functions and hooks.

setLocale(locale)

Sets the active locale to the provided one.

getLocale()

A function that returns the active locale.

This function is not reactive and it's not meant for use in client components.

useLocale()

A hook that returns the active locale.

getTranslations(prefix)

An async function that returns a slice of the active translations.

This function is not reactive and it's not meant for use in client components.

useTranslations(prefix)

A hook that returns a slice of the active translations.

This hook triggers a Suspense.

t(translation, data?)

formatter, returned for convenience.