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

v2.0.0-rc4

Published

Flexible i18n package for React Native or Expo based on "i18next"

Downloads

26

Readme

React Native International

Flexible internalization of your React Native project with "i18next" library. The i18next library is used for the message format.

Demo

Install

If your application on Expo:

npx expo install expo-localization

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

For first make directory translations and separate files for each language.

translations/en.ts
export default <LanguagePack>{
    locale: 'en',
    meta: { // Shown in method getLanguages
        label: 'English',
    },
    translations: {
        hello: 'Hello!',
        hello_with_name: 'Hello {{name}}!',
    },
}
translations/ky.ts
export default <LanguagePack>{
    locale: 'ky',
    meta: { // Shown in method getLanguages
        label: 'Кыргызча',
    },
    translations: {
        hello: 'Салам!',
        hello_with_name: 'Салам {{name}}!',
    }
}

Step 2. Initialize languages

Make initiator, for example, translations/_i18t.ts.

import { initialization } from 'react-native-international'
import { getLocales } from "expo-localization";

import enLang from './en'
import kyLang from './ky'

const localeFromPhone = async () => {
    // You can take saved language from storage here
    return getLocales()?.[0]?.languageCode ?? "en";
}

void initialization({
    defaultFallback: 'en',
    languages: [
        enLang,
        kyLang
    ],
    localeFromPhone,
    debug: true // i18next debug (optional)
})

Open index.js, App.tsx, or app/_layout.tsx and add languages initialization import.

import "./localization/_i18n"

If you want to set language immediately, you can use localeFromPhone handler

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 i18next 
    } = useIntl()

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

Change language

There are several helper methods for working with languages.

const {
    t, // Instance format-message 
    locale, // Current locale string
    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 {
    getLanguages, // Method to change locale
} = useIntl()

const languages = getLanguages()

console.log(languages)

// [{
//     locale: 'en',
//     selected: true,
//     meta: {
//          label: 'English' // Meta from language pack
//     }
// }, {
//     locale: 'ky',
//     selected: false,
//     meta: {
//          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.

const {
    changeLocale, // Method to change locale
} = useIntl()

changeLocale('ky')