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

react-easy-i18n

v0.3.1

Published

React i18n made easy and simple

Downloads

25

Readme

react-easy-i18n

Installation

npm install --save react-easy-i18n

Usage

Creating new language bundle

You can create a language bundle by using the registerLang function from the package. You first pass in the language abbreviation, then you pass the translation object :

import { registerLang } from 'react-easy-i18n';

registerLang('en', {
    home: 'Home',
    user: 'User'
});
Nesting language bundles

You can nest language bundle into sub object for easier listing.

import { registerLang } from 'react-easy-i18n';

registerLang('en', {
    home: 'Home',
    user: {
        firstname: 'Firstname',
        config: {
            language: 'Language'
        }
    }
});

And then pass in a slug splitted by dot to your <Text/> component like :

<Text text="user.config.language"/> //  Would output "Language"

Switching locales

You can switch locale with the setCurrentLocale function. Make sure you already registered the language or it'll fallback to english

import { registerLang, setCurrentLocale } from 'react-easy-i18n';

registerLang('en', {
    home: 'Home',
    user: 'User'
});

setCurrentLocale('en');

Usage in React

A component Text is included with the package which handles formatting and translations. You must at least provide the text prop which is the key of your translation.

import React from 'react';
import { Text } from 'react-easy-i18n';

const SuperReactCompoenent = () => {
    return (
        <div>
            <Text text="welcome" />
        </div>
    );
};

The Text component will render your translation of "welcome" inside spans.

You can also provide 2 other props : params and formatters

Advanced usage

Parameters

You may require to parameterize string instead of concatenate them. You can do so by providing your parameter key with a colon (:) inside you string. For instance, I want to say Hi to my users.

import React from 'react';
import { registerLang, Text } from 'react-easy-i18n';

registerLang('en', {
    welcome: 'Hi :fullname'
});

const SuperReactCompoenent = () => {
    const fullname = 'John Doe';
    const params = {
        fullname
    };
    return (
        <div>
            <Text text="welcome" params={params}/>
        </div>
    );
};

The rendered text will be Hi John Doe.

Formatters

You may want to format text sometimes. It helps you keep your translation base clean and reusable because you will be formatting it on runtime.

First, you must register your formatter with the registerFormatters helper. It simply takes and object in parameter with the function you use as formatters

import { registerFormatters } from 'react-easy-i18n';

registerFormatters({
    uppercase: text => text.toUpperCase(),
    exclamation: text => text + '!',
});

And then you pass a string with all the formatters you want splitted by pipes

import React from 'react';
import { Text, registerFormatters } from 'react-easy-i18n';

registerLang('en', {
    welcome: 'Hi :fullname'
});

registerFormatters({
    uppercase: text => text.toUpperCase(),
    exclamation: text => text + '!',
});

const SuperReactCompoenent = () => {
    return (
        <div>
            <Text text="welcome" formatters="uppercase|exclamation"/>
        </div>
    );
};
With parameters

It would render HI JOHN DOE!. It may end up with the need to parameterize these formatters. Stay safe, we did it for you. You can, after you formatter key, add a colon (:) and pass in parameters splitted by commas

I want a formatter to surround a text with things. Sometimes it'll be parentheses, sometimes brackets.

import React from 'react';
import { registerFormatters, registerLang, Text } from 'react-easy-i18n';

registerLang('en', {
    home: 'Home'
});

registerFormatters({

    // args is the array of things after the colon
    surround: (text, args) => args[0] + text + args[1]
});

const SuperReactCompoenent = () => {
    return (
        <div>
            <Text text="welcome" formatters="surround:(,)"/>
            <Text text="welcome" formatters="surround:[,]"/>
        </div>
    );
};

The first component will render (Home) and the second will render [Home]