EWeb4J 国际化支持出炉啦(有源码看)
很荣幸也很开心地公布,EWeb4J 支持国际化了。并且有着它独特的味道。首先我们来看看最终的效果。
PS:
PS:后面的 country 是可选的
2.其次,准备好你的 properties 文件,老规矩,在 start.xml:
注意,这里的file的所有命名不需要跟国际化有任何关系。
3.虽然上面的配置文件里不需要写什么国际化语言,但是在你的文件系统里,你至少得准备好两份文件:
!!正是因为这个代理类 MapProxy 的存在,所以在使用 Props.getMap("Message") 的 get("welcome") 的时候才会去找到匹配的国际化资源信息。这个很重要!!package org.eweb4j.i18n;import java.util.List;import java.util.Locale;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.eweb4j.config.ConfigConstant;import org.eweb4j.config.Log;import org.eweb4j.config.LogFactory;import org.eweb4j.config.bean.I18N;import org.eweb4j.mvc.Context;import org.eweb4j.mvc.MVC;import org.eweb4j.mvc.MVCCons;/** * EWeb4j 国际化支持 * @author weiwei * */public class Lang {private static Log log = LogFactory.getMVCLogger(Lang.class);private static ThreadLocal<Locale> current = new ThreadLocal<Locale>();public static Locale get(){Locale locale = current.get();if (locale == null){Context ctx = MVC.current.get();if (ctx != null){HttpServletRequest req = ctx.getRequest();HttpServletResponse res = ctx.getResponse();if (req != null){resolve(req, res);}else{setDefaultLocale();}}else{setDefaultLocale();}locale = current.get();}return locale;}private static void resolve(HttpServletRequest req, HttpServletResponse res) {Cookie[] cookies = req.getCookies();if (cookies != null){for (Cookie cookie : cookies){if (MVCCons.COOKIE_KEY_LOCALE.equals(cookie.getName())){String loc = cookie.getValue();if (loc == null)continue;if (loc.indexOf("_") > 0){String[] locs = loc.split("_");if (set(new Locale(locs[0], locs[1]))){return ;}}if (set(new Locale(loc)))return;}}}Locale locale = req.getLocale();set(locale);res.addCookie(new Cookie(MVCCons.COOKIE_KEY_LOCALE, locale.toString()));}public static boolean set(Locale locale){if (I18N.get().contains(locale)){log.debug("Locale is set -> %s", locale.toString());current.set(locale);return true;}log.warn("Locale %s is not defined in your %s > i18n", locale, ConfigConstant.START_FILE_NAME);return false;}public static void clear(){current.remove();}public static void change(Locale locale){if (get() == null){if (set(locale))MVC.current.get().getResponse().addCookie(new Cookie(MVCCons.COOKIE_KEY_LOCALE, locale.toString()));}else{if (!get().equals(locale)){if (set(locale))MVC.current.get().getResponse().addCookie(new Cookie(MVCCons.COOKIE_KEY_LOCALE, locale.toString()));}}}private static void setDefaultLocale() {List<org.eweb4j.config.bean.Locale> locales = I18N.get().getLocale();if (locales == null || locales.isEmpty()){set(Locale.getDefault());} else{set(new Locale(locales.get(0).getLanguage(), locales.get(0).getCountry()));}}}