Java Spring国际化配置 实现多语言切换

Spring国际化配置通过使用MessageSourceLocaleResolverLocaleChangeInterceptor等组件来实现。以下是一个详细的示例,展示如何配置Spring项目以支持国际化:

1. 配置MessageSource

        首先,配置一个MessageSource bean,用于加载国际化资源文件。这些资源文件通常位于src/main/resources目录下。

@Configuration
public  class LocaleConf {

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource source  = new ReloadableResourceBundleMessageSource();
        //classpath的目录下指的是src/main/resources,可设置多个配置文件
        source.setBasenames("classpath:i18n/messages",
                "classpath:i18n/financeManage",
                "classpath:i18n/menuFields",
                "classpath:i18n/systemManage"
                );
        source.setDefaultEncoding("UTF-8");
        return source;
    }
}

2. 配置LocaleResolver

     LocaleResolver用于解析和存储用户的语言环境。可以使用SessionLocaleResolverCookieLocaleResolver。resolver.setDefaultLocale(Locale.US);设置默认语言。

@Configuration
public  class LocaleConf {

	@Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver slr = new CookieLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.US);
        slr.setCookieName("sis_lang");
        return slr;
    }

}

3. 配置LocaleChangeInterceptor

     LocaleChangeInterceptor用于拦截请求并根据请求参数更改语言环境。

Java配置:

@Configuration
public  class LocaleConf {
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
                localeInterceptor.setParamName("lang");
                registry.addInterceptor(localeInterceptor);
            }
        };
    }
}

xml配置

<!-- spring-mvc.xml -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:i18n/messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

4. 配置国际化资源文件

          在src/main/resources目录下创建国际化资源文件,例如messages_en_US.propertiesmessages_zh_CN.properties

messages_en_US.properties:

common.greeting=Hello
common.farewell=Goodbye

messages_zh_CN.properties:

common.greeting=你好
common.farewell=再见

5. 使用国际化资源文件

          创建MessageUtill类,在代码中通过MessageSource来获取国际化消息。调用MessageUtils中的getMessage函数根据当前语言环境获取对应的国际化资源消息。

public class MessageUtils {

    private static MessageSource messageSource;

    public MessageUtils(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public static String getMessage(String msgKey, Object[] args) {
        return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale());
    }

    public static String getMessage(String msgKey) {
        return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
    }
}

6.前端中使用国际化

使用 FreeMarker 模板引擎与 Spring Boot 集成来实现国际化功能,可以通过 <#import "/spring.ftl" as spring> 标签导入 Spring FreeMarker 标签库。以下是如何在 FreeMarker 模板中使用国际化消息。

<#import "/spring.ftl" as spring>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>例子</title>
</head>
<body>
    <h1>
        <@spring.message "common.greeting" />
    </h1>
    
    <button onclick="changeLanguage('en_US')">English</button>
    <button onclick="changeLanguage('zh_CN')">中文</button>

    <script>
        function changeLanguage(lang) {
            window.location.href = "http://localhost:8080/?"lang="+lang;
        }
    </script>
</body>
</html>

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值