Javascript gets the prompt for unified management of message

  • 2020-12-18 01:44:15
  • OfStack


In the project development, each page, each business operation will use the prompt. With so many message, a better approach is to manage these messages in one package. In this way, it is also convenient to do unified 1 processing when doing internationalization.

The recommendation uses a database to manage all the prompts and loads them into memory or cache storage when the project starts.

There's nothing to say about how it's implemented in Java, it's simple. So how do we use these tips in Javascript? The following is a simple encapsulation with clear thinking and simple use for your reference.

1. Create ES12en. jsp with the following code:


<%-- <%@ 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();
//  You can use it here Java The method of the code reads the message prompt that has been cached by the server 
<%-- 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 you can set it statically 1 Some of the clues 
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 successful" and "Operation failed" in the code are the values I statically wrote dead. In jsp, we use dynamic Java code to read all message prompts that have been fully cached by the server after system startup, and set its dynamic output to this.message [' xxxx'].

2. How to use it in the page


window.onload = function(){
i18n.alert('M0001');
//alert(i18n.get('M0001'));
}

We can use our defined get and alert methods directly, but we can extend our own methods as needed.


Related articles: