百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-localization

> 编程语言
开源

一个简单的模块,用于使用 ReactNativeLocalization 模块中使用的相同语法来本地化 React 界面。

371 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个简单的模块,用于使用 ReactNativeLocalization 模块中使用的相同语法来本地化 React 界面。

react-localization

Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module.

React 19 support

Version 2.0 has been recreated to support React 19, removing babel and adding vite tooling.

If you work with previous versions of react you can use version 1.x.

Note about version 1.x

This library has been refactored to use the newly created localized-strings package, now added as a dependency, so to unify the code and make it easier to mantain

All the basic code is now in the localized-strings project but this React version add support for embedding JSX code in the formatted strings, by overriding the formatString method.

How it works

The library uses the current interface language, then it loads and displays the strings matching the current interface locale or the default language (the first one if a match is not found) if a specific localization can't be found.

It's possible to force a language different from the interface one.

Installation

npm install --save react-localization

Usage

In the React class that you want to localize, require the library and define the strings object passing to the constructor a simple object containing a language key (i.e. en, it, fr..) and then a list of key-value pairs with the needed localized strings.

javascript
// ES6 module syntax
import LocalizedStrings from "react-localization";

let strings = new LocalizedStrings({
  en: {
    how: "How do you want your egg today?",
    boiledEgg: "Boiled egg",
    softBoiledEgg: "Soft-boiled egg",
    choice: "How to choose the egg",
  },
  it: {
    how: "Come vuoi il tuo uovo oggi?",
    boiledEgg: "Uovo sodo",
    softBoiledEgg: "Uovo alla coque",
    choice: "Come scegliere l'uovo",
  },
});

Then use the strings object literal directly in the render method accessing the key of the localized string.

javascript

  {strings.how}
;

The first language is considered the default one, so if a translation is missing for the selected language, the default one is shown and a line is written to the log as a reminder.

Update / Overwrite Locale

You might have default localized in the build but then download the latest localization strings from a server. Use setContent to overwrite the whole object.

NOTE that this will remove all other localizations if used.

javascript
strings.setContent({
  en: {
    how: "How do you want your egg todajsie?",
    boiledEgg: "Boiled eggsie",
    softBoiledEgg: "Soft-boiled egg",
    choice: "How to choose the egg",
  },
});

API

  • setLanguage(languageCode) - to force manually a particular language
  • getLanguage() - to get the current displayed language
  • getInterfaceLanguage() - to get the current device interface language
  • formatString() - formats the input string and returns a new string, replacing its placeholders with the other arguments strings
…

Typical usage is to render it in a JSX with formatString calls inlined:

javascript

;

Beware: do not define a string key as formatString!

  • setContent(props) - to dynamically load another set of strings
  • getAvailableLanguages() - to get an array of the languages passed in the constructor

Examples

To force a particular language use something like this:

javascript
_onSetLanguageToItalian() {
  strings.setLanguage('it');
  this.setState({});
}

Typescript support

Because of the dynamically generated class properties, it's a little tricky to have the autocomplete functionality working.

Anyway it's possible to gain the desired results by:

  1. defining an Interface that extends the LocalizedStringsMethods interface and has all the object string's keys
  2. defining that the LocalizedStrings instance implements that interface

This is the suggested solution to work with Typescript:

javascript
export interface IStrings extends LocalizedStringsMethods{
    score:string;
    time: String;
}

public strings: IStrings;
this.strings = new LocalizedStrings({
            it: {
                score: "Punti",
                time: "Tempo"
            },
            en: {
                score: "Score",
                time: "Time"
            }
        });

Questions or suggestions?

Feel free to contact me on Twitter or open an issue.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言