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

@kaliber/i18n

v1.1.0

Published

Internationalize your React application

Downloads

9

Readme

i18n

Internationalize your React application.

Motivation

i18n, a.k.a. internationalization. Often you want to make your application available in multiple languages. @kaliber/i18n helps translating the static strings in your application.

Installation

yarn add @kaliber/i18n

Usage

Skip to reference

import { I18nProvider, useI18n, useI18nLanguage } from '@kaliber/i18n'
import { i18n } from './config/i18n.js'

function App() {
  return (
    <I18nProvider value={i18n} language='en'>
      <Page />
    </I18nProvider>
  )
}

function Page() {
  const i18n = useI18n()
  const language = useI18nLanguage()

  return (
    <div>
      <header>
        <Navigation links={i18n('navigation')} />
      </header>
      <main>
        <PageMain i18nPath='home' />
      </main>
      <aside>
        {/* Missing translations yield a warning in your console, when not in production mode */}
        {i18n('missing.aMissingTranslation')}
      </aside>
      <footer>
        {i18n('footer').copyright()} - {language.toUpperCase()}
      </footer>
    </div>
  )
}

// Please note that the i18nPath prop is not an actual part of this library,
// it's just a variable containing a path string ('home').

function PageMain({ i18nPath }) {
  const i18n = useI18n()

  return (
    <article>
      <header>
        {/* Prefix 'title' with the i18nPath */}
        <h1>{i18n(i18nPath, 'title')}</h1>

        {/* Access a translation in 'global' */}
        <em>{i18n('global.writtenBy')}: Kaliber</em>

        {/* Pass down the internationalized items as props */}
        <Tags tags={i18n(i18nPath, 'meta.tags')} />
      </header>

      {/* Or pass down the i18nPath and handle translations lower in the tree */}
      <PageContent {...{ i18nPath }} />
    </article>
  )
}

Example i18n object:

const i18n = {
  navigation: [
    {
      label: 'Home',
      link: '#'
    },
    {
      label: {
        nl: 'Neem contact op',
        en: 'Contact us'
      },
      link: '#contact'
    }
  ],
  home: {
    title: {
      nl: 'Welkom!',
      en: 'Welcome!'
    },
    meta: {
      tags: [
        {
          nl: 'dummy tekst',
          en: 'dummy text'
        },
        {
          nl: 'pagina',
          en: 'page'
        }
      ]
    },
    content: {
      title: {
        nl: 'Een klein beetje typesetting tekst',
        en: 'A bit of typesetting text'
      },
      body: <p>Lorem ipsum dolor sit amet <strong>consectetur adipisicing elit</strong>. Facere esse unde aut officiis repudiandae vero placeat totam voluptas. Doloribus iste quae maiores officia praesentium magni, perspiciatis quibusdam necessitatibus sequi nemo.</p>,
    },
  },
  footer: {
    copyright: () => `Copyright ${(new Date()).getFullYear()}`
  },
  global: {
    writtenBy: {
      nl: 'geschreven door',
      en: 'written by'
    },
    tags: 'tags'
  }
}

Reference

I18nProvider

<I18nProvider value={i18n} language='en'>
  {children}
</I18nProvider>

| Props | | |----------------|-------------------------------------------------------------------------------| | value | The i18n object to use. | | language | Optional. When a language is given, normalization will be attempted at the leave nodes. E.g.: given the language en this means a leafnode with the shape { en: 'countries', nl: 'landen' } will be normalized to just 'countries'. | | logMissingTranslation | Optional. (default: console.warn)If you want to change how missing translations are logged, you can provide your own logMissingTranslation function. This function is called with an object of the shape: { language, path }. |

The provided i18n object may be a deeply nested object, optionally containing arrays. Values don't have to be strings, you can also provide numbers, functions or React elements (see the example i18n object under usage)

useI18n

Returns (a subsection of) the i18n object.

const i18n = useI18n(i18nPath) 
const translatedString = i18n('path')

| Input | | |----------------|-------------------------------------------------------------------------------| | i18nPath | Optional. When given, the returned i18n function looks up its translations in a subset of the i18n object indicated by i18nPath. |

| Output | | |----------------|-------------------------------------------------------------------------------| | i18n | A function which accepts one or multiple i18nPath segments as arguments, which will be joined by dots. The value at this resulting path (prefixed with the i18nPath) will be normalized, then returned. When no value is found a warning will be logged (unless you're running production mode). |

Examples

const i18n = useI18n()
const translatedString = i18n('global.cta')
const i18n = useI18n('global')
const translatedString = i18n('cta')
function Component({ i18nPath }) {
  const i18n = useI18n()
  const translatedString = i18n(i18nPath, 'content.title')
  const anotherTranslatedString = i18n('global.cta')

  // ...
}

useI18nLanguage

Returns the language that is being used.

const language = useI18nLanguage() 

Disclaimer

This library is intended for internal use, we provide no support, use at your own risk. It does not import React, but expects it to be provided, which @kaliber/build can handle for you.

This library is not transpiled.