JavaScript get unified management prompt (message) _javascript tips

Source: Internet
Author: User
Tags i18n


In the project development, each page, each business operation will use the prompt language. In the face of so many messages, a better way is to manage them uniformly. So in the internationalization of the time to do a unified processing also become convenient.

The recommended scenario uses a database to manage all the prompts and load all prompts into memory or cache storage when the project is started.

There is nothing to say about how to implement in Java, it's simple. So how do we use these hints in JavaScript, the following is a simple package, clear thinking, the use of simple, for your reference.

1, create message.jsp, the code is as follows:

<%--<%@ page import= "Com.xxx.xxx.xxx.xxx.I18nManager"%> "--%> <%@ page language=" java "pageencoding="
UTF-8 "%> function I18nmanager () {this.init; this.messages;}; I18nmanager.prototype = {get:function (id, args) {if (!this.init) {this.messages = new Array ()//This can be used to read the service in Java code The end has been cached good message prompt <%--this.messages[' charset ' = "<%= i18nmanager.get (User," CharSet ")%>"; --%> <%--this.messages[' "=" <%= i18nmanager.get (User, "")%> "; --%> <%--this.messages[' M0001 '] = "<%= i18nmanager.get" (User, "M0001")%> "; --%> <%--this.messages[' M0002 '] = "<%= i18nmanager.get" (User, "M0002")%> "; --%> <%--this.messages[' M0003 '] = "<%= i18nmanager.get" (User, "M0003")%> "; --%> <%--this.messages[' M0004 '] = "<%= i18nmanager.get" (User, "M0004")%> "; --%> <%--this.messages[' M0005 '] = "<%= i18nmanager.get" (User, "M0005")%> "; --%> <%--this.messages[' M0006 '] = "<%= i18nmanager.get (User," M0006 ") %> "; --%> <%--this.messages[' ui.title.user '] = "<%= i18nmanager.get" (User, "Ui.title.user")%> "; --%> <%--this.messages[' ui.title.xview '] = "<%= i18nmanager.get" (User, "Ui.title.xview")%> "; --%> <%--this.messages[' ui.title.xview.memo '] = "<%= i18nmanager.get" (User, "Ui.title.xview.memo")%> "; --%> <%--this.messages[' ui.title.xviewPointList '] = "<%= i18nmanager.get (User," ui.title.xviewPointList ")% > "; --%> <%--this.messages[' ui.title.sherpaOracle '] = "<%= i18nmanager.get (User," Ui.title.sherpaOracle ")%> "; --%>//or can be statically set some hint language this.messages[' M0001 ' = "Operation successful!" 
"; this.messages[' M0001 ' = ' operation failed! 
";
This.init = true;
var message = This.messages[id];
if (!message && message!== "") {return ID;}  if (args) {if (typeof args = = "Object" && args.length) {for (var i = 0, i < args.length; i++) {var pattern = 
New RegExp ("\\{" + i + "\}", "G");
Message = Message.replace (pattern, args[i]); } else {MeSsage = Message.replace (/\{0\}/g, args);
} return message;
}, Alert:function (ID, args) {alert (This.get (ID, args));}; var i18n = new I18nmanager ();

"Operation Success" and "Operation failure" in code are the values that I statically write dead. We use dynamic Java code in JSP to read all message prompts that have been cached by the server after the system started and set their dynamic output to this.message[' xxxx '.

2, how to use in the page

Window.onload = function () {
i18n.alert (' M0001 ');
Alert (I18n.get (' M0001 '));
}

Can be used directly using our defined get and alert methods, and of course we can expand our approach to the actual needs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.