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-i18loader

v1.0.2

Published

An i18n solution for React (Next.js) with auto generated lanuage file and modularization

Downloads

19

Readme

react-i18loader

react-i18loader is a webpack loader for React (or React framework, e.g. Next.js) i18n(Internationalization) solution.

Documentation in Chinese 中文文档

How it work?

  1. Extract the sentences to be translated from the target file.
  2. Generate a JSON file corresponding to target file and fill in the extracted sentences.
  3. Loader will hanler the target file to import this JSON file and add a method to retrieve the translations.
  4. Change the $lang in props (You can change it in state or get the value by function, depends on your webpack config) to change the APP language.

Usage

Simple usage

Set Up

First install it:

npm install react-i18loader --save-dev

Configure webpack

{
    test: /\.(js|jsx)$/, // this loader will generate *.lang.json beside *.jsx files
    exclude: [
        /(node_modules)|(\.next)/,
    ],
    use: {
        loader: "react-i18loader",
        options: {
            // The folder where store the language json file.
            // If storePath value is null or empty, the language json file would store corresponding to target js/jsx file
            storePath: "locales",
            languages: ["zh_Hans_CN", "zh_Hant_HK", "en_US"], // The langauages that you App supported
        }
    }
}

Put label @i18n("Page_Or_Component_Name") on your pages or components

hello.js

import React, { Component } from "react";

@i18n("Hello")
export default class Hello extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    const msg = "我是简体";
    return <div>
      <p>你好,欢迎使用react-i18loader,发现代码之美</p>
      <p>{msg}</p>
    </div>
  }
}
  • You need to change the $lang in props to change the language

index.js

import React, { Component } from "react";
import Hello from "../components/hello";

export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      lang: "zh_Hans_CN"
    };
  }

  changeLang(lang) {
    this.setState({lang});
  }

  render() {
    return <div>
      <button onClick={this.changeLang.bind(this, "zh_Hans_CN")}>简体中文</button> 
      <button onClick={this.changeLang.bind(this, "zh_Hant_HK")}>繁體中文</button>
      <button onClick={this.changeLang.bind(this, "en_US")}>English</button>
      <Hello $lang={this.state.lang}/>
    </div>
  }
}

Advance usage

Configure webpack for more customization

{
    // this loader will generate *.lang.json beside *.jsx files
    test: /\.(js|jsx)$/,
    exclude: [
        /(node_modules)|(\.next)/,
    ],
    use: {
        loader: "react-i18loader",
        options: {
            // The folder where store the language json file.
            // If storePath value is null or empty, the language json file would store corresponding to target js/jsx file
            storePath: "locales",

            // Required fields.
            // The langauages that you App supported
            languages: ["zh_Hans_CN", "zh_Hant_HK", "en_US"],


            // Optional fields.
            // The target component object where the loader will put a filed "$lang" in.
            method: "props", // It could be these three values: props, state, func 
            // The content to be translated. And the content must accord with this RegEx, default value is Chinese Simplified
            regText: "[\u4e00-\u9fa5\u3002\uff1b\uff0c\uff1a\u2018\u2019\u201c\u201d\uff08\uff09\u3001\uff1f\uff01\ufe15\u300a\u300b]+",
        }
    }
}

Property Of options

storePath

Indicate the folder where store the language json file. If storePath value is null or empty, the language json file would store corresponding to target js/jsx file.

languages

It should be an array which could contain the target language. You can also add Ja_JP, In_In ,bala and so on to this array.

regText

Based on the regular text regText, you can extract the matching text as the translation source.

So if you want to translate Chinese into other languages,you should set:

regText: "[\u4e00-\u9fa5\u3002\uff1b\uff0c\uff1a\u2018\u2019\u201c\u201d\uff08\uff09\u3001\uff1f\uff01\ufe15\u300a\u300b]+"

method

Property method could be these three values: props, state, func. Its default value is props.

value | description |
-|-| props | An property name $lang would add to the props of React component, so you need to change the props.$lang to change the web language. For more information, please refer the set_lang_via_props in examples folder | state | An property name $lang would add to the state of React component, so you need to change the state.$lang to change the web language. For more information, please refer the set_lang_via_state in examples folder | func | You need to add a method $getLang() {return XXX; // The language code like 'zh_Hans_CN' to your React component}. For more information, please refer the set_lang_via_func in examples folder|

They are using react-i18loader

SamYoc API Doc | WinterOK| :---: | :---: |