Javascript obtains the message and javascriptmessage of unified management.

Source: Internet
Author: User
Tags i18n

Javascript obtains the message and javascriptmessage of unified management.

 
During project development, prompts are provided for various page and business operations. A better way to deal with so many messages is to manage these messages in a unified manner. In this way, unified processing becomes more convenient during internationalization.

We recommend that you use a database to manage all the prompts and load all the prompts to the memory or cache when the project starts.

There is nothing to say about how to implement in Java, which is very simple. So how can we use these prompts in Javascript? The following is a simple encapsulation, clear thinking, and easy to use for your reference.

1. Create message. 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 (); // here you can use the Java code to read the message prompts that have 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 some static prompts for this. message S ['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 ();

In the Code, "operation succeeded" and "operation failed" are static write dead values. In jsp, we use dynamic Java code to read all the message prompts cached by the server after the system is started, and set the dynamic output to this. message ['xxxx.

2. How to Use

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

You can use the get and alert methods we have defined to directly use them. Of course, you can expand your methods as needed.

Articles you may be interested in:
  • Javascript MessageBox effect code
  • ExtJS Ext. MessageBox. alert () dialog box details
  • Extjs Ext. MessageBox. confirm confirmation dialog box
  • Example of cross-origin communication using HTML 5 window. postMessage in JavaScript
  • How to Implement JavaScript by entering a prompt in the text box (Password box)
  • Prompt in the JavaScript implementation input box (Password box)

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.