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

@federico.moretti/react-translate

v2.0.3

Published

Translate your React app without any hassle and with no setup!

Downloads

99

Readme

react-translate

Translate your React app without any hassle and with no setup!

Features

  • Translate function
  • Translate component
  • Allow nested translations
  • Singular, plural and zero based on count
  • Lightweight (~3KB)
  • Built with TypeScript
  • ~99% code coverage

Installing

npm install @federico.moretti/react-translate
# or
yarn add @federico.moretti/react-translate

Example

Basic usage:

import React from 'react';
import ReactDOM from 'react-dom';
import {
  TranslateProvider,
  useTranslate,
} from '@federico.moretti/react-translate';

const translations = {
  pear: {
    it: 'Pera',
    en: 'Pear',
  },
  apple: {
    it: ['Mela', 'Mele'],
    en: ['Apple', 'Apples'],
  },
  sub: {
    strawberry: {
      en: ['1 strawberry', '%n strawberries', '0 strawberries'],
      it: ['1 ciliegia', '%n ciliegie', '0 ciliegie'],
    },
  },
};

function App() {
  const { t, setLanguage, withPrefix, language } = useTranslate();
  const sub = withPrefix('sub');

  return (
    <div>
      <p>Selected language: {language}</p>
      <p>{t('pear')}</p>
      <p>{t('apple', { count: 2 })}</p>
      <p>{t('sub.strawberry')}</p>
      <p>{sub('strawberry')}</p>
      <div>
        <button onClick={() => setLanguage('it')}>Change to it</button>
        <button onClick={() => setLanguage('en')}>Change to en</button>
      </div>
    </div>
  );
}

ReactDOM.render(
  <TranslateProvider defaultLanguage="en" translations={translations}>
    <App />
  </TranslateProvider>,
  document.getElementById('root')
);

CodeSandbox

API

t(id: string, params?: TranslateParams): string

It returns the translation as a string.

  • id
    • use dot notation to get nested translations
  • params:
    • count?: number
      • select singular, plural or zero
      • if plural %n in a string will be replaced with the count
    • prefix?: string
      • allows to always get nested translations
    • returnIdIfMissing: boolean
      • defaults to true
      • allows to hide the translation id if missing

setLanguage(language: string): void

It changes the language in the provider.

withPrefix(prefix: string): function

It returns t() with the prefix already added.

This is useful if you have to use a lot of translations with the same prefix, for example in a page.

const t = withPrefix('dashboard');
const dashboardTitle = t('title'); // 'dashboard.title'

<T />

Creates a text node (or another element) with the translation.

  • id: string
  • type: keyof React.ReactHTML
    • if type equals p it will return <p>your translation</p>
  • prefix: string
  • count: number
  • returnIdIfMissing: boolean

<TranslateProvider />

Wrap the app with the provider.

  • defaultLanguage: string
    • it defaults to browser language if undefined
    • example: it-IT
  • translations: Translations
    • the translations object
    • required
  • fallbackLanguage: string
    • if a translation is missing this language will be used
    • example: en-GB
  • suppressWarnings: boolean
    • hides the warnings in the console
  • showIds: boolean
    • show translation ids

Translation object

const translations = {
  // basic
  pear: {
    it: 'Pera',
    en: 'Pear',
  },
  apple: {
    // [1, 2+]
    it: ['Mela', 'Mele'],
    en: ['Apple', 'Apples'],
  },
  // nested
  sub: {
    strawberry: {
      // [1, 2+, 0]
      en: ['1 strawberry', '%n strawberries', '0 strawberries'],
      it: ['1 ciliegia', '%n ciliegie', '0 ciliegie'],
    },
  },
};

translate(object): TranslateFunctionParams

Same logic of t() but exported to be used outside of the React context, for example in e2e tests.

  • object:
    • id: string
      • required
    • language: string
      • required
    • translations: Translations
      • the translations object
      • required
    • params: TranslateParams
      • count?: number
        • select singular, plural or zero
        • if plural %n in a string will be replaced with the count
      • prefix?: string
        • allows to always get nested translations
      • returnIdIfMissing: boolean
        • defaults to true
        • allows to hide the translation id if missing
    • fallbackLanguage: string
      • if a translation is missing this language will be used
      • example: en-GB
    • suppressWarnings: boolean
      • hides the warnings in the console
    • showIds: boolean
      • show translation ids

mergeTranslations(array): Translations

A function to merge different language files.

  • array: { language: string; translations: TranslationsWithoutLanguage }[]
    • it will merge the translation using the language
const translationsEn = {
  pear: 'Pear',
  banana: ['Banana', 'Bananas'],
};

const translationsIt = {
  pear: 'Pera',
  banana: ['Banana', 'Banane'],
};

const merged = mergeTranslations([
  { language: 'it', translations: translationsIt },
  { language: 'en', translations: translationsEn },
]);

/* this is how the result will be
{
  pear: { it: 'Pera', en: 'Pear' },
  banana: {
    it: ['Banana', 'Banane'],
    en: ['Banana', 'Bananas'],
  },
};
*/

Coverage

| File | % Stmts | % Branch | % Funcs | % Lines | | --------- | ------- | -------- | ------- | ------- | | All files | 99 | 91 | 100 | 99 |

Licence

MIT © Federico Moretti