1.在Action类的类路径下,建Basename.language-country.properties文件
例如:zhang.en_US.properties,zhang.zh.CN.properties文件
2.在struct.xml文件中配置要读取的国际化资源文件
代码:
3.在页面上可以读取国际化资源文件的属性
<s:text name=“title”></s:text>
<s:form action=“UserLogin.action” method=“post”>
<s:textfield name=“username” key=“username”></s:textfield>
<s:textfield name=“birthday” key=“birthday”></s:textfield>
<s:password name=“password” key=“password”></s:password>
<s:submit key=“submit”></s:submit>
</s:form>
English
中文
to other page
4.通过超连接的方式类决定语言选择的实现:
English
中文
原理:在struct框架中,在执行action方法之前要经过一个i18n拦截器,
首先在session中获取是否有location,如果存在直接使用
如果 不存在的话,在action的请求参数中获取是否有:request_locale的属性,如果存在这个属性的话
strucct将会把这个属性对应的值转化成一个Location对象,并把它保存在session,而且保存在
actioncontext中,一遍在页面上显示,
源码:
protected Locale getLocaleFromParam(Object requestedLocale) {
Locale locale = null;
if (requestedLocale != null) {
locale = (requestedLocale instanceof Locale) ?
(Locale) requestedLocale :
LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
if (locale != null && LOG.isDebugEnabled()) {
LOG.debug(“applied request locale=#0”, locale);
}
}
if (locale != null && !Arrays.asList(Locale.getAvailableLocales()).contains(locale)) {
locale = Locale.getDefault();
}
return locale;
}