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

reacti18n-translate

v1.0.8

Published

A context wrapper for your react application to simplify internationalization and localization.

Downloads

18

Readme

Travis npm package

Reacti18n-translate is a wrapper for your react application that simplifies internationalization.

Follow the step-by-step instructions below:

Install

npm i reacti18n-translate
# or
yarn add reacti18n-translate

CDN

<script src="https://unpkg.com/reacti18n-translate/umd/reacti18n-translate.js"></script>

In your src directory, create another directory for your translations

  1. Create a .json file for each supported lanuage in this directory ( i.e. en.json, es.json).
  2. Create an index.js file inside this directory to export your supported languages as a JavaScript object.
// index.js file
import EN from './en.json';
import ES from './es.json';
import DE from './de.json';

const translations = {
  en: EN,
  es: ES,
  de: DE
};

export default translations;

Wrap your appliction with Reacti18nProvider, while also providing a default language, and your translations. The defaultLanguage prop is optional, 'en' will be used if defaultLanguage is not provided.

// In your application file
import React from 'react';
import { Reacti18nProvider } from 'reacti18n-translate';
// Import your translations
import translations from './i18n';

<Reacti18nProvider translations={translations} defaultLanguage="en">
  <App />
</Reacti18nProvider>;

React-i18n-translate provides a translate function that you can destructure from useReacti18n to use when translating content. Pass in a string that represents the key from your translation file. Current support is for objects with a maximum depth level of 3.

// In your translation file en.json
{
  "example": "example",
  "common": {
    "form": {
      "submit": "submit"
    }
  }
}
// In your component file
import React from "react";
import { useReacti18n } from "reacti18n-translate";

const ExampleComponent = () => {
    const { translate } = useReacti18n();
    return (
        <div>
            <p>Translated content: {translate('example')}</p>
            <p>Translated content: {translate('common.form.submit')}</p>
        </div>
    );
};
...

React-i18n-translate also provides a dispatch function that you can destructure (as the second argument if also using translate) from useReacti18n to set the language you want to use. It accepts a type 'setLanguage', and a payload which represents the desired language code.

// In your component file
import React from "react";
import { useReacti18n } from "reacti18n-translate";
const ExampleComponent = () => {
    const { translate, dispatch } = useReacti18n();

    const handleLanguageChange = e =>
    dispatch({ type: "setLanguage", payload: e.target.value  });

    return (
        // Example using a select
       <select onChange={handleLanguageChange}>
        <option value="en"></option>
        <option value="es"></option>
       </select>

       // Example using a button
    <button onClick={() => dispatch({ type: "setLanguage" payload: "en" })}>
        EN
      </button>
        </div>
    );
};
...

Congratulations! Your react application is now internationalized. :sweat_smile: