simpler-translate

1.0.1 • Public • Published

simpler-translate

A even simpler translate helper method.

Installation

npm i simpler-translate

Usage

Create the translate helper by initializing it with an object of languages containing objects of key/value translations.

import { Translate } from 'simpler-translate';

const translate = Translate({ 'en': { 'HELLO': 'Hello' }, 'nl': { 'HELLO': 'Hallo' } });

console.log(await translate('en', 'HELLO')); // --> Hello
console.log(await translate('nl', 'HELLO')); // --> Hallo

Replacement values can be used in the same format as javascript template literals.

import { Translate } from 'simpler-translate';

const translate = Translate({ 'en': { 'HELLO': 'Hello ${name}' }});

console.log(await translate('en', 'HELLO', { name: 'Hank' })); // --> Hello Hank

The translations object can also be a promise returned by reading a file from the filesystem or fetching a file.

import { Translate } from 'simpler-translate';

const translate = Translate(Promise.resolve({ 'en': { 'HELLO': 'Hello' }, 'nl': { 'HELLO': 'Hallo' } }));

console.log(await translate('en', 'HELLO')); // --> Hello
console.log(await translate('nl', 'HELLO')); // --> Hallo

If you need more control over how your replacement values get joined with the translated text you can use TranslateRaw which will return the result as seperate segments just like tag functions.

import { TranslateRaw } from 'simpler-translate';

const translate = Translate({ 'en': { 'HELLO': 'Hello ${name}, how are you doing?' }});

console.log(await translate('en', 'HELLO', { name: 'Hank' })); // -> `[ ['Hello ', ', how are you doing?'], 'Hank' ]`

Dependencies (0)

    Dev Dependencies (11)

    Package Sidebar

    Install

    npm i simpler-translate

    Weekly Downloads

    0

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    16.1 kB

    Total Files

    15

    Last publish

    Collaborators

    • handijk