GithubHelp home page GithubHelp logo

isabella232 / i18next-sprintf-postprocessor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from i18next/i18next-sprintf-postprocessor

0.0 0.0 0.0 28 KB

sprintf post processor for i18next

License: MIT License

JavaScript 100.00%

i18next-sprintf-postprocessor's Introduction

Introduction

This is a i18next postProcessor enabling sprintf usage for translations.

Getting started

Source can be loaded via npm, bower or downloaded from this repo.

# npm package
$ npm install i18next-sprintf-postprocessor

# bower
$ bower install i18next-sprintf-postprocessor
  • If you don't use a module loader it will be added to window.i18nextSprintfPostProcessor

Wiring up:

import i18next from 'i18next';
import sprintf from 'i18next-sprintf-postprocessor';

i18next
  .use(sprintf)
  .init(i18nextOptions);

usage sample

// given loaded resources
// translation: {
//   key1: 'The first 4 letters of the english alphabet are: %s, %s, %s and %s',
//   key2: 'Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s',
//   key3: 'The last letter of the english alphabet is %s',
//   key3: 'Water freezes at %d degrees'
// }

i18next.t('key1', { postProcess: 'sprintf', sprintf: ['a', 'b', 'c', 'd'] });
// --> 'The first 4 letters of the english alphabet are: a, b, c and d'

i18next.t('key2', { postProcess: 'sprintf', sprintf: { users: [{name: 'Dolly'}, {name: 'Molly'}, {name: 'Polly'}] } });
// --> 'Hello Dolly, Molly and Polly'

Using overloadTranslationOptionHandler

import i18next from 'i18next';
import sprintf from 'i18next-sprintf-postprocessor';

i18next
  .use(sprintf)
  .init({
    overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler
  });

// given loaded resources
// translation: {
//   key1: 'The first 4 letters of the english alphabet are: %s, %s, %s and %s',
//   key2: 'Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s',
//   key3: 'The last letter of the english alphabet is %s',
//   key3: 'Water freezes at %d degrees'
// }

i18next.t('interpolationTest1', 'a', 'b', 'c', 'd');
// --> 'The first 4 letters of the english alphabet are: a, b, c and d'

i18next.t('interpolationTest3', 'z');
// --> 'The last letter of the english alphabet is z'

i18next.t('interpolationTest4', 0);
// --> 'Water freezes at 0 degrees'

Gold Sponsors

i18next-sprintf-postprocessor's People

Contributors

aminify avatar jamuhl avatar perrin4869 avatar

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.