由于项目有需求,看了下jfinal的国际化,没有格式化的功能,比较纠结,在已经的基础上修改了下。下面直接上干货,以插件的方式加载到jfinal就可以了
package com.jfinal.plugin.i18n; import java.util.Locale; import com.jfinal.core.Const; import com.jfinal.plugin.IPlugin; public class I18nPlugin implements IPlugin{ String string; Locale locale = Locale.CHINA; Integer max_age = Const.DEFAULT_I18N_MAX_AGE_OF_COOKIE; public I18nPlugin(String string ){ this.string = string; } public I18nPlugin(String string ,Locale locale,Integer max_age){ this.string = string; this.locale = locale; this.max_age = max_age; } public boolean start() { I18nKit.init(string, locale, max_age); return true; } public boolean stop() { return true; } public static void main(String[] args) { new I18nPlugin("i18n.common").start(); System.out.println(I18nKit.getText("test1")); System.out.println(I18nKit.getText("test",Locale.FRANCE)); System.out.println(I18nKit.getText("test")); } }
package com.jfinal.plugin.i18n; import java.util.Enumeration; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import com.jfinal.common.MessageFormatKit; import com.jfinal.core.Const; import com.jfinal.log.Logger; public class I18nKit { private static Logger logger = Logger.getLogger(I18nKit.class); private static String baseName; private static Locale defaultLocale = Locale.getDefault(); private static int i18nMaxAgeOfCookie = Const.DEFAULT_I18N_MAX_AGE_OF_COOKIE; private static final NullResourceBundle NULL_RESOURCE_BUNDLE = new NullResourceBundle(); private static final Object[] NULL_OBJECTARR = new Object[0]; private static final ConcurrentMap<String, ResourceBundle> bundlesMap = new ConcurrentHashMap<String, ResourceBundle>(); public static void init(String baseName, Locale defaultLocale, Integer i18nMaxAgeOfCookie) { I18nKit.baseName = baseName; if (defaultLocale != null) I18nKit.defaultLocale = defaultLocale; if (i18nMaxAgeOfCookie != null) I18nKit.i18nMaxAgeOfCookie = i18nMaxAgeOfCookie; } public static Locale getDefaultLocale() { return defaultLocale; } final static public int getI18nMaxAgeOfCookie() { return i18nMaxAgeOfCookie; } private static ResourceBundle getResourceBundle(Locale locale) { String resourceBundleKey = getresourceBundleKey(locale); ResourceBundle resourceBundle = bundlesMap.get(resourceBundleKey); if (resourceBundle == null) { try { resourceBundle = ResourceBundle.getBundle(baseName, locale); bundlesMap.put(resourceBundleKey, resourceBundle); } catch (MissingResourceException e) { resourceBundle = NULL_RESOURCE_BUNDLE; } } return resourceBundle; } private static String getresourceBundleKey(Locale locale) { return baseName +'_'+ locale.toString(); } public static String getText(String key) { return getText(key, defaultLocale, NULL_OBJECTARR); } /** * 有格式化的国际化 * @param key * @param locale * @param objects * @return */ public static String getText(String key, Locale locale , Object... objects) { try{ String string = getString(key, locale); if(objects.length >0){ return MessageFormatKit.format(string, locale,objects); }else{ return string; } }catch (java.util.MissingResourceException e) { logger.error(e.getMessage()); return key; } } /** * 无格式化的国际化 * @param key * @param locale * @return */ public static String getString(String key){ return getString(key, getDefaultLocale()); } public static String getString(String key, Locale locale){ try{ return getResourceBundle(locale).getString(key); }catch (java.util.MissingResourceException e) { logger.error(e.getMessage()); return key; } } public static String getText(String key, Locale locale) { return getText(key, locale, NULL_OBJECTARR); } private static class NullResourceBundle extends ResourceBundle { public Enumeration<String> getKeys() { return null; } protected Object handleGetObject(String key) { return null; } } }
package com.jfinal.common; import java.text.MessageFormat; import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import com.jfinal.log.Logger; import com.jfinal.plugin.i18n.I18nKit; /** * 字符格式化,主要用于国际化 * @author Administrator * */ public class MessageFormatKit { private static Logger logger = Logger.getLogger(MessageFormatKit.class); private static final ConcurrentMap<MessageFormatKey, MessageFormat> messageFormats = new ConcurrentHashMap<MessageFormatKey, MessageFormat>(); public static String format(String value,Object...strings){ return format(value, I18nKit.getDefaultLocale(), strings); } public static String format(String value,Locale locale,Object...strings){ try { MessageFormat mf = buildMessageFormat(value, locale); return formatWithNullDetection(mf, strings); } catch (Exception e) { logger.error("formart error :"+value); } return value; } private static String formatWithNullDetection(MessageFormat mf, Object... args) { String message = mf.format(args); if ("null".equals(message)) { return null; } else { return message; } } private static MessageFormat buildMessageFormat(String pattern, Locale locale) { MessageFormatKey key = new MessageFormatKey(pattern, locale); MessageFormat format = messageFormats.get(key); if (format == null) { format = new MessageFormat(pattern); format.setLocale(locale); format.applyPattern(pattern); messageFormats.put(key, format); } return format; } static class MessageFormatKey { String pattern; Locale locale; MessageFormatKey(String pattern, Locale locale) { this.pattern = pattern; this.locale = locale; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof MessageFormatKey)) return false; final MessageFormatKey messageFormatKey = (MessageFormatKey) o; if (locale != null ? !locale.equals(messageFormatKey.locale) : messageFormatKey.locale != null) return false; if (pattern != null ? !pattern.equals(messageFormatKey.pattern) : messageFormatKey.pattern != null) return false; return true; } public int hashCode() { int result; result = (pattern != null ? pattern.hashCode() : 0); result = 29 * result + (locale != null ? locale.hashCode() : 0); return result; } } }