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-switch-lang

v1.0.2

Published

React Multilanguage Higher-Order Component

Downloads

828

Readme

react-switch-lang

React Multi-language Higher Order Component with cookie support.

Installation

npm install react-switch-lang --save

Usage

en.json

{
  "home": {
    "title": "Homepage"
  },
  "hello": "Hello {name}!",
  "fallback": "Fallback message"
}

th.json

{
  "home": {
    "title": "หน้าแรก"
  },
  "hello": "สวัสดี {name}!"
}

SomeComponent.js

import React from 'react';
import PropTypes from 'prop-types';

// Translation Higher Order Component
import {
  setTranslations,
  setDefaultLanguage,
  setLanguageCookie,
  setLanguage,
  translate,
} from 'react-switch-lang';
import en from 'en.json';
import th from 'th.json';

// Do this two lines only when setting up the application
setTranslations({ en, th });
setDefaultLanguage('en');

// If you want to remember selected language
setLanguageCookie();

class SomeComponent extends React.Component {
  handleSetLanguage = (key) => () => {
    setLanguage(key);
  };

  render() {
    const { t } = this.props;
    return (
      <div>
        {t('home.title')}
        {t('home.title', null, 'th')}
        {t('hello', { name: 'World' })}
        {t('fallback')}

        <button type="button" onClick={this.handleSetLanguage('th')}>
          Switch language
        </button>
      </div>
    )
  }
}

SomeComponent.propTypes = {
  t: PropTypes.func.isRequired,
};

export default translate(SomeComponent);

Injected Method

If using the Higher Order Component translate(SomeComponent)

t(path, params, lang)

Get text function, will return the translated string

Params | Type | Description ------ | ------ | ------------------------------------------------------------------------------------ path | string | translation path that identifies the text params | object | (optional) {'param': 'value', ...} each param will be set on the string in its correct location lang | string | (optional) force select language with translation key, in this example 'en' or 'th'

Exported Methods

setDefaultTranslations(translations)

Sets the translations

Params | Type | Description ------------ | ------ | ---------------------------- translations | object | { "en": { "hello": "Hello" } }

setTranslations(translations)

Same as setDefaultTranslations, but this will update all components using translations

Params | Type | Description ------------ | ------ | ---------------------------- translations | object | { "en": { "hello": "Hello" } }

setDefaultLanguage(key)

Set the default application language, when current language does not have translation text method t() will fallback with this default language Note: this method will set current language but not update components

Params | Type | Description ------ | ------ | --------------------------------------------- key | string | translation key, in this example 'en' or 'th'

setLanguage(key)

Set current language, this will update all components using translations

Params | Type | Description ------ | ------ | --------------------------------------------- key | string | translation key, in this example 'en' or 'th'

setLanguageCookie(name, option, reqCookie)

Set the language cookie name, will setLanguage form this cookie and store language key back to cookie when call setLanguage

Params | Type | Default | Description ------ | ------ | ------ | --------------------------------------------- name | string | 'language' | (optional) name of cookie to store in browser option | object | { path: '/', maxAge: 157680000 } | (optional) cookie option base on "universal-cookie", default age is 5 years reqCookie | string | undefined | (optional) the express cookie header (req.headers.cookie), this is required if you use server-side rendering such as Next.js

getLanguages()

Return all translations key. Example: ['en', 'th']

getDefaultLanguage()

Return the default fallback language

getLanguage()

Return the current selected language

t(key, params, lang)

Same as t() method above