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

expo-next-translation

v0.0.1

Published

Native translation i18n for next.js & expo

Downloads

1

Readme

Expo + Next.js Translation Using i18n-js

Universal localization for native platforms such as mobile (expo) and web (Next.js). This library was built on top of next-translate for Next.js & expo-localization for Expo. A Monorepo example is linked here.

Install

yarn add expo-next-translation

Setup

Install next with expo:

  • Init: expo init (or npx create-next-app)

  • Install: yarn add @expo/next-adapter

  • Install next: yarn add next

  • Configure: yarn next-expo

  • Start: yarn next dev

Step 1. Edit/create next.config.js

yarn add next-translate next-compose-plugins next-fonts next-images next-transpile-modules

Step 2: edit next.config.js to look something like this:

const { withExpo } = require('@expo/next-adapter')
const withFonts = require('next-fonts')
const withImages = require('next-images')
const nextTranslate = require('next-translate')
const withPlugins = require('next-compose-plugins')
const withTM = require('next-transpile-modules')([
  'expo-next-translation',
  // you can add other modules that need traspiling here
])
module.exports = withPlugins(
  [withTM, nextTranslate, withFonts, withImages, [withExpo, { projectRoot: __dirname }]],
  {
    // ...
  }
)

Step 3. Setup the localization setting for Next.js & Expo

  • Set the localization setting file in the root directory of the web project. For more information refer to the documentation. Simple example as below:

    module.exports = {
    locales: ["en", "cn"],
    defaultLocale: "en",
    pages: {
        "*": ["common", "screen"]
    },
    loadLocaleFrom: (lang, ns) =>
        import(`${lang}/${ns}.json`).then((m) => m.default)
    };
  • Same as the web localization setting file, we have to setup for the mobile directory as well. i18n-js & expo-localization will be used to setup for mobile

    import * as Localization from "expo-localization";
    import i18n from "i18n-js";
    
    i18n.fallbacks = true;
    i18n.translations = {
    cn: {
        common: require("cn/common.json"),
        screen: require("cn/screen.json")
    },
    en: {
        common: require("en/common.json"),
        screen: require("en/screen.json")
    }
    };
    i18n.locale = Localization.locale || "en";

Perfect! The setup for both web and mobile is completed

API

useTranslation()

React hook that wraps useTranslation (from next-translate/useTranslation) hook and t (from i18n).

import { useTranslation } from 'expo-next-translation'

export default function App() {
    const { lang, setLanguage, t } = useTranslation()
}

lang

  • returns the current language as string
  • Example ( 'en', 'cn' )

setLanguage

  • A React hook to set a new language. Wraps with useRouter from Next.js & i18n.locale for Expo
import { useTranslation } from 'expo-next-translation'

export default function App() {
    const { setLanguage } = useTranslation()

    const onPress = (selectedLang: string) => {
        setLanguage({ lang: selectedLang, url: '/', as: '/' })
    }
}
  • lang

    • Argument to be passed to update the screen language
    • string & required
  • url

    • This will be required for web to set the URL. For more information do refer to the Next.js Docs. By default is set to root path.
    • string, optional, default = '/'
  • as

    • This will be required for web to set the URL to be shown in the URL bar. By default is set to root path.
    • string, optional, default = '/'

t()

A React hook used to used to translate value using its key. Note: Should provide the translation folder name either passing through useTranslation() or t(). The example folder used below is common.

import { useTranslation } from 'expo-next-translation'

export default function App() {
    const { t } = useTranslation('common')

    return (
        <View>
            <Text>
                {t('greetings')}
            </Text>
        </View>
    )
}

OR

import { useTranslation } from 'expo-next-translation'

export default function App() {
    const { t } = useTranslation()

    return (
        <View>
            <Text>
                {t('common:greetings')}
            </Text>
        </View>
    )
}

The json file en/common.json should be as below

{
  "greetings": "Hey, Welcome",
  "greetingsRole": "Hey, Welcome {{role}}"
}

Dynamic variable can also be used for Next.js and Expo as shown below using the greetingsRole key

import { useTranslation } from 'expo-next-translation'

export default function App() {
    const { t } = useTranslation()

    return (
        <View>
            <Text>
                {t('common.greetings', {role: 'Admin'})}
            </Text>
        </View>
    )
}

This should render as

Hey, Welcome Admin

Follow

Follow me on Twitter