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

@rockpack/localazer

v1.9.0

Published

This module can help you organize localization in your React application

Downloads

50

Readme

@rockpack/localazer

Most application localization approaches use JSON files as the storage location. JSON is a convenient format for a developer but not for a localizer. The localizer works in specialized software that must maintain correct spelling, find typos, and combine GIT-style developments between versions of the application to form a dictionary.

The most common approach for localizing applications is gettext. This is a set of localization programs that organize spell checking, merge different versions of application localizations, and delete unnecessary text data. This application has been used by most of the desktop developers since the 90s.

In order to organize the communication of our React application with gettext and back, @rockpack/compiler and @rockpack/localizer can help us.

@rockpack/localazer this module is part of the Rockpack project. See more details on the official site.

Articles

How it works

Stage 1. We need to add localization and make it friends with our application. Stage 2. Extract all the data for the dictionary from our application and pass it in the gettext format to the translator. Stage 3. Having received the finished translation, we must overtake it into JSON and insert it into our application.

Using

  1. Installation:
# NPM
npm install @rockpack/localaser --save
npm install @rockpack/compiler --save-dev

# YARN
yarn add @rockpack/localaser
yarn add @rockpack/compiler --dev
  1. In order for language switching to work correctly, you need to wrap the application in a component
import { LocalizationObserver } from '@rockpack/localaser';

class Root extends Component {
  render() {
    return (
      <LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
        <App/>
      </LocalizationObserver>
    )
  }
}
  1. In the components where you need to translate, you need to add components with the default language:
import Localization, { LocalizationObserver, l, nl, sprintf } from '@rockpack/localaser';

...

<h2><Localization>{l('Hello')}</Localization></h2>

If you want to use variables in translation, you need to use:

<Localization>
  {
    sprintf(
      l('Your name is %s', 'USER'),
      name
    )
  }
</Localization>

For plural forms:

<Localization>
  {
    sprintf(
      nl(
        '%d click',
        '%d clicks',
        count
      ),
      count
    )
  }
</Localization>

As a result, with count = 0, the text will be displayed 0 clicks, with count = 1 - 1 click.

  1. After the text for localization has been added to the application, you need to extract the dictionary with these text fragments.

5.1 Make makePOT.js in the root of project

const { localazer } = require('@rockpack/compiler');

localazer.makePot({
    dist: './po',
    src: './src'
});

Run the script

node makePOT.js

As a result, a dictionary with all text fragments for translation will be created.

To translate a dictionary, you must use POEdit tool:

5.2 After translating the dictionary through POEdit, you need to save the mo file with the created translation. This file must be added to the project. Then it needs to be converted to JSON:

Make po2json.js in the root of project

const { localazer } = require('@rockpack/compiler');

localazer.po2json({
    dist: './json',
    src: './po'
});

Run the script

node po2json.js
  1. When you convert the translated snippets to JSON, you can add them to the component with and create a way to switch the language.
import ru from '../json/ru.json';

class Root extends Component {
  constructor(props) {
    super(props);

    this.state = {
      currentLanguage: 'en',
      languages: { ru }
    }
  }

  setCurrentLanguage = (currentLanguage) => {
    this.setState({ currentLanguage })
  }

  render() {
    return (
      <LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
        <App setCurrentLanguage={setCurrentLanguage} />
      </LocalizationObserver>
    )
  }
}

@rockpack/localazer is not responsible for passing translations to the app. You can do this of your choice, for example through dynamic imports, backend API, Redux, Local Storage, etc.

An example of receiving converted transfers via the Backend API:

class Root extends Component {
  constructor(props) {
    super(props);

    this.state = {
      currentLanguage: 'en',
      languages: {}
    }
  }

  componentDidMount() {
    this.getLanguages();
  }

  getLanguages = () => {
    fetch('http://localhost:8000/api/languages')
      .then(response => {
        return response.json();
      })
      .then(({ languages }) => {
        this.setState({ languages });
      });
  }

  setCurrentLanguage = (currentLanguage) => {
    this.setState({ currentLanguage })
  }

  render() {
    return (
      <LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
        <App setCurrentLanguage={setCurrentLanguage} />
      </LocalizationObserver>
    )
  }
}

Properties

  • <LocalizationObserver /> properties:

| Property | Type | Description | | --- | --- | --- | | currentLanguage | String | Set active language | | defaultLanguage | String['en'] | Default language | | languages | Object | Object with JSON translations |

Q&A

How do I use gettext on Windows?

  • It needs to install gettext version no earlier than 0.20 (for supporting JS)
  • You can download the latest gettext Windows version here
  • Before running localazer makePOT, you need to make sure that gettext, xgettext are available in the console
  • To edit PO and POT files, you need to install POEdit

The MIT License

MIT