回顾一下国际化的大概流程:client:IE/FF向服务器发送请求的同时,在html协议里的header里有accept-language字段,服务器接收后,把消息传递给Struts,Struts根据浏览器的发送过来的支持语言种类里选择对应的ApplicationResources_XX_XX_XX.properties。如果Struts里没有配置了对应的properties文件的话就采用默认的properties文件。在properties文件里的书写方式是如:error.username.null=username required!一行一对'键值对'。等号前面是key,等号后面是value。有了这个便于coder书写消息的规定之后,Struts对properties文件的解析就方便多了。下面来看看Struts是怎么取得Message的:
org.apache.struts.util.MessageResources里的getMessage方法
大概说一下里面的处理流程把:在MessageResources类里,把properties解析后的'键值对'放在一个HashMap里,取出的时候,根据key来取。如果有复合消息文本的话就先对其'格式化'了在输出!
以上纯粹个人理解,谁看到错误了请不吝指正。共同进步!
org.apache.struts.util.MessageResources里的getMessage方法
public String getMessage(Locale locale, String key, Object[] args) {
// Cache MessageFormat instances as they are accessed
if (locale == null) {
//源文件289行:protected Locale defaultLocale = Locale.getDefault();
locale = defaultLocale};
}
/*
源文件460到462中定义的方法体:
protected String messageKey(Locale locale, String key) {
return (localeKey(locale) + "." + key);
}
*/
MessageFormat format = null;
String formatKey = messageKey(locale, key);
synchronized (formats) {
// 86行定义了用于存放国际化消息的哈希表:protected HashMap formats = new HashMap();
format = (MessageFormat) formats.get(formatKey);
if (format == null) {
String formatString = getMessage(locale, key);
if (formatString == null) {
return returnNull ? null : ("???" + formatKey + "???");
}
format = new MessageFormat(escape(formatString));
format.setLocale(locale);
formats.put(formatKey, format);
}
}
return format.format(args);
}
大概说一下里面的处理流程把:在MessageResources类里,把properties解析后的'键值对'放在一个HashMap里,取出的时候,根据key来取。如果有复合消息文本的话就先对其'格式化'了在输出!
以上纯粹个人理解,谁看到错误了请不吝指正。共同进步!