Git Product home page Git Product logo

i18n-tag-wrapper's Introduction

i18n-tag-wrapper

A wrapper to es2015-i18n-tag for use with Next.js

Usage

import translatorSetup from 'i18n-tag-wrapper'
import fr from './locales/fr'
import de from './locales/de'

const locales = { fr, de }
const default = fr

const getTranslator = translatorSetup({ default, plurals })

const T = getTranslator("fr")

let name = "Henri"

T`Hello ${name)}`
// Bonjour Henri

Locale configuration files

Example:

{
  locales: 'de-DE',
  // group: 'my-lib', // Optional, can be used to avoid configuration overrides. This is recommended for libraries!
  translations: {
      'Hello ${0}': 'Hallo, ${0}',
      'You have ${0} notifications:none': 'Sie habe keine Mitteilung',
      'You have ${0} notifications:one': 'Sie habe eine Mitteilung',
      'You have ${0} notifications:few': 'Sie habe zwei Mitteilungen',
      'You have ${0} notifications:many': 'Sie habe ${0} Mitteilungen',
      'You have ${0} horses:none': 'Sie haben kein Pferd',
      'You have ${0} horses:one': 'Sie haben ein Pferd',
      'You have ${0} horses:zwei': 'Sie haben zwei Pferde',
      'You have ${0} horses:many': 'Sie haben ${0} Pfrede',
  },
  number: {
      currency: 'EUR' // Intl NumberFormat options as described here: https://goo.gl/pDwbG2
  },
  date: {
      [...options] // Intl DateTimeFormat options as described here: https://goo.gl/lslekB
  },
  /**
   * @param {number} n Number of things
   * @returns {'none'|'one'|'few'|'many'|'other'}
   */
  plurals: (n) => {
    return n == 0 ?
      'none'
    : n == 1 ?
      'one'
    : n == 2 ?
      'few'
    : 'many'
  }
}

Plurals

Locale files must export a plurals(n) function with the following signature:

type Purals = (n: integer) -> "none"|"one"|"few"|"many"|"other"

Depending on the pluralization rules for your language, plurals() may return not return all variants. English, for instance only requires "one" and "many", but Polish will require all of them.

In the translations (under the translations key in the locale file), each translation requiring plural handling should have as many keys as there are variants returned by the plurals() function. Each key should by appended with the variant it handles.

Long sentences are no match for good examples:

// /locale/de.js
{
  locales: 'de-DE',
  /*
   * Germans like to write 0 as kein(e), 1 as ein(e), and 2 as zwei, using
   * numbers for higher values.
   */
  plurals: (n) => {
    return n == 0 ?
      'none'
    : n == 1 ?
      'one'
    : n == 2 ?
      'few'
    : 'many'
  }
  translations: {
    'Hello ${0}': 'Hallo, ${0}',
    /*
      * Plurals returns 4 possible variants, either: none, one, few or many
      * The "You have ${0} notifications" must have 4 variants
      */
    'You have ${0} notifications:none': 'Sie habe keine Mitteilung',
    'You have ${0} notifications:one': 'Sie habe eine Mitteilung',
    'You have ${0} notifications:few': 'Sie habe zwei Mitteilungen',
    'You have ${0} notifications:many': 'Sie habe ${0} Mitteilungen',
  }
}

Using it:

T.some(0, 'You have ${0} notifications')
// Sie habe keine Mitteilung

T.some(1, 'You have ${0} notifications')
// Sie habe eine Mitteilung

T.some(2, 'You have ${0} notifications')
// Sie habe zwei Mitteilungen

T.some(1000, 'You have ${0} notifications')
// Sie habe 1,000 Mitteilungen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.