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

@cpro-js/react-i18n

v0.3.0

Published

Preconfigured i18n system for react apps to remove boilerplate code.

Downloads

8

Readme

@cpro-js/react-i18n

Preconfigured i18n system for react apps to remove boilerplate code.

Uses i18next under the hood to handle translations and to detect the current language. Uses Intl API for date and number formatting.

Installation

$ yarn add @cpro-js/react-i18n

Setup

Configure internationalization by providing a fallbackLocale and a method to retrieve a message bundle based on its language (and / or bundle name, see namespaces): getTranslations.

The library used (i18next) will load message bundles in order of specificity, e.g. you provide "en-GB" then getTranslations will be called twice: 1) "en-GB" and 2) "en". Thus, you can bundle common English translations in the "en"-bundle while providing only specific, diverging translations for "en-GB".

To improve loading of message bundles, provide the locales of the maintained message bundles: maintainedTranslations. Your implementation of getTranslation will now only be called with maintained translations.

Locale resolution can either be set explicitly by you (providing the locale string or a function which returns the locale string) or determined automatically. If the localeResolver is undefined, then the language will be detected by query parameter language or by the navigator property of the browser. To configure the detection options provide them via the localeResolver property (see Language Detector).

The property supportedFormattingLocales is used to restrict the locale used for formatting dates and numbers. Without specifying this property you allow every locale, quite independently of the used locale for text translations (which is always restricted and has its specific fallback logic). To restrict to a language, but allowing for any sub-language, a special syntax is used, e.g. "en-*" to allow all possible locales for English.

import { Container } from "@cpro-js/react-di";
import { I18nService, createI18nModuleRegistry } from "@cpro-js/react-i18n";

const container = new Container();

// i18n
await container.loadAsync(
  createI18nModuleRegistry({
    debug: true,
    fallbackLocale: "en",
    getTranslations: language => import(`../asset/locale/${language}.i18n.json`),
    maintainedTranslations: ["en", "de"],
    localeResolver: "de-DE",
    supportedFormattingLocales: ["en-*", "de-*"],
  })
);

if (module.hot) {
  const i18nService = container.get(I18nService);
  const id = ((module.children as any) as Array<string>).find(mod => (mod as any).indexOf(".i18n\\.json") !== -1);
  if (id) {
    module.hot.accept(id, () => {
      i18nService.useLocale(i18nService.getLocale());
    });
  }
}

Usage

As class:

// ...
import { observer, I18nService } from "@cpro-js/react-core";

@observer
class App extends Component<{}> {
  @inject(I18nService) private i18nService!: I18nService;

  render() {
    return (
      <div>
        {this.i18nService.t("hello.world")}
      </div>
    );
  }
}

Or as function component:

import { observer, useInjection } from "@cpro-js/react-core";

export const App: FC = observer(() => {
  const { t } = useInjection(I18nService);

  return (
    <div>
      {t("hello.world")}
    </div>
  );
})