SpringBoot国际化i18n-freemark

Springboot支持国际化,可以通过编写message_语言包_.properties来使用官网进行数据的文字切换,需要注意的是不同的模板系统获取语言数据字段的方式也不同。

在resources 目录下新建多语言文件,格式如下 messages_语言_地区.propertiesmessages为默认前缀 默认:messages.properties 、英文:messages_en.properties 、中文:messages_zh_CN.properties

brand=品牌客户
brand=Brand Clients

增加i18n的配置

spring:
  session:
    store-type: none
  freemarker:
    template-loader-path: classpath:/templates/
    cache: false
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
      # 暴露spring提供的freemarker宏
    expose-spring-macro-helpers: true
    request-context-attribute: request
    suffix: .ftl
    settings:
      datetime_format: yyyy-MM-dd HH:mm:ss
  messages:
    basename: i18n/message
    encoding: UTF-8

在截拦器进行配置。

package com.cross1000.index.config;

import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.List;
import java.util.Locale;

@Configuration
public class MobileWebMvcConfigurer implements WebMvcConfigurer {
    @Bean
    public DeviceResolverHandlerInterceptor
    deviceResolverHandlerInterceptor() {
        return new DeviceResolverHandlerInterceptor();
    }

    @Bean
    public DeviceHandlerMethodArgumentResolver
    deviceHandlerMethodArgumentResolver() {
        return new DeviceHandlerMethodArgumentResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(deviceResolverHandlerInterceptor());
        registry.addInterceptor(localeChangeInterceptor());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(deviceHandlerMethodArgumentResolver());
    }

    //    多语言配置
    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver slr = new CookieLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.CHINA);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("_lang");
        return lci;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

在freemark中需要引入模板,如下代码所示。

<#import "/spring.ftl" as spring>
使用数据
 <@spring.message code='brand'/>

前端切换代码

    //    切换语言
    $(".changeLang").click(function () {
        console.log($(this).text())
        let url = window.location.href;
        switch ($(this).text()) {
            case "中文 ": {
                window.location.href = "/?_lang=zh_CN"
                break;
            }
            case " English": {
                window.location.href = "/?_lang=en_US";
                break;
            }
        }
    });

链接