Internationalization by using database management for internationalized resource configuration and custom tags

Source: Internet
Author: User
Tags i18n locale tld

Internationalization by using database management for internationalized resource configuration and custom tags

The management of international resources is generally used . Properties to configure, for resource information modification or new internationalized resource files need to restart the service to take effect, will include international resources, including the system information stored in the database, And when the service is started to read and incorporate the cache, the system configuration information can be implemented immediately.

The storage table structure for internationalized resources is as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/80/wKiom1TUd9zzC7YcAAEuQb9zwz8656.jpg "title=" QQ picture 20150206155745.png "alt=" Wkiom1tud9zzc7ycaaeuqb9zwz8656.jpg "/>

Establishment of international resource management based on internationalized Resource table adding and deleting the module, updating the cache once modified, and updating the internationalization effect with custom tags. The internationalization of the backend is to read the resource from the cache and then replace it. The following international implementations of custom labels are mainly for pages and JS.

Ways to get international resources:

/** *  get all international resources  *  @author  chao.gao *  @date  2013-12-30  10:51:39  *  @return  Map<language,Map<moduleName,Map<key,value>>> */private  Map<string,map<string,string>> getallmsgs ()  {Map<String, Map<String,  String>> i8nmap = new hashmap<string, map<string, string>> (); Map<string, string> map = new hashmap<string, string> (); Locale defaultlocale = locale.getdefault (); i18nresourceentity queryentity = new  i18nresourceentity (); Queryentity.setlanguage (Defaultlocale.getlanguage ()); List<i18nresourceentity> i18nresourceentitylistlocacle = queryi18nresourcelist ( queryentity);if  (! Collectionutils.isempty (i18nresourceentitylistlocacle))  {for  (i18nresourceentity entity :  i18nresourceentitylistlocacle)  {map.Put (Entity.geti18nkey (),  entity.geti18nvalue ()), I8nmap.put (Entity.getmodulename (),  map);}} Return i8nmap;}


here 's a look at usingJSP2.0provided byTagextThe custom label of the function implements the internationalization effect of the page. Tagext Packages are related classes that provide custom tags, and when you need to develop Custom tags for jsps, your tablib class must inherit or implement Tagext The class in the package. tagext thatThe extension to tag .

First, we customize the label class and must inherit Simpletagsupport

In this example, the custom tag definition class code is as follows:

/**&NBSP;*&NBSP;&NBSP;*&NBSP;JS Internationalization Tags  *  @author  chao.gao *  @date  2014-1-14  Afternoon 2:14:22 *  @version  <b>1.0.0</b> */public class I18nJsTag  extends simpletagsupport implements ijsptag {private string id;private  string modulename;/** *  @author  chao.gao *  @date  2013-12-31  1:31:25  *  @see  javax.servlet.jsp.tagext.simpletagsupport#dotag ()  *  @throws  jspexception  *  @throws  ioexception */public void dotag ()  throws jspexception,  ioexception {i18njstagadapter.geti18njsptagadapter (). Dotag (This, getjspcontext ());} /** *  get id *  @return  string */public string getid ()  {return  ID;} /** *  setting id *  @param  id */public void setid (string id)  { This.id = id;} /** *&nbSP; get modulename *  @return  string */public string getmodulename ()  {return  modulename;} /** *  set modulename *  @param  modulename */public void setmodulename ( String modulename)  {this.modulename = modulename;}}

Custom Label Processing class , a simple custom label can define the logic directly in Dotag

/** *  @author  chao.gao *  @date  2013-12-31  afternoon 3:58:06 *  @version  <b>1.0.0</b> */public class I18nJspTagAdapter {private static  final map<string, map<string, string>> i18nmap = new  Concurrenthashmap<string, map<string, string>> ();  private static final  i18njsptagadapter i18njsptagadapter = new i18njsptagadapter () {};p rivate  I18njsptagadapter ()  {}/**  *  *  @author  chao.gao *  @date   2013-12-12  pm 9:54:27 *  @see  com.gaochao.platform.web.tag.ijsptagspi#dotag ( Com.fx.platform.web.tag.ijsptag, javax.servlet.jsp.jspcontext)  *  @param  jsptag *   @param  jspContext *  @throws  ioexception */public void dotag (ijsptag  jsptag, jspcontext jspcontext)  throws ioexception {i18ntag i18ntag =  (I18ntag)  jspTag; String modulename = i18ntag.getmodulename (); String key = i18ntag.getkey ();if  (Stringutils.isblank (modulename)  | |  stringutils.isblank (key)  | |  stringutils.isempty (modulename)  | |  stringutils.isempty (key))  {return;} String message = i18nmap.get (modulename)  == null ?  ""  :  I18nmap.get (ModuleName). get (key); Jspcontext.getout (). write (message); Public static i18njsptagadapter geti18njsptagadapter ()  {return i18njsptagadapter;} Public void puti18nresourcemap (Map<string, map<string, string>> i18nmap)  {i18njsptagadapter.i18nmap.putall (I18nmap);}}

Then write the descriptor configuration file Tagext.tld placed under Meta-inf

<?xml version= "1.0"  encoding= "UTF-8"? ><taglib xmlns= "Http://java.sun.com/xml/ns/j2ee"  xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"  xsi:schemalocation= "http://java.sun.com/xml/ Ns/j2eeweb-jsptaglibrary_2_0.xsd " version=" 2.0 "> <description>fx jsp tag  </description><tlib-version>2.2</tlib-version><short-name>gaochao</short-name> <jsp-version>2.0</jsp-version><uri>/gaochao</uri><tag><name>i18n</name ><tag-class>com.gaochao.platform.web.tag.impl.i18ntag</tag-class><body-content>empty</ body-content><attribute><name>modulename</name><required>true</required>< Rtexprvalue>true</rtexprvalue></attribute><attribute>    <name> Id</name>    <required>false</required></attribute><attribute ><name>key&lT;/name><required>true</required><rtexprvalue>true</rtexprvalue></attribute> </tag><tag><name>i18nJs</name><tag-class> Com.gaochao.platform.web.tag.impl.i18njstag</tag-class><body-content>empty</body-content> <attribute><name>moduleName</name><required>true</required><rtexprvalue> True</rtexprvalue></attribute><attribute>    <name>id</name>     <required>false</required></attribute></tag></taglib>

 

 Add internationalization information language= "zh", modulename= "sys", key= "type", value= "types:"

Then add it on the JSP page

<%@ taglib uri= "/gaochao" prefix= "G"%>

  Original code:

<td> type:</td>

  After replacement:

<td><p:i18n modulename= "sys" key= "type" ></td>

The above is the JSP page internationalization tag, the following is the JS custom label logic processing class, its definition class basic and the JSP custom label is the same, and the description information in the above TAGEXT.TLD has been defined.

/**  *  *  @author  chao.gao *  @date  2013-12-12  9:54:27  *  @see  com.gaochao.platform.web.tag.ijsptagspi#dotag (com.fx.platform.web.tag.ijsptag,  Javax.servlet.jsp.JspContext)  *  @param  jspTag *  @param  jspcontext * @ Throws ioexception */public void dotag (ijsptag jsptag, jspcontext  Jspcontext)  throws IOException {I18nJsTag i18nTag =  (I18njstag)  jspTag; String modulename = i18ntag.getmodulename ();if  (Stringutils.isblank (moduleName)  | |  stringutils.isempty (modulename))  {return;} Map<string, string> message = i18nmap.get (ModuleName); Stringbuilder msgobject = new stringbuilder (""); Msgobject.append ("<script type=") Text/javascript ' > \n '); if (message != null) {msgobject.append ("var i18njs={"); Iterator <string>&nbSp;it = message.keyset (). iterator (); while (It.hasnext ()) {string element = it.next (); Msgobject.append ("\" " + element + " \ ": \" " + message.get (Element)  + " \ " ,");} Msgobject.deletecharat (Msgobject.length ()  - 1); Msgobject.append ("}"); Msgobject.append ("</script>"); Jspcontext.getout (). Write (msgobject.tostring ());}

From the above logic can be seen through the custom tag, we generate a section of JS code, in the code to define a map-like variable, in the JSP referenced by JS can use I18NJS this variable to obtain the desired internationalization information.

This article is from the "South Lake Miner Technology Space" blog, please be sure to keep this source http://jncumter.blog.51cto.com/812546/1612468

Internationalization by using database management for internationalized resource configuration and custom tags

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.