1、编写properties资源文件
yourfilename_zh_CN.properties test=您好 yourfilename_en_US.properties test=hello2、在JFinal配置类,配置常量中
public void configConstant(Constants me) { ... //后面两个参数根据自己情况添加默认语言国家 I18N.init("youfilename", null, null); ... }如果资源文件不在classpath下面那么需要加上包名称,例如 I18N.init("i18n.youfilename", null, null);
/*** * * @author yinjun622 2013-05-25 */ public class GlobalInterceptor implements Interceptor { @Override public void intercept(ActionInvocation ai) { Controller c = ai.getController(); String tmp = c.getCookie(Const.I18N_LOCALE); String i18n = c.getRequest().getLocale().toString(); if (!i18n.equals(tmp)) { ai.getController().setCookie(Const.I18N_LOCALE, i18n, Const.DEFAULT_I18N_MAX_AGE_OF_COOKIE); } ai.invoke(); } }这样我们会在用户的cookie中记录所用语言信息当Controller.getText("key");自动去cookie中查找语言并显示。
我们还可以直接使用I18N.getText("key",locale);
5、让beetl实现国际化
beetl配置到JFinal在这里就不多说了,beetl自定义一个函数I18n,
并配置 gt.registerFunction("i18n", new I18n());
public class I18n implements Function { @Override public Object call(Object[] obj, Context context) { HttpServletRequest req = (HttpServletRequest )context.getVar("request"); return I18N.getText((String) obj[0],req.getLocale()); } }
在页面调用
${i18n("test")}