fusion-plugin-i18n
Adds I18n (Internationalization) support to a FusionJS app
Last updated 18 days ago by fusion-ci .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fusion-plugin-i18n 
SYNC missed versions from official npm registry.

fusion-plugin-i18n

Build status

Adds I18n (Internationalization) string support to a Fusion.js app.

This plugin looks for translations in the ./translations folder by default. Translations for each language are expected to be in a JSON file with a locale as a filename. For example, for U.S. English, translations should be in ./translations/en-US.json. Language tags are dictated by your browser, and likely follow the RFC 5646 specification.

If you're using React, you should use fusion-plugin-i18n-react instead of this package.


Table of contents


Installation

yarn add fusion-plugin-i18n

Usage

Simple middleware

The plugin provides a programmatic interface which exposes translate. See Service API for more details.

// src/some-middleware-plugin.js
import {createPlugin} from 'fusion-core';

export default createPlugin({
  deps: {I18n: I18nToken},
  middleware: ({I18n}) => {
    return (ctx, next) => {
      if (__NODE__ && ctx.path === '/some-path') {
        const i18n = I18n.from(ctx);
        ctx.body = {
          message: i18n.translate('test', {name: 'world'}), // hello world
        };
      }
      return next();
    };
  },
});

The default loader expects translation files to live in ./translations/{locale}.

./translations/en-US.json

{
  "HomeHeader": "Welcome!",
  "Greeting": "Hello, ${name}"
}

./translations/pt-BR.json

{
  "HomeHeader": "Bem-vindo!",
  "Greeting": "Olá, ${name}"
}

Setup

// src/main.js
import App from 'fusion-core';
import I18n, {
  I18nToken,
  I18nLoaderToken,
  createI18nLoader,
} from 'fusion-plugin-i18n';
import {FetchToken} from 'fusion-tokens';
import fetch from 'unfetch';

export default () => {
  const app = new App(<div />);

  app.register(I18nToken, I18n);
  __NODE__
    ? app.register(I18nLoaderToken, createI18nLoader())
    : app.register(FetchToken, fetch);

  return app;
};

API

Registration API

I18n
import I18n from 'fusion-plugin-i18n-react';

The i18n plugin. Typically, it should be registered to I18nToken. Provides the i18n service

I18nToken
import {I18nToken} from 'fusion-plugin-i18n-react';

The canonical token for the I18n plugin. Typically, it should be registered with the I18n plugin.

Dependencies

I18nLoaderToken
import {I18nLoaderToken} from 'fusion-plugin-i18n';

A function that provides translations. Optional. Server-side only.

Type
type I18nLoader = {
  from: (ctx: Context) => {locale: string | Locale, translations: {[string]: string},
};
  • loader.from: (ctx) => ({locale, translations}) -
    • ctx: FusionContext - Required. A Fusion.JS context object.
    • locale: Locale - A Locale
    • translations: {[string]: string} - A object that maps translation keys to translated values for the given locale
Default value

If no loader is provided, the default loader will read translations from ./translations/{locale}.json. See src/loader.js for more details.

HydrationStateToken
import {HydrationStateToken} from 'fusion-plugin-i18n';

Sets the hydrated state in the client, and can be useful for testing purposes. Optional. Browser only.

Type
type HydrationState = {
  chunks: Array<string | number>,
  translations: {[string]: string},
};
Default value

If no hydration state is provided, this will be an empty object ({}) and have no effect.

FetchToken
import {FetchToken} from 'fusion-tokens';

A fetch implementation. Browser-only.

Type
type Fetch = (url: string, options: Object) => Promise<Response>;
  • url: string - Required. Path or URL to the resource you wish to fetch.
  • options: Object - Optional. You may optionally pass an init options object as the second argument. See Request for more details.
  • [return]: Promise<Request> - Return value from fetch. See [Response](A function that loads appropriate translations and locale information given an HTTP request context) for more details.
Default value

If no fetch implementation is provided, window.fetch is used.

Service API

const translation: string = i18n.translate(
  key: string,
  interpolations: {[string]: string}
);
  • key: string - A translation key. When using createI18nLoader, it refers to a object key in a translation json file.
  • interpolations: object - A object that maps an interpolation key to a value. For example, given a translation file {"foo": "${bar} world"}, the code i18n.translate('foo', {bar: 'hello'}) returns "hello world".
  • translation: string - A translation, or key if a matching translation could not be found.

Other examples

Custom translations loader example

// src/main.js
import App from 'fusion-core';
import I18n, {I18nToken, I18nLoaderToken} from 'fusion-plugin-i18n';
import {FetchToken} from 'fusion-tokens';
import fetch from 'unfetch';
import I18nLoader from './translations';

export default () => {
  const app = new App(<div></div>);

  app.register(I18nToken, I18n);
  __NODE__
    ? app.register(I18nLoaderToken, I18nLoader);
    : app.register(FetchToken, fetch);

  // ....

  return app;
}

// src/translation-loader.js
import {Locale} from 'locale';

const translationData = {
  'en_US': {
    test: "hello ${name}"
  }
}

export default (ctx) => {
  // locale could be determined in different ways,
  // e.g. from ctx.headers['accept-language'] or from a /en_US/ URL
  const locale = new Locale('en_US');
  const translations = translationData[locale];
  return {locale, translations};
}

Custom locale resolver

If you want to use the default loader, but want to provide a custom locale resolver, you can pass it into createI18nLoader

// src/main.js
import App from 'fusion-core';
import I18n, {createI18nLoader, I18nToken, I18nLoaderToken} from 'fusion-plugin-i18n';
import {FetchToken} from 'fusion-tokens';
import fetch from 'unfetch';

// use custom locale header instead of default
const myLocaleResolver = (ctx) => ctx.headers['my-locale-header'];

export default () => {
  const app = new App(<div></div>);

  app.register(I18nToken, I18n);
  __NODE__
    ? app.register(I18nLoaderToken, createI18nLoader(myLocaleResolver));
    : app.register(FetchToken, fetch);

  // ....

  return app;
}

Current Tags

  • 0.0.0-canary.67e33d8.0                                ...           canary (3 days ago)
  • 2.3.1                                ...           latest (18 days ago)
  • 1.2.3-0                                ...           prerelease (5 months ago)

186 Versions

  • 0.0.0-canary.67e33d8.0                                ...           3 days ago
  • 0.0.0-canary.a911dd9.0                                ...           3 days ago
  • 0.0.0-canary.22593cf.0                                ...           4 days ago
  • 0.0.0-canary.dd07585.0                                ...           4 days ago
  • 0.0.0-canary.5be6a00.0                                ...           4 days ago
  • 0.0.0-canary.d4b0b6c.0                                ...           4 days ago
  • 0.0.0-canary.49d4d79.0                                ...           4 days ago
  • 0.0.0-canary.2925a25.0                                ...           5 days ago
  • 0.0.0-canary.ff793d9.0                                ...           7 days ago
  • 0.0.0-canary.a27ac53.0                                ...           7 days ago
  • 0.0.0-canary.08011c5.0                                ...           7 days ago
  • 0.0.0-canary.ad6c873.0                                ...           7 days ago
  • 0.0.0-canary.c0ad947.0                                ...           7 days ago
  • 0.0.0-canary.413f404.0                                ...           7 days ago
  • 2.3.1                                ...           18 days ago
  • 0.0.0-canary.66d82db.0                                ...           18 days ago
  • 0.0.0-canary.109b061.0                                ...           18 days ago
  • 0.0.0-canary.a0f0132.0                                ...           19 days ago
  • 0.0.0-canary.2a2b83b.0                                ...           24 days ago
  • 0.0.0-canary.d78b7bb.0                                ...           25 days ago
  • 0.0.0-canary.096185d.0                                ...           25 days ago
  • 0.0.0-canary.9d5ef83.0                                ...           25 days ago
  • 0.0.0-canary.f608c58.0                                ...           a month ago
  • 0.0.0-canary.a9d6e7c.0                                ...           a month ago
  • 0.0.0-canary.aa40d96.0                                ...           a month ago
  • 0.0.0-canary.c189a42.0                                ...           a month ago
  • 0.0.0-canary.d767d28.0                                ...           a month ago
  • 0.0.0-canary.854848e.0                                ...           a month ago
  • 0.0.0-canary.e2fcadb.0                                ...           a month ago
  • 2.3.0                                ...           a month ago
  • 0.0.0-canary.ffb0ab3.0                                ...           a month ago
  • 0.0.0-canary.b102bb9.0                                ...           a month ago
  • 0.0.0-canary.1e7190d.0                                ...           a month ago
  • 0.0.0-canary.73f956e.0                                ...           a month ago
  • 0.0.0-canary.e3270dd.0                                ...           a month ago
  • 0.0.0-canary.4c8e718.0                                ...           a month ago
  • 0.0.0-canary.aa5bec0.0                                ...           2 months ago
  • 0.0.0-canary.bd48967.0                                ...           2 months ago
  • 0.0.0-canary.465e7e1.2                                ...           2 months ago
  • 0.0.0-canary.465e7e1.1                                ...           2 months ago
  • 0.0.0-canary.465e7e1.0                                ...           2 months ago
  • 0.0.0-canary.d1e11d1.1                                ...           2 months ago
  • 0.0.0-canary.d1e11d1.0                                ...           2 months ago
  • 0.0.0-canary.b30cfc9.0                                ...           2 months ago
  • 0.0.0-canary.3a6c203.0                                ...           2 months ago
  • 0.0.0-canary.73b3b7a.0                                ...           2 months ago
  • 0.0.0-canary.87693bb.1                                ...           2 months ago
  • 0.0.0-canary.87693bb.0                                ...           2 months ago
  • 0.0.0-canary.36c6897.0                                ...           2 months ago
  • 0.0.0-canary.ca9c1e7.0                                ...           2 months ago
  • 0.0.0-canary.bdd94ac.0                                ...           2 months ago
  • 2.2.2                                ...           2 months ago
  • 0.0.0-canary.631e52a.0                                ...           2 months ago
  • 0.0.0-canary.96b08c1.0                                ...           2 months ago
  • 0.0.0-canary.88403d8.0                                ...           2 months ago
  • 0.0.0-canary.4a47f03.0                                ...           2 months ago
  • 0.0.0-canary.a8af54e.0                                ...           2 months ago
  • 0.0.0-canary.675492e.0                                ...           2 months ago
  • 0.0.0-canary.1b63aac.0                                ...           2 months ago
  • 0.0.0-canary.47d3251.0                                ...           2 months ago
  • 0.0.0-canary.6499178.0                                ...           2 months ago
  • 0.0.0-canary.d0e4cd8.0                                ...           2 months ago
  • 0.0.0-canary.17cfc1d.0                                ...           2 months ago
  • 0.0.0-canary.08790d0.0                                ...           2 months ago
  • 0.0.0-canary.fbc7ca5.0                                ...           2 months ago
  • 0.0.0-canary.9dc742c.0                                ...           2 months ago
  • 0.0.0-canary.471a10b.0                                ...           2 months ago
  • 0.0.0-canary.0953f07.0                                ...           2 months ago
  • 0.0.0-canary.14b4f0e.0                                ...           2 months ago
  • 0.0.0-canary.da2cfec.0                                ...           2 months ago
  • 0.0.0-canary.02195f4.0                                ...           2 months ago
  • 0.0.0-canary.a8de467.1                                ...           2 months ago
  • 0.0.0-canary.a8de467.0                                ...           2 months ago
  • 0.0.0-canary.fc2bf62.0                                ...           2 months ago
  • 0.0.0-canary.e9f07ef.0                                ...           2 months ago
  • 0.0.0-canary.ec8188c.0                                ...           2 months ago
  • 0.0.0-canary.403f456.0                                ...           2 months ago
  • 0.0.0-canary.66cfac0.0                                ...           2 months ago
  • 0.0.0-canary.396f8bb.0                                ...           2 months ago
  • 0.0.0-canary.db655de.1                                ...           2 months ago
  • 0.0.0-canary.3b5ed2f.0                                ...           2 months ago
  • 0.0.0-canary.0a4669b.1                                ...           2 months ago
  • 0.0.0-canary.0a4669b.0                                ...           2 months ago
  • 0.0.0-canary.50aa07c.0                                ...           2 months ago
  • 0.0.0-canary.b0e5994.0                                ...           2 months ago
  • 0.0.0-canary.4cb9c2b.0                                ...           2 months ago
  • 0.0.0-canary.4c30d33.0                                ...           2 months ago
  • 0.0.0-canary.fbfd207.0                                ...           2 months ago
  • 0.0.0-canary.40eb0a5.1                                ...           2 months ago
  • 0.0.0-canary.40eb0a5.0                                ...           2 months ago
  • 0.0.0-canary.1ddabc1.1                                ...           2 months ago
  • 0.0.0-canary.1ddabc1.0                                ...           2 months ago
  • 0.0.0-canary.016f954.0                                ...           2 months ago
  • 0.0.0-canary.98adb08.0                                ...           2 months ago
  • 2.2.1                                ...           3 months ago
  • 0.0.0-canary.69d2497.1                                ...           3 months ago
  • 0.0.0-canary.69d2497.0                                ...           3 months ago
  • 0.0.0-canary.16ee004.0                                ...           3 months ago
  • 0.0.0-canary.5c3b351.0                                ...           3 months ago
  • 0.0.0-canary.3e2cc31.0                                ...           3 months ago
  • 0.0.0-canary.ed79452.0                                ...           3 months ago
  • 0.0.0-canary.407b8e4.0                                ...           3 months ago
  • 2.2.0                                ...           3 months ago
  • 0.0.0-canary.63145f9.0                                ...           3 months ago
  • 0.0.0-canary.801e47f.0                                ...           3 months ago
  • 0.0.0-canary.d0b5703.0                                ...           3 months ago
  • 0.0.0-canary.d93b398.0                                ...           3 months ago
  • 0.0.0-canary.3703af8.1                                ...           3 months ago
  • 0.0.0-canary.3703af8.0                                ...           3 months ago
  • 0.0.0-canary.27245f3.0                                ...           3 months ago
  • 0.0.0-canary.51752cd.0                                ...           3 months ago
  • 0.0.0-canary.8ae44a5.1                                ...           3 months ago
  • 0.0.0-canary.8ae44a5.0                                ...           3 months ago
  • 0.0.0-canary.b7caf15.1                                ...           3 months ago
  • 0.0.0-canary.b7caf15.0                                ...           3 months ago
  • 0.0.0-canary.7cfc88b.0                                ...           3 months ago
  • 0.0.0-canary.0b1303b.0                                ...           3 months ago
  • 0.0.0-canary.8c0849b.0                                ...           3 months ago
  • 0.0.0-canary.c237758.0                                ...           3 months ago
  • 0.0.0-canary.2fcf0b5.1                                ...           3 months ago
  • 0.0.0-canary.2fcf0b5.0                                ...           3 months ago
  • 0.0.0-canary.1131efb.0                                ...           3 months ago
  • 0.0.0-canary.af0fe55.1                                ...           3 months ago
  • 0.0.0-canary.af0fe55.0                                ...           3 months ago
  • 0.0.0-canary.72a6835.0                                ...           3 months ago
  • 0.0.0-canary.4fb3fa6.1                                ...           3 months ago
  • 0.0.0-canary.4fb3fa6.0                                ...           3 months ago
  • 0.0.0-canary.43352f4.1                                ...           3 months ago
  • 0.0.0-canary.43352f4.0                                ...           3 months ago
  • 0.0.0-canary.9b5e4cf.0                                ...           4 months ago
  • 0.0.0-canary.d9a277c.0                                ...           4 months ago
  • 0.0.0-canary.2fb1140.0                                ...           4 months ago
  • 0.0.0-canary.5bf099f.0                                ...           4 months ago
  • 0.0.0-canary.4e310bc.0                                ...           4 months ago
  • 0.0.0-canary.fd5a750.0                                ...           4 months ago
  • 2.1.0                                ...           4 months ago
  • 0.0.0-canary.872b547.0                                ...           4 months ago
  • 2.0.0                                ...           4 months ago
  • 0.0.0-canary.3811188.0                                ...           4 months ago
  • 0.0.0-canary.9941505.0                                ...           4 months ago
  • 0.0.0-canary.309a1d1.1                                ...           4 months ago
  • 0.0.0-canary.309a1d1.0                                ...           4 months ago
  • 1.2.3-0                                ...           5 months ago
  • 1.2.2                                ...           6 months ago
  • 1.2.2-1                                ...           6 months ago
  • 1.2.2-0                                ...           6 months ago
  • 1.2.1                                ...           6 months ago
  • 1.2.1-0                                ...           6 months ago
  • 1.2.0                                ...           8 months ago
  • 1.2.0-0                                ...           8 months ago
  • 1.1.6                                ...           9 months ago
  • 1.1.6-0                                ...           10 months ago
  • 1.1.5                                ...           10 months ago
  • 1.1.5-0                                ...           10 months ago
  • 1.1.4                                ...           10 months ago
  • 1.1.4-0                                ...           10 months ago
  • 1.1.3                                ...           10 months ago
  • 1.1.2                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.1-0                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.4.3                                ...           2 years ago
  • 0.4.2                                ...           2 years ago
  • 0.4.1                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.11                                ...           2 years ago
  • 0.1.10                                ...           2 years ago
  • 0.1.9                                ...           2 years ago
  • 0.1.8                                ...           2 years ago
  • 0.1.6                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 3,700
Last Day 8
Last Week 2,891
Last Month 7,638
Dependencies (3)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |