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 等国际化文件放在类路径下。也可以放在别的包内,但是要改一下设置。再查。

  • 相关阅读:
    QuantLib 金融计算——基本组件之 Date 类
    挑选合适的机器学习资料
    【翻译】理解 LSTM 及其图示
    《信任的速度》读后感
    Git分支使用心得
    c# 多线程 创建对象实例
    c# 设计模式之单例模式
    C# 设计模式之空对象模式
    c# 静态构造函数与构造函数的调用先后
    C# 中关于接口实现、显示实现接口以及继承
  • 原文地址:https://www.cnblogs.com/appzhang/p/3889102.html
Copyright © 2011-2022 走看看