溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

SpringBoot如何實(shí)現(xiàn)前后端分離國(guó)際化

發(fā)布時(shí)間:2023-02-22 11:44:38 來(lái)源:億速云 閱讀:180 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇“SpringBoot如何實(shí)現(xiàn)前后端分離國(guó)際化”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“SpringBoot如何實(shí)現(xiàn)前后端分離國(guó)際化”文章吧。

前言

Springboot國(guó)際化可以幫助使用者在不同語(yǔ)言環(huán)境中構(gòu)建應(yīng)用程序,這樣應(yīng)用程序可以有效地適應(yīng)不同語(yǔ)言文化背景下的用戶(hù)需求。

此外,Springboot國(guó)際化也可以方便多語(yǔ)言應(yīng)用程序重用和維護(hù),從而減少了系統(tǒng)部署的時(shí)間成本和維護(hù)的費(fèi)用。

要實(shí)現(xiàn)Springboot國(guó)際化應(yīng)用,主要有三個(gè)步驟。

1、設(shè)置國(guó)際化屬性文件

定義國(guó)際化資源文件,使用properties格式的文件,將不同的多國(guó)語(yǔ)言文本資源放在不同的文件中,每個(gè)文件的命名采用【locale】+【messages】的方式,如zh_CN.properties、en_US.properties等。

SpringBoot如何實(shí)現(xiàn)前后端分離國(guó)際化

message.properties文件內(nèi)容可為空。

message.en_US.properties內(nèi)容示例:

40001=Hello

message.zh_CN.properties內(nèi)容示例:

40001=你好

2、創(chuàng)建解析器和攔截器

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class LocaleConfig {

	@Bean
	public SessionLocaleResolver localeResolver() {
		SessionLocaleResolver localeResolver = new SessionLocaleResolver();
		localeResolver.setDefaultLocale(Locale.CHINA);
		return localeResolver;
	}
	
	@Bean
	public WebMvcConfigurer localeInterceptor() {
		return new WebMvcConfigurer() {
			@Override
			public void addInterceptors(InterceptorRegistry registry) {
				LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
				localeInterceptor.setParamName("lang");
				registry.addInterceptor(localeInterceptor);
			}
		};
	}
}

3、啟動(dòng)配置文件設(shè)置

application.properties中添加如下內(nèi)容

#i18n
spring.messages.basename=i18n.messages
spring.messages.cache-duration=3600
spring.messages.encoding=UTF-8

application.yml中添加如下內(nèi)容

spring: 
  messages:
    basename: i18n/messages

4、控制器示例

Springboot的國(guó)際化是通過(guò)一個(gè)稱(chēng)為“messageSource”的bean實(shí)現(xiàn)的。

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestControler {

	@Autowired
	private MessageSource messageSource;
	
	@GetMapping("/hello")
	public Map<Object, Object> test() {
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("code", 40001);
		result.put("msg", messageSource.getMessage("40001", null, LocaleContextHolder.getLocale()));
		return result;
	}

}

5、小結(jié)

Springboot國(guó)際化可以幫助使用者在不同語(yǔ)言環(huán)境中構(gòu)建應(yīng)用程序,這樣應(yīng)用程序可以有效地適應(yīng)不同語(yǔ)言文化背景下的用戶(hù)需求。

此外,Springboot國(guó)際化也可以方便多語(yǔ)言應(yīng)用程序重用和維護(hù),從而減少了系統(tǒng)部署的時(shí)間成本和維護(hù)的費(fèi)用。要實(shí)現(xiàn)Springboot國(guó)際化應(yīng)用,主要有三個(gè)步驟。

1.設(shè)置國(guó)際化屬性文件:要實(shí)現(xiàn)Springboot國(guó)際化,首先要準(zhǔn)備一系列國(guó)際化屬性文件,包括語(yǔ)言和地區(qū)信息。

2.注冊(cè)國(guó)際化消息資源:將屬性文件中的語(yǔ)言和地區(qū)信息注冊(cè)到Springboot應(yīng)用程序中,并指定默認(rèn)值,以便在擴(kuò)展多語(yǔ)言時(shí)可以快速、高效地訪問(wèn)相關(guān)屬性資源。

3.定義多語(yǔ)言捆綁文件:將已定義的國(guó)際化屬性文件與應(yīng)用程序結(jié)合起來(lái),形成多語(yǔ)言捆綁文件,以便在用戶(hù)選擇不同語(yǔ)言時(shí)可以及時(shí)調(diào)整應(yīng)用程序語(yǔ)言版本。

以上就是關(guān)于“SpringBoot如何實(shí)現(xiàn)前后端分離國(guó)際化”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI