一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot消息國際化配置實現過程解析

SpringBoot消息國際化配置實現過程解析

2020-07-31 14:59與李 Java教程

這篇文章主要介紹了SpringBoot消息國際化配置實現過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、目的

針對不同地區,設置不同的語言信息。

SpringBoot國際化配置文件默認放在classpath:message.properties,如果自定義消息配置文件,需要application.properties或application.yml中設置spring.messages.basename的值。

二、步驟

在src/main/resources 下建i18n文件夾

在i18n文件夾中建立messages.properties 找不到語言配置時,使用此文件

hello=你好_默認

在i18n文件夾中建立messages_en_US.properties 英文語言配置

hello=hello_English

在i18n文件夾中建立messages_zh_CN.properties 中文語言配置

hello=你好_中文

MessageConfig.java

對消息的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.spring.security.config.spring;
 
import java.util.Locale;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.util.Assert;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AbstractLocaleContextResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
 
@Configuration
public class MessageConfig extends AbstractLocaleContextResolver{
 
    @Value("${spring.messages.basename}")
    public String[] basenames;
 
    @Bean(name = "messageSource")
    public ResourceBundleMessageSource resourceBundleMessageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        if (basenames != null) {
            for (int i = 0; i < basenames.length; i++) {
                String basename = basenames[i];
                Assert.hasText(basename, "Basename must not be empty");
                this.basenames[i] = basename.trim();
            }
            source.setBasenames(basenames);
        } else {
            this.basenames = new String[0];
            source.setBasename(basenames[0]);
        }
        source.setDefaultEncoding("UTF-8");
        source.setUseCodeAsDefaultMessage(true);
        return source;
    }
 
  @Bean
  public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
  }
 
  /**
   * 國際化,設置url識別參數
   *
   * @return
   */
  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
  }
 
    @Override
    public LocaleContext resolveLocaleContext(HttpServletRequest request) {
        return null;
    }
 
    @Override
    public void setLocaleContext(HttpServletRequest request, HttpServletResponse response,
            LocaleContext localeContext) {
    }
}

SpringUtils.java

Spring工具類,用于獲取ApplicationContext

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.spring.security.common.utils;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
 
/**
 * Spring容器
 */
@Service
public class SpringUtils implements ApplicationContextAware {
 
  private static ApplicationContext context = null;
 
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (context == null) {
      context = applicationContext;
    }
  }
 
  /**
   * 獲取容器
   *
   * @return 容器
   */
  public static ApplicationContext getContext() {
    return context;
  }
}

MessageUtils.java

封裝獲取message的工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.spring.security.common.utils;
 
import java.util.Locale;
 
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
 
public class MessageUtils {
 
    public static String getMessage(String code) {
         Locale locale = LocaleContextHolder.getLocale();
         ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
       String message = reloadableResourceBundleMessageSource.getMessage(code, null, locale);
       return message;
    }
}

** WebMvcConfig.java**

mvc配置,解決跨域,接口中文亂碼,添加語言攔截器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.spring.security.config.spring;
 
import java.nio.charset.Charset;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
 
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
 
    @Autowired
    private LocaleChangeInterceptor localeChangeInterceptor;
 
    /**
     * 解決跨域
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry
        .addMapping("/**")
        .allowedHeaders("*")
        .allowedMethods("*")
        .allowedOrigins("*")
        .allowCredentials(true);
    }
 
    /**
     * 配置消息轉換器
     * 解決返回String亂碼
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }
 
    @Bean
  public HttpMessageConverter<String> responseBodyConverter() {
    return new StringHttpMessageConverter(Charset.forName("UTF-8"));
  }
 
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(localeChangeInterceptor);
    }
 
}

三、測試

測試接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.spring.security.controller;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.spring.security.common.utils.I18nUtils;
 
@RestController
public class TestController {
 
    @GetMapping("/test")
    public String doTest() {
        return I18nUtils.getMessage("hello");
    }
}

SpringBoot消息國際化配置實現過程解析

SpringBoot消息國際化配置實現過程解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/yl-space/p/13383994.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 18free性欧美另类hd | 四虎影视永久在线观看 | 亚洲国产精久久久久久久 | 精品国产91久久久久 | 手机看片自拍自自拍日韩免费 | 日本亚欧乱色视频在线观看 | 丝袜爆操| 午夜小视频网站 | 日韩天堂在线 | 狗老公小说 | 91久久综合九色综合欧美98 | 精品午夜久久福利大片免费 | 激情小说色图 | 隔壁老王国产在线精品 | 波多野结衣黑人系列在线观看 | 黑人巨荃大战乌克兰美女 | 好逼365 | 免看一级a一片成人123 | 国产午夜免费秋霞影院 | 奇米影视在线视频 | 国产精品久久久久影院色老大 | 欧美洲大黑香蕉在线视频 | 五月天视频网 | 免看一级a一片成人123 | 动漫美女隐私尿口图片 | 99毛片| 五月天婷婷精品免费视频 | 国产精品嫩草影院在线看 | 亚洲乱码一区二区三区国产精品 | 奇米影视在线视频8888 | 国产成+人+综合+欧美 亚洲 | 日本68xxxxxxxxx59 日本 视频 在线 | 视频大全在线观看网址 | 欧美一级乱妇老太婆特黄 | 91精品大神国产在线播放 | 性夜影院爽黄A爽免费动漫 性色欲情网站IWWW九文堂 | 啊好大好粗| 水蜜桃一二二区视在线 | 色老板在线视频 | 天天摸天天操天天爽 | 天天做天天爱天天爽综合区 |