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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-tr

v1.2.0

Published

Translation manager for react apps.

Readme

Install

$ npm install react-tr

Import

// use TranslateManager in language changing handler
import { TranslationManager } from 'react-tr'
// use tr in translatable components
import { tr } from 'react-tr'

Usage

The minimum set of functions You need for translating is TranslationManager.set and tr, but it's strongly recommended to use TranslationManager.setParent as well.

Management

TranslationManager.set(src, handler=undefined)
// put this code in language changing handler
// for example:
handleEngClick = () => {
    TranslationManager.set('my.domain.com/localization/eng.json', (flag) => {
        console.log('Was dictionary loaded from src?', flag);
    })
};
handleRusClick = () => {
    TranslationManager.set('my.domain.com/localization/rus.json', (flag) => {
        console.log('Was dictionary already loaded before?', (!flag));
    })
};
TranslationManager.setParent(parent)
// put this code in component, you want to update, after dictionary loading
// for example:
class App extends Component {
    constructor(props) {
        super(props);
        TranslationManager.setParent(this);
    }
    
    // ...
}
TranslationManager.setDefaultFunction(function)
// put this code somewhere before rendering translatable components
// this will change default function (by default it returns text as it is)
// for example:
class App extends Component {
    constructor(props) {
        super(props);
        TranslationManager.setDefaultFunction((text) => { return '' });
    }
    
    // ...
}
// with this code, all unknown sentences will become empty strings
TranslationManager.loadDictionary(handler=undefined)
// you can use loadDictionary if you want to force reloading from src
// for example:
handleThatServerToldThatHisDictionaryHasBeenChanged = () => {
    TranslationManager.loadDictionary((flag) => {
        console.log('Was dictionary updated?', flag);
    })
};
TranslationManager.generateDictionary(useUnmetWords=false, saveToFile=true)
// every time tr was called, it save the word to usedWords dictionary
// call generateDictionary, if you want to save file with all the words, you have met on your site
handleSpecialClick = () => {
    TranslationManager.generateDictionary();
};
// if you don't want to save file, or if you want to use not encountered words,
// that were presented in your downloaded dictionary, you can change corresponding flags:
handleSpecialClick = () => {
    console.log('This is my dictionary: ' + JSON.parse(TranslationManager.generateDictionary(true, false)));
};
// this function returns dictionary with encountered words as keys and translations for this keys (or '') as values

Translating

Use this in your components:

// just put your text in tr:
<div>{ tr('This is the first text') }</div>
<div>{ tr('This is the second text') }</div>
<div>{ tr('This is the first text') }</div>

Then create .json files and put them somewhere on your server (for example: /var/www/html/localization/)

Example of rus.json file:

{
    "This is the first text": "Этот текст написан на великом и могучем",
    "This is the second text": "Этот текст написан на могучем и великом"
}

Example of eng.json file:

{
    "This is the first text": "This is first text in english",
    "This is the second text": "This is second text in english"
}

If text is not presented in the dictionary or .json files are unreachable or can't be parsed by JSON.parse, text will be passed out of tr function by default function. For example, code below will show you "This is the third text, and it is not presented in the dictionary" (if default function was not changed by TranslationManager.setDefaultFunction)

<div>{ tr('This is the third text, and it is not presented in the dictionary') }</div>