Git Product home page Git Product logo

nunjucks-translation's Introduction

Nunjucks Translation

Build NPM Version NPM Downloads

Nunjucks extension for translation. With this you can use the trans and endtrans tags or the trans filter.

How to install it?

$ npm install nunjucks-translation

How to use it?

Example using static translations

import { TranslationExtension } from 'nunjucks-translation';

const translationExtension = new TranslationExtension({
  translations: {
    de: {
      message: {
        hello1: 'Hallo',
        hello2: 'Hallo {name}',
      },
    },
    en: {
      message: {
        hello1: 'Hello',
        hello2: 'Hello {name}',
      }
    },
  },
  locale: 'de',
  fallbackLocale: 'en',
});

nunjucksEnv.addExtension('translation-extension', translationExtension);
nunjucksEnv.addFilter('trans', (textId: string, locale: string, params: object) =>
  translationExtension.translateText(textId, locale, params),
);

Example using translations from JSON file:

import * as fs from 'fs';
import { TranslationExtension } from 'nunjucks-translation';

const translationExtension = new TranslationExtension(
  JSON.parse(
    fs.readFileSync('/path/to/translation.json', 'utf8'),
  )  
);

nunjucksEnv.addExtension('translation-extension', translationExtension);
nunjucksEnv.addFilter('trans', (textId: string, locale: string, params: object) =>
  translationExtension.translateText(textId, locale, params),
);

Translate content using tags

You can use the trans and endtrans tags to translate your content.

<html>
  <head>
  </head>
  <body>
    {% trans('de') %}message.hello1{% endtrans %} // result will be "Hallo"
    {% trans('en') %}message.hello1{% endtrans %} // result will be "Hello"
    {% trans('de', { name: 'John Doe' }) %}message.hello2{% endtrans %} // result will be "Hallo John Doe"
    {% trans('de', { name: 'John Doe' }) %}message.hello2{% endtrans %} // result will be "Hello John Doe"

    {% trans(null) %}message.hello1{% endtrans %} // result will be "Hallo" due to default locale
    {% trans('es') %}message.hello1{% endtrans %} // result will be "Hello" due to fallback locale
  </body>
</html>

Translate content using filter

You can also use the trans filter to translate your content.

<html>
  <head>
  </head>
  <body>
    {{ 'message.hello1'|trans('de') }} // result will be "Hallo"
    {{ 'message.hello1'|trans('en') }} // result will be "Hello"
    {{ 'message.hello2'|trans('de', { name: 'John Doe' }) }} // result will be "Hallo John Doe"
    {{ 'message.hello2'|trans('en', { name: 'John Doe' }) }} // result will be "Hello John Doe"

    {{ 'message.hello1'|trans(null) }} // result will be "Hallo" due to default locale
    {{ 'message.hello1'|trans('es) }} // result will be "Hello" due to fallback locale
  </body>
</html>

nunjucks-translation's People

Contributors

mgascoyne avatar

Watchers

 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.