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-native-international

v1.0.1

Published

Flexible internalization of your React Native project with "format-message" library

Downloads

6

Readme

React Native International

Flexible internalization of your React Native project with "format-message" library. By default, it uses the locale of the phone, but it is possible to change the language via the changeLocale method. The format-message library is used for the message format.

Demo

Install

Use npm:

npm i react-native-international

Use yarn:

yarn add react-native-international

Usage

The first step is to add language packs

Step 1. Create language packs

Make separate files for each language.

en.js
export default {
    locale: 'en',
    meta: { // Shown in method getLanguages
        label: 'English',
    },
    translations: {
        hello: 'Hello!',
        hello_with_name: 'Hello {name}!',
    },
}
ru.js
export default {
    locale: 'ru',
    meta: { // Shown in method getLanguages
        label: 'Русский язык',
    },
    translations: {
        hello: 'Привет!',
        hello_with_name: 'Привет {name}!',
    },
}

Step 2. Initialize languages

Open index.js and add languages initialization before app render.

import {AppRegistry} from 'react-native'
import {name as appName} from './app.json'

// Import initialization
import {initialization} from 'react-native-international'

// Add locales
initialization([
    require('./en'),
    require('./ru')
])

AppRegistry.registerComponent(appName, () => App)

If you want to set default fallback language, you can use second argument

initialization([
    require('./en'),
    require('./ru')
], 'ru')

Step 3. Use a hook

Use a webhook useIntl in a component that uses strings.

import React from 'react'
import {View, Text} from 'react-native'
import {useIntl} from 'react-native-international'

export default ({navigation}) => {
    const {
        t, // Instance format-message 
    } = useIntl()

    return (
        <View>
            <Text>{t('hello')}</Text>
            <Text>{t('hello_with_name', {name: 'Smith'})}</Text>
        </View>
    )
}

Change language

There are several helper methods for comfortable working with languages.

const {
    t, // Instance format-message 
    locale, // Current locale
    getLanguages, // Method to get locales with "meta" property from language pack and "selected" flag.
    changeLocale, // Method to change locale
} = useIntl()

Property: locale

Return current locale.

console.log(locale) 
// Return "en"

Method: getLanguages

Get languages method return array.

const languages = getLanguages()
console.log(languages)
// [{
//     locale: 'en',
//     selected: true,
//     label: 'English' // Meta from language pack
// }, {
//     locale: 'ru',
//     selected: false,
//     label: 'Русский язык' // Meta from language pack
// }]

Method: changeLocale

Will change the language. If suddenly the locale was not found, use the fallback locale or the last selected one.

changeLocale('ru')