1. Design of the original
because jfinal 's internationalization (i18n) support in JSP is not good, therefore, the idea of solving this short board.
Implementation also considered several ways, and ultimately decided to use the most original JSP tags. Because the custom label is easy to implement in the JSP , the content is flexible and the function is more powerful, extensibility is good.
2. i18nlabel
Custom i18n tags require the best support for each interface of the i18n, using <jf:i18n/> as the label name, the following is Several interfaces of the i18n class in jfinal :
public static string GetText (string key) public static string GetText (string key, string defaultvalue) public static string GetText (string key, locale locale) public static string GetText (string key, string defaultvalue, locale locale)
The complete label is designed to:
<jf:i18n key= "" defaultvalue= "" locale= "" paras= ""/>
Parameter description:
action |
|
corresponding interface key |
corresponding interface defaultvalue |
&NBSP; |
Locale |
in the corresponding interface locale |
There are different data types, and only strings can be used here, such as: ZH_CN, en_US |
Paras |
New properties, designed to support parameter substitution |
1. If the key corresponds to the string "Good morning!" {0} They are now experimenting with {1}. The Paras value is " small white, i18n label " (The parameters are separated by commas); Then the end result is "Good morning!" Xiao Bai , now experimenting with i18n tags . " 2.In addition, parameters can be taken from the controller's attr, for example: Controller setattr ("P1", "Small white"). SetAttr ("P2", "i18n Label"), the Paras value in the label is "P1,P2"The end result is Good morning!" Small whiteWe're experimenting now, guys.i18n Label. " |
3. Code implementation3.1 I18ntag.java
Package com.jfinal.tag;import java.io.ioexception;import javax.servlet.jsp.jspexception;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.Tag;import javax.servlet.jsp.tagext.tagsupport;import com.jfinal.i18n.i18n;import com.jfinal.kit.localekit; The label used in the import com.jfinal.kit.stringkit;/** * jsp page to support i18n. */public class I18nTag extends TagSupport {private static final Key value in long serialversionuid = -8073376431317433802l;/** * i18n */private string key;/** * default value to use when the key value does not exist */private String defaultValue;/** * Regional attributes, such as:zh_cn, en_us */private string locale;/** * , are used as parameters for value formatting values, and many are separated by commas, If the parameter value has a corresponding attribute in the request, take the attribute value */private String paras; @SuppressWarnings ("deprecation" ) @Overridepublic int dostarttag () throws jspexception {//if , (Stringkit.isblank (This.getkey ())) {//throw new jspexception ("The tag attribute of key is not exists. "); /}// defines the textstring text = null;try {if (Stringkit.isblank (locale)) output to the page {// Get value Text = i18n.gettext (key, this.defaultvalue) via i18n interface;} else {// locale defines a value that specifies the type of language to be displayed by the front end, and the language type is referred to Localekit processing text = i18n.gettext (key, This.defaultvalue, i18n.localefromstring (locale));}} catch (Exception e) {text = defaultvalue;} if (Stringkit.notblank (paras)) {// if paras is specified in tag, paras is resolved to arraystring[] attrs = paras.split (",");object[] values = new object[attrs.length];// loop takes the parameter to the request value If there is a value, replace for (int i = 0; i < attrs.length; i++) {String a = attrs[i];values[i] = pagecontext.getrequest (). getattribute (a) &nbsP;== null ? attrs[i] : pagecontext.getrequest (). getattribute (a);} Pagecontext.getattribute (""); Text = string.format (text, values);} try {// outputs the results to page pagecontext.getout (). write (text);} catch (Ioexception e) {return tag.skip_body;} Return tag.eval_body_include;} Public string getkey () {return this.key;} Public void setkey (String key) {this.key = key;} Public string getdefaultvalue () {return this.defaultvalue;} Public void setdefaultvalue (String defaultvalue) {this.defaultValue = DefaultValue;} Public string getlocale () {return this.locale;} Public void setlocale (String locale) {this.locale = locale;} Public string getparas () {return this.paras;} Public void setparas (String paras) {this.paras = paras;}
3.2 Jfinal.tld
The file is placed in the same directory as the I18ntag.java:
<?xml version= "1.0" encoding= "UTF-8"? ><! doctype taglib public "-//sun microsystems, inc.//dtd jsp tag library 1.1//en " " HTTP://JAVA.SUN.COM/J2EE/DTDS/WEB-JSPTAGLIBRARY_1_1.DTD "><taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</ Jspversion> <shortname>bean</shortname> <uri >http://www.jfinal.com/tag</uri> <tag> <name>i18n</name> <tagclass >com.jfinal.tag.I18nTag</tagclass> <bodycontent> jsp</bodycontent> <attribute> <name>key</name>&nBsp; <required>true</required> <rtexprvalue></rtexprvalue > </attribute> <attribute> <name >defaultValue</name> < required>false</required> < rtexprvalue></rtexprvalue> </attribute> <attribute> <name>paras</name> <required>False</required> <rtexprvalue ></rtexprvalue> </attribute> <attribute> <name>locale</name> <required>false</required> <rtexprvalue></rtexprvalue> </attribute > </tag></taglib>
4. Use of Tag4.1 Defining a multi-language properties file
Greeting=today is%2$s/%3$s/%1$s
Greeting= today is%1 $ s%2$s month%3$s Day
4.2 Loading I18 at startup
Add the following code to Jfinalconfig.configconstant (Constants me):
Loaded in i18n file me.seti18n ("myi18n", Locale.simplified_chinese, Integer.max_value);
4.3 setting properties in the controller
Calendar C = calendar.getinstance (), C.settime (New Date ()), this.setattr ("yyyy", C.get (Calendar.year)), This.setattr (" MM ", C.get (Calendar.month) + 1); This.setattr (" DD ", C.get (Calendar.day_of_month));
4.4 Defining tags in JSP pages
The first introduction of the label, introduced is the location of the label definition file TLD, after compilation, the location of the TLD is "/web-inf/classes/com/jfinal/tag/jfinal.tld" , it is also the target location.
4.5 Execution Results
5. Questions
The attentive reader may have discovered that the Paras parameter was substituted for the type parameter {0},{1}, but the result was replaced with a parameter such as the%1 $ s year%2$s. This is because the code uses the String.Format () format, but it does not know why {0},{1} is not supported. The author has no time to explore, which reader has time to help me find out the answer.