zoukankan      html  css  js  c++  java
  • 项目记录:springmvc+freemarker 实现国际化

    第一步,在SpringMVC的配置文件中,添加如下支持国际化的两段配置

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="useCodeAsDefaultMessage" value="true" />
    <property name="basenames" value="messages"></property>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    <property name="defaultLocale" value="zh_CN" />
    </bean>

    解释:第一个messages,表示放置国际化内容的文件要以这个字符串开头,如 messages_zh_CN.properties, messages_en_US.properties 

    第二个,这个表示依据本地文件来进行国际化,即自己提供不同的包含国际化内容的文件。zh_CN 即是手动选择要显示中文。当然显示英文此处改为en_US。

    有些国际化是在英文浏览器下就显示英文,中文浏览器下显示中文,那么这里就不是第二段这样的配置了,要更换,需要配置按cookie 或 session 等配置国际化。

    第二步,需要一个工具类 SpringUtils。有了该类,就可以直接在freemarker文件中,用 ${variable} 显示国际化内容了。该类源码如下

    public final class SpringUtils implements ApplicationContextAware, DisposableBean {

    /** applicationContext */
    private static ApplicationContext applicationContext;

    /**
    * 不可实例化
    */
    private SpringUtils() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
    SpringUtils.applicationContext = applicationContext;
    }

    public void destroy() throws Exception {
    applicationContext = null;
    }

    /**
    * 获取applicationContext
    *
    * @return applicationContext
    */
    public static ApplicationContext getApplicationContext() {
    return applicationContext;
    }

    /**
    * 获取实例
    *
    * @param name
    * Bean名称
    * @return 实例
    */
    public static Object getBean(String name) {
    Assert.hasText(name);
    return applicationContext.getBean(name);
    }

    /**
    * 获取实例
    *
    * @param name
    * Bean名称
    * @param type
    * Bean类型
    * @return 实例
    */
    public static <T> T getBean(String name, Class<T> type) {
    Assert.hasText(name);
    Assert.notNull(type);
    return applicationContext.getBean(name, type);
    }

    /**
    * 获取国际化消息
    *
    * @param code
    * 代码
    * @param args
    * 参数
    * @return 国际化消息
    */
    public static String getMessage(String code, Object... args) {
    LocaleResolver localeResolver = getBean("localeResolver", LocaleResolver.class);
    Locale locale = localeResolver.resolveLocale(null);
    String result = applicationContext.getMessage(code, args, locale);
    return result;
    }

    }

    第三步,在Controller中如下进行到freemarker的跳转,同时显示国际化内容:

    public String test(ModelMap model){

      String country = SpringUtils.getMessage("country");   // -- country 是国际化文件中的内容

      model.put("guojia",country);

      return "i8";

    }

    则在i8.ftl 中 直接用 ${"guojia"} 就可以显示国际化内容了

    messages_zh_CN.properties, messages_en_US.properties 等国际化文件放在类路径下。也可以放在别的包内,但是要改一下设置。再查。

  • 相关阅读:
    element-ui 多张图上传
    js json生取key,value 值
    iview DatePicker 回显验证报错
    iview的Select控制value为数字类型时表单验证无法通过
    iview 自定义树形
    tree 树形递归修改 key
    根据月份选择 生成这个月的每一天
    微信小程序超出隐藏省略号和自动换行
    uni-app picker select 取想要的值
    element-ui 表格fixed 样式修改
  • 原文地址:https://www.cnblogs.com/appzhang/p/3889102.html
Copyright © 2011-2022 走看看