一个全功能的国际化翻译的JavaScript库:i18next
jopen
10年前
i18next是一个全功能,用于Web应用的国际化翻译的JavaScript库。运行在浏览器中,Node.js,rhino 和其他JavaScript运行时。i18next通过定义语言变量,页面编写私有标签,再绑定标签和语言变量,从而达到页面层国际化效果。
好处:
- support for variables
- support for nesting
- support for context
- support for multiple plural forms
- gettext support
- sprintf supported
- detect language
- graceful translation lookup
- jquery function
- get string or object tree
- get resourcefiles from server
- resource caching in browser
- post missing resources to server
- highly configurable
- custom post processing
- translation ui
使用示例:
page source:
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="[PATH]/jquery.js" /> // optional <script type="text/javascript" src="[PATH]/i18next.js" /> </head> <body> <ul class="nav"> <li><a href="#" data-i18n="nav.home"></a></li> <li><a href="#" data-i18n="nav.page1"></a></li> <li><a href="#" data-i18n="nav.page2"></a></li> </ul> </body> </html>
loaded resource file (locales/en/translation.json):
{ "app": { "name": "i18next" }, "nav": { "home": "Home", "page1": "Page One", "page2": "Page Two" } }
javascript code:
i18n.init(function(t) { // translate nav $(".nav").i18n(); // programatical access var appName = t("app.name"); });