本文主要是介绍Spring 读取i18n国际化资源文件的工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
现有一个国际化资源文件demoUser_zh_CN.properties如下:
demoUser.create.success=创建成功
demoUser.update.success=更新成功
demoUser.delete.success=删除成功
demoUser.start.success=送审成功
demoUser.approval.success=审核成功
demoUser.claim.success=签收成功
demoUser.assignUser.success=分配成功
demoUser.backNode.success=回退成功
demoUser.resolve.success=处理成功
demoUser.assign.success=转办成功
demoUser.signal.success=流转成功
现在我们需要在程序中根据key读取相应的value,因此现在我们写一个加载i18n文件的工具类,以便我们进行操作,此工具类是基于Spring Framework来编写的,具体的代码如下:
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;public class MessageUtil{protected static MessageSourceAccessor accessor;protected static MessageSource messageSource;protected static Set<String> set = new HashSet<String>();private static int defaultStatus = 1;static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();//静态块初始化资源static{addLocation("classpath*:i18n/*");}//初始化资源文件的存储器protected static void initMessageSourceAccessor(){String[]basenames = new String[set.size()];set.toArray(basenames);ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();reloadableResourceBundleMessageSource.setBasenames(basenames);reloadableResourceBundleMessageSource.setCacheSeconds(5);accessor = new MessageSourceAccessor(reloadableResourceBundleMessageSource);messageSource = reloadableResourceBundleMessageSource;}//删除指定位置的资源public static void removeLocation(String locationPattern){try {Resource[] resources = resourcePatternResolver.getResources(locationPattern);for (int i = 0; i < resources.length; i++) {String url = resources[i].getURL().toString();int lastIndex = url.lastIndexOf("/");String prefix = url.substring(0,lastIndex + 1);String suffix = url.substring(lastIndex+1);suffix = suffix.split("\\.")[0].split("_")[0];set.remove(prefix + suffix);}} catch (IOException e) {throw new RuntimeException(e);}initMessageSourceAccessor();}//加载指定位置的资源文件public static void addLocation(String locationPattern){try {Resource[] resources = resourcePatternResolver.getResources(locationPattern);for (int i = 0; i < resources.length; i++) {String url = resources[i].getURL().toString();int lastIndex = url.lastIndexOf("/");String prefix = url.substring(0,lastIndex + 1);String suffix = url.substring(lastIndex+1);suffix = suffix.split("\\.")[0].split("_")[0];set.add(prefix + suffix);}} catch (IOException e) {throw new RuntimeException(e);}initMessageSourceAccessor();}private MessageUtil(){}public static Message message(int status,String code,Object[] args,Object data){return new Message(status, getMessage(code,args), data);}public static Message message(int status,String code,Object data){return new Message(status, getMessage(code), data);}public static Message message(int status,String code,Object[] args){return new Message(status, getMessage(code,args));}public static Message message(int status, String code) {return new Message(status, getMessage(code));}public static Message message(int status,Object data){return new Message(status,"1",data);}public static Message message(String code){return new Message(defaultStatus, getMessage(code));}public static Message message(String code,Object[] args){return new Message(defaultStatus, getMessage(code, args));}public static Message message(String code,Object[] args,Object data){return new Message(defaultStatus, getMessage(code, args), data);}public static Message message(String code,Object data){return new Message(defaultStatus, getMessage(code), data);}public static String getMessage(String code){return accessor.getMessage(code,LocaleContextHolder.getLocale());}public static String getMessage(String code,String defaultMessage){return accessor.getMessage(code,defaultMessage,LocaleContextHolder.getLocale());}public static String getMessage(String code, Object[] args){return accessor.getMessage(code,args,LocaleContextHolder.getLocale());}public static String getMessage(String code, Object[] args,String defaultMessage){return accessor.getMessage(code, args, defaultMessage, LocaleContextHolder.getLocale());}public static MessageSource getMessageSource() {return messageSource;}}
具体使用:
//新增@RequestMapping(method = RequestMethod.POST)public @ResponseBody Message create(DemoUser demoUser){demoUser.setProcessState(ProcessBizEngineService.PROCESS_NEW_STATE);demoUserService.save(demoUser);return MessageUtil.message("demoUser.create.success");}
这篇关于Spring 读取i18n国际化资源文件的工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!