1. i18next-sprintf-postprocessor
backend layer for i18next using browsers xhr
i18next-sprintf-postprocessor
Package: i18next-sprintf-postprocessor
Created by: i18next
Last modified: Sat, 18 Jun 2022 23:48:44 GMT
Version: 0.2.2
License: MIT
Downloads: 63,548
Repository: https://github.com/i18next/i18next-sprintf-postProcessor

Install

npm install i18next-sprintf-postprocessor
yarn add i18next-sprintf-postprocessor

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.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'

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code