`
iwindyforest
  • 浏览: 230045 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

i18next: A Solution for Front End i18n Message

阅读更多

i18next: A Solution for Front End i18n Message  

 

Purpose

This document is used to introduce a solution which can support below features:

Ø  Support displaying i18n message in javascript

Ø  Support displaying message with parameters

Ø  Support display text for HTML elements

Ø  Support resource path custmization

Requirments

Jquery, i18next , JSON, IE Browser

Implementation

 

Initiation

When we are trying to use the any message in the jsp or javascript, we should make sure the javascript files which are needed should be included in the jsp file:

<script src="./js/jquery.js" type="text/javascript"></script>

  <script src="./js/i18next.js " type="text/javascript"></script>  

<script src="./js/sample.js" type="text/javascript"></script>

Take above sniplet as exame, if sample.js is our customized javascript, we should make sure all the initiation work should be included in $(document).ready() of this file:

$(document).ready(function(){

                  $.i18n.init(

                                {

                                                lng: 'en',

                                                ns: 'message',                      

                                                resGetPath: 'resource/locales/__lng__/__ns__.json'

                                },

                                function() {

                                                // save to use translation function as resources are fetched

                                                $(".menu").i18n();

                                                $(".translation").i18n();

                                                $("#userName").i18n();

                                }                             

                  );

});

$.i18n.init() is the init method for i18next,  the meanings of the variables used in this method is listed as following:

lng : The locale code for the browser, it can be mannually set, if it is not set manually, it will use defalut value: dev.

ns : name space, name space means the prefix name of message file which will be loaded

resGetPath : the resource files path, in this case, the variable: __lng__ and __ns__ are used, lng is en, ns is message, so the message file: 'resource/locales/en/message.json' will be loaded.

$(".translation").i18n();

$("#userName").i18n();

These two lines means loading required properties in the loaded message file.

If messages will be used in a container with the class name the same as the name in JSON file, It should be like: $(".translation").i18n();

If message will be used in a single element with the id the same as the name in JSON file ,

It should be like: $("#userName").i18n();

 

 

Display text for HTML elements

 

·        Display text within a container with class attribute

Like privously mentioned, this container should have a class attribute whose name should be the same as the name in JSON file .

e.g:

menu is defined in JSON file:

"menu": {  

    "firstName": "First Name:",

    "familyName": "Family Name:"   

  },

When we use menu.firstName and menu.familyName , we shuld make sure menu is a class name of the required container, the elements with in this container can use these two properties by adding an extra attribute:  data-i18n

<table width="100%" border="0" id="menu1" class="menu ">

                <tr id="firstName">

                                <td width="50%" data-i18n="menu.firstName "></td>

                                <td width="50%"><INPUT TYPE="text" id="userFirstName" data-i18n ="[value]userFirstName"></td>

                </tr>    

                <tr id="familyName">

                                <td width="50%" data-i18n="menu.familyName "></td>

                                <td width="50%"><INPUT TYPE="text" id="userFamilyName" data-i18n ="[value]userFamilyName"></td>

                </tr>                                                                                                    

</table>

 

 

·        Display text for an element with id attribute

The properties in JSON file should be defined as below:

"userFirstName": "Ivar",

  "userFamilyName": "Chen",

When we use userFirstName and userFamilyName , we shuld make sure userFirstName or userFamilyName is the id of this element, then add an extra attribute:  data-i18n

<INPUT TYPE="text" id="userFirstName " data-i18n ="[value]userFirstName">

<INPUT TYPE="text" id="userFamilyName " data-i18n ="[value]userFamilyName">

The value for added an extra attribute:  data-i18n should be like: "[value]userFirstName”

[value] means the attribute of this element,

userFirstName means the key of used property.

 

Display i18n message with parameters in javascript

 

Firstly we need to define the properties in JSON files:

"messages": { 

    "hello": "Hello, __name__" 

  }

  "hello": is the key

"Hello, __name__ " is the value , __name__ is the parameter , multiple paraters are supported.

In javascript, we can simply use method $.i18n.t() to get the message:

$.i18n.t("messages.hello", { name: $("#userFirstName").val() + " " + $("#userFamilyName").val() } )

The second paramter should be written as this format: {param1: param1value, param2:param2value}

 

Resource path custmization

 

set resGetPath in $.i18n.init() while initiation

分享到:
评论

相关推荐

    i18next:i18next:学习一次-到处翻译

    i18next:一次学习-随处翻译 i18next是一个非常流行的国际化框架,适用于浏览器或任何其他javascript环境(例如,node.js)。 i18next提供: 与灵活连接(通过xhr加载翻译,...) 可选,用户,... 适当的翻译,灵活...

    react-i18next:国际化已正确完成。 使用i18next i18n生态系统

    react-i18next 重要: Master Branch是使用钩子的新v10。 $ v10.0.0 npm i react-i18next react-native:要在react-native中使用钩子,必须使用react-native v0.59.0或更高版本 对于旧版本,请使用 $ v9.0.10 ...

    ng-i18next:使用i18next进行AngularJS的翻译

    ng-i18next-将i18next与一起使用 项目目标是提供一种将与使用的简便方法: ng-i18next指令i18next过滤器首先查看JanMühlemann的。特征AngularJS提供程序,指令和过滤器变量绑定(当变量更改时再次翻译) 嵌套翻译...

    koa-i18next:在Koajs中使用i18next的中间件

    入门安装依赖npm install koa-i18next与后端一起工作const koa = require ( 'koa' )const i18next = require ( 'i18next' )const Backend = require ( 'i18next-sync-fs-backend' ) // or i18next-node-fs-...

    vue-i18next:使用i18next i18n生态系统进行vue的国际化

    使用i18next i18n生态系统对vue进行国际化。 介绍 18next不仅提供了标准的i18n功能,例如(复数,上下文,插值,格式)。 它为您提供了一个完整的解决方案,可以将产品从Web本地化到移动设备和台式机。 vue-i18...

    storybook-addon-i18next:适用于i18next的React Storybook插件

    故事书插件i18next Storybook Addon i18next允许您使用以不同的语言显示您的故事。 注意:目前仅支持React。 安装 安装以下npm模块: npm i --save-dev storybook-addon-i18next 或与纱线: yarn add -D ...

    babel-plugin-react-i18next:一个babel插件,可帮助您全局调用一些与i18n相关的翻译功能

    babel-plugin-react-i18next 一个babel插件,可帮助您全局调用一些与i18n相关的翻译功能。 安装 纱 yarn add babel-plugin-react-i18next --dev NPM npm i babel-plugin-react-i18next --save-dev 用法 首先,...

    ng2-i18next:在Angular2中使用i18next

    1.安装npm软件包npm install ng2-i18next --save这还将安装三个i18next软件包(i18next,i18next-browser-languagedetector和i18next-xhr-backend),这是该模块正常工作所必需的。2.编辑您的angular-cli-build.js...

    next-i18next:翻译NextJs应用程序的最简单方法

    next-i18next 翻译NextJs应用程序的最简单方法。 如果您在生产中使用next-i18next,请考虑以您认为合适的任何数量。 这是什么? next-i18next是项目的一个插件,它使您可以快速轻松地启动并运行翻译,同时完全...

    phaser-i18next:用于使用i18next进行翻译的Phaser插件

    移相器i18next 移相器i18next是一个插件,可以让你有无缝翻译在你的游戏。 它使用作为翻译管理的源代码,在其他项目中JS社区也广泛采用了 。 主要特点: 支持翻译名称空间 简单键/值JSON 无缝切换语言 不需要...

    vue-i18next:集成I18Next的Vue插件

    它提供了一个全局Vue.t()函数和一个传递给i18next.t()的局部$t()函数,但全局 Vue 参数i18nextLanguage作为选项lng传递给i18next.t() $t()本地 Vue 选项i18nextNamespace被传递给i18next.t()作为选项ns 。...

    ember-i18next:将i18next集成到Ember CLI应用程序中

    ember-i18next 关于 一个插件,用于使用库国际化Ember.js应用程序。 该插件提供了一个包装i18next的Ember服务以及一个用于在模板中显示本地化文本的Handlebars帮助器。 兼容性 ember-i18next支持当前的Ember(发行版...

    i18next html 国际化

    前段页面的国际化,运用jquery-i18next js做国际化处理,i18n实现前端国际化,页面的多语言切换,本例切换了中英文,运用cookie存取使得刷新页面仍然保持当前语言 1、通过请求不同的语言文件,达到国际化前端的效果...

    前端项目-jquery-i18next.zip

    前端项目-jquery-i18next,i18next plugin for jquery usage

    eslint-plugin-i18next:适用于i18next的ESLint插件。 禁止为用户显示非本地化的文本

    eslint-plugin-i18next 适用于i18n的ESLint插件安装npm install eslint-plugin-i18next --save-dev用法将i18next添加到.eslintrc配置文件的plugins部分。 { " plugins " : [ " i18next " ]} 然后在“规则”部分下...

    国际化i18n之i18next-scanner使用案例

    vue框架插入i18next-scanner自动化生成key的demo (√ ) 解决页面使用$t('title')导致可读性差问题 (√ ) 解决国际化变量命名冲突问题 技术教程: ...

    elm-i18next:从i18next JSON文件生成Elm代码

    elm-i18next 从i18next JSON文件生成Elm。 安装 npm install @marreman/elm-i18next 使用 给定一个具有以下内容的JSON文件english.json 。 { " hello " : " Hello, World! " } 使用默认选项从JSON文件生成Elm。 ...

    actions-i18next:检查所有i18next密钥是否已翻译的动作

    tangro / actions-i18next @tangro动作,用于验证使用i18next-scanner收集的所有密钥是否都已翻译。版本您可以使用此操作的特定version 。 最新发布的版本是v1.0.7 。 您还可以使用latest ,以随时获得最新的版本。...

    angular-i18next:与i18next v19.4 +集成的angular v10 +

    angular-i18next v8.4 +与 v2.0 +的集成产品特点本机i18next承诺初始化支持活动支持命名空间延迟加载i18next本机支持document.title本地化错误处理策略i18next角度模块和组件的名称空间和范围(前缀) AOT支持支持还...

Global site tag (gtag.js) - Google Analytics