I18n for modern web
Install
yarn add i18n-jsnext
npm install i18n-jsnext --save
Features
- typescript based source files and convenient api
- loading localization resources with namespaces support (splitting to multiple files)
- react.js built-in components (react >=16.3 required)
- messageformat templates support
Plugins
reactPostProcess()
(variables interpolation for react.js components)keepLocale()
(remember locale in localStorage or cookies and restore after page reload)
Usage with React (optional)
I18nProvider
,I18nConsumer
(provide/consume i18n instance for whole app)i18nInjector()
decorator for providingthis.props.i18n
within componentsI18nMessage
(general use i18n.translate wrapper)I18nNumber
,I18nPrice
,I18nPercents
(format numbers)I18nDate
(format date/time)I18nPlural
(pluralize word form, supports only single locale, not included in common bundleindex.js
)
Example
For more details see in ./example
folder
// i18n-init.ts;; const i18n = loadPath: "/locales/{locale}.json" plugins: ;
// components/app.tsximport * as React from "react";import render from "react-dom";import I18nProvider I18nMessage from "i18n-jsnext";import i18n from "../i18n-init"; Component static init = await i18n; // loading resources ; { return <I18nProvider => <I18nMessage ="App.greetings" =/> </I18nProvider> } App;
# components/app.intl.yml en: App: greetings: Hello <highlight>world</highlight>! # param "highlight" parsed with react-postprocess plugin Today is {now, date, full} # param "now" parsed with messageformat package ---ru: App: greetings: Привет <highlight>мир</highlight>! Сегодня {now, date, full}
Server-side addons
Require additional installs:
npm install commander glob js-yaml lodash --save
- built-in locales route for express.js
; const app = ; // provide localization json files in runtime from source files folder.// url for consuming: "/locale/en.json?ns=dictionary", // where "?ns=" optional query param to instruct the route provide resources from specific namespace only app;
- built-in script to gather all localization files from the project and make complete json for consuming in production
Add to package.json
runnable script:
"scripts":
Where client
and build
directories relative to project root, same as used in localesRoute()
above.
Running demo
git clone git@github.com:ixrock/i18n-jsnext.git
cd ./i18n-jsnext
yarn
ornpm install
(compilation might have errors with npm)yarn dev
ornpm run dev
By default dev server will be running at port 9000
.
If it's already taken in your system you can run the project
as LOCAL_SERVER_PORT=9001 yarn dev
or change the port at ./example/server/config.ts
and try again.
`
Useful links:
Find plural categories for specific locale and their corresponding numeric rules:
Powered by typescript, react, webpack and some others. Made with ♥