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

@jupri-lab/react-i18n

v2.1.4

Published

Speak Easy. Effortless Internationalization for Your React Apps

Downloads

3

Readme

JupriLab React i18n

react-i18n is a flexible and powerful library designed to simplify internationalization in React applications. It leverages a core i18n library to provide a seamless experience for managing translations and switching between languages in your React app.

Installation

To install react-i18n, you need to install React adapter:

$ npm install @jupri-lab/react-i18n

Usage

Adding the Provider

To use react-i18n, you need to wrap your application with the I18nProvider component. This component will provide the i18n instance to the rest of your application.

Adding Configs to Provider

Create your i18n configuration object and pass it to the I18nProvider.

import React from "react";
import I18nProvider from "@jupri-lab/react-i18n";
import type { Ii18nConfigs } from "@jupri-lab/i18n-core";

type Languages = "en" | "es";
type Resources = {
  greeting: string;
};

const i18nConfigs: Ii18nConfigs<Languages, Resources> = {
  languages: ["en", "es"],
  resources: {
    en: { greeting: "Hello" },
    es: { greeting: "Hola" },
  },
  enableQueryParams: true, // Enable persisting language via query params
  queryParam: "lang", // Default is 'lang', you can customize it
};

const App: React.FC = () => (
  <I18nProvider configs={i18nConfigs}>{/* Your app components */}</I18nProvider>
);

export default App;

Using useI18n and TypeScript Usage

The useI18n hook provides access to translation functions and language management utilities. Here's an example of how to use it in a component:

import React from "react";
import { useI18n } from "@jupri-lab/react-i18n";

const Greeting: React.FC = () => {
  const { translate } = useI18n<Languages>();

  return <p>{translate("greeting")}</p>;
};

export default Greeting;

Changing Language Using getChangeLanguageHandler

You can change the current language using the getChangeLanguageHandler function provided by the useI18n hook.

import React from "react";
import { useI18n } from "@jupri-lab/react-i18n";

const LanguageSwitcher: React.FC = () => {
  const { getChangeLanguageHandler } = useI18n<Languages>();

  return (
    <div>
      <button onClick={() => getChangeLanguageHandler("en")}>English</button>
      <button onClick={() => getChangeLanguageHandler("es")}>Spanish</button>
    </div>
  );
};

export default LanguageSwitcher;

Get Current Language Using getCurrentLanguage

To get the current language, you can use the getCurrentLanguage function provided by the useI18n hook.

import React from "react";
import { useI18n } from "@jupri-lab/react-i18n";

const CurrentLanguage: React.FC = () => {
  const { getCurrentLanguage } = useI18n<Languages>();

  return <p>Current Language: {getCurrentLanguage()}</p>;
};

export default CurrentLanguage;

Persisting Language via Query Parameters

By enabling the enableQueryParams option in the configuration, the selected language will be persisted via query parameters in the URL. This ensures that the language setting is maintained across page reloads and can be shared via URL.

To enable this feature, set enableQueryParams to true and optionally specify a custom queryParam name in your configuration.

const i18nConfigs: Ii18nConfigs<Languages, Resources> = {
  languages: ["en", "es"],
  resources: {
    en: { greeting: "Hello" },
    es: { greeting: "Hola" },
  },
  enableQueryParams: true, // Enable persisting language via query params
  queryParam: "lang", // Default is 'lang', you can customize it
};

Core Features

  • Flexible Configuration: Easily configure supported languages and resources.
  • Type Safety: Fully typed to leverage TypeScript for safety and autocomplete.
  • Translation Functions: Use the translate function to get translated strings.
  • Language Switching: Seamlessly switch between languages using getChangeLanguageHandler.
  • Current Language: Access the current language with getCurrentLanguage.
  • HTML Escaping: Built-in support for escaping HTML in translations.
  • Interpolation: Support for string interpolation in translations.