fusion-plugin-i18n
Adds I18n (Internationalization) support to a FusionJS app
For date i18n, consider using date-fns
If you're using React, you should use fusion-plugin-i18n-react
instead of this package.
Installation
yarn add fusion-plugin-i18n
Example
// src/main.js
import React from 'react';
import App from 'fusion-react';
import Internationalization from 'fusion-plugin-i18n';
import fetch from 'unfetch';
import Hello from './hello';
import TranslationsLoader from './translation-loader';
export default () => {
const app = new App(<div></div>);
const I18n = app.plugin(Internationalization, __BROWSER__ ? {fetch} : {TranslationsLoader});
app.plugin(Hello, {I18n});
return app;
}
// src/hello.js
export default ({I18n}) => (ctx, next) => {
// use the service
if (__NODE__ && ctx.path === '/hello') {
const i18n = I18n.of(ctx);
ctx.body = {
message: i18n.translate('test', {name: 'world'}), // hello world
}
}
return next();
}
// src/translation-loader.js
import {Plugin} from 'fusion-core';
const translations = {
'en-US': {
test: "hello ${name}"
}
}
export default () => {
return new Plugin {
Service: class {
constructor(ctx) {
// locale could be determined in different ways,
// e.g. from ctx.headers['accept-headers'] or from a /en-US/ URL
this.locale = 'en-US';
this.translations = translations[this.locale];
}
}
}
}
API
Instance methods
const {translate} = app.plugin(Internationalization, __BROWSER__ ? {fetch} : {TranslationsLoader}).of();
translate: (key: string, interpolations: Object) => string