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

handlebars-i18next

v1.0.3

Published

Handlebars helper that lets you translate with i18next inside your templates

Downloads

6,155

Readme

handlebars-i18next

npm npm version

Handlebars helper that lets you translate with i18next inside your templates.

New! Need to automatically collect the {{i18n}} tags from your Handlebars templates for your translation JSON? Look no further than our sister package handlebars-i18next-parser.

Quickstart

Installation

$ npm i handlebars-i18next

Glue code

import Handlebars from 'handlebars';  // runtime also possible
import i18next from 'i18next';
import registerI18nHelper from 'handlebars-i18next';

// Prepare your i18next instance (can be a custom instance)
i18next.init({
    resources: {
        en: {
            translation: {
                greeting: 'Hello, {{name}}!',
            }
        },
        fr: {...},
    },
    ...
}, function(error, t) {
    // Once this callback is called, you can start rendering templates
    // that depend on the helper (if `error` is undefined).
});

registerI18nHelper(Handlebars, i18next);

Template

{{i18n 'greeting'}}

Template call

template({name: 'Alice'});

Result

Hello, Alice!

Properties in the context of the helper are automatically available as interpolation values to i18next. It just works!

Advanced usage

Block helper: templated default value

You can use the helper as a section. The nested template block will be rendered as usual with the same context and passed to i18next.t as the defaultValue option.

{{#i18n 'greeting'}}Please be welcome, {{name}}!{{/i18n}}

So if the greeting key is not found in any of the selected languages in the current namespace, this will be rendered:

Please be welcome, Alice!

Otherwise, the following or one of its translations.

Hello, Alice!

Providing explicit interpolation values

You can pass an i18next.replace property in the root context of the template call in order to provide interpolation values for all helpers in the template.

template({
    name: Alice,
    i18next: {
        replace: {
            name: 'Bob',
        },
    },
});

will result in

Hello, Bob!

You can also pass arbitrary keyword arguments to the helper. These will be passed as options to i18next.t and be available as interpolation values.

{{i18n 'greeting' name='Cynthia'}}

will result in

Hello, Cynthia!

Keyword arguments take precedence over root.i18next.replace, which in turn takes precedence over the current context of the helper.

Passing other options to i18next.t

See the i18next documentation for available options.

In order to provide default options for all occurrences of the helper in your template, pass the options hash as the i18next property of the root context to the template call.

template({name: 'Alice', i18next: {
    lng: 'fr',
    interpolate: {...},
    ...
}});

In order to override options for a single occurrence of the helper, pass them directly as keyword arguments to the helper.

{{i18n 'greeting' lng='fr' interpolate='{...}'}}

Some notes:

  • The options lngs, fallbackLng, ns, postProcess and interpolation must be JSON-encoded strings when passed as keyword arguments.
  • The returnObjects option is forced to be false, since Handlebars helpers must return a string. You can pass another value, but it will be ignored.
  • The replace option is not supported as keyword argument. Pass the interpolation values individually as keyword arguments instead, as described in the previous section.

Changing the name of the helper

You can override the helper name by passing the name of your choice as the optional third argument to the exported helper registering function.

import registerI18nHelper from 'handlebars-i18next';

// ...

registerI18nHelper(Handlebars, i18next, 't');
{{t 'greeting'}}

Made by

Digital Humanities Lab