使用spring實現資源國際化
有時候需要使用資源國際化來定義和處理一些欄位。本文是一個簡單的使用例子用於展示在基於java註解的配置中配置spring資源國際化的類並使用其獲取prop檔案中的屬性。
1、建立資原始檔
本文示例使用的資原始檔為一個屬性檔案,預設的屬性檔名稱為i18n.properties,存放在工程的類路徑下的i18n目錄下。實際使用中可以針對需要新增針對不同語音的屬性檔案,如:
i18n_en_US.properties、i18n_zh_CN.properties等。本例中的預設屬性配置檔案中的內容如下:
munu.btn1.title=\u5B89\u9632
menu.btn2.title=\u670D\u52A1
menu.btn3.title=\u6211\u7684
munu.btn3.sub1.title=\u4E3B\u9875
2、配置bean
本文使用給予java註解的配置。主要時建立一個org.springframework.context.support.ResourceBundleMessageSource的bean例項。原始碼如下:
package api.landsem.base.configuration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan({"api.landsem.base.bean"})
@ImportResource("classpath:applicationContext-*.xml")
public class RootConfiguration {
@Bean("messageSource")
public MessageSource getResourceBundleMessageSourc() {
ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
resource.setBasename("i18n/i18n");
return resource;
}
}
3、讀取資源屬性檔案
在java中使用建立的MessageSource bean來讀取屬性檔案,如下為測試原始碼:
package api.landsem.base.test;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
public class PropertiesTest extends BaseTest{
private static final Logger logger = Logger
.getLogger(PropertiesTest.class);
@Autowired
private MessageSource mMessageSource;
@Test
public void test() {
String msg = mMessageSource.getMessage("munu.btn1.title", null, Locale.getDefault());
logger.info(msg);
}
}
相關文章
- 在Spring Boot實現國際化的案例Spring Boot
- 使用vue-i18n實現國際化Vue
- Spring Webflux國際化SpringWebUX
- 【SpringBoot學習(四) 使用 thymeleaf實現國際化功能】Spring Boot
- Spring-boot國際化Springboot
- 小程式國際化實現方式
- vue中如何使用i18n實現國際化Vue
- 【spring 國際化】springMVC、springboot國際化處理詳解SpringMVCSpring Boot
- 使用Spring Boot實現模組化Spring Boot
- Java模組化的國際化實現- GunnarJava
- 從原始碼MessageSource的三個實現出發實戰spring·i18n國際化原始碼Spring
- 使用jquery.i18n實現國際化多語言顯示jQuery
- Android國際化(多語言)實現,支援8.0Android
- 人力資源實現智慧化的原因
- 使用Spring Cloud Data Flow + CDC Debezium源實時實現變更資料捕獲 - SpringIOSpringCloud
- Struts2【UI標籤、資料回顯、資源國際化】UI
- Webnovel 國際化實踐Web
- 使用Spring Data JPA實現持久化層的簡化開發Spring持久化
- 初識ABP vNext(6):vue+ABP實現國際化Vue
- Spring Boot應用的國際化與本地化支援Spring Boot
- 使用Spring Boot實現微服務架構的開源專案Spring Boot微服務架構
- 使用 Performance API 實現前端資源監控ORMAPI前端
- 使用Spring Boot實現資料庫整合配置案例Spring Boot資料庫
- 用Rust 實現的現代化實時開源資料倉儲Rust
- 基於react-intl實現手動國際化切換React
- Flutter 國際化適配實戰Flutter
- 實用的 Flutter 國際化指南Flutter
- 談談Spring Boot 資料來源載入及其多資料來源簡單實現Spring Boot
- 龍源國際電話@⒈⒎〇⒏⒋⒉⒉⒉⒉⒈⒈@龍源國際@我是大神仙
- 國際化
- 黃東旭提出:“最好的國產化是國際化。” 而開源社群
- springboot、Thymeleaf、國際化的簡單使用Spring Boot
- 龍源國際電話@⒈⒎〇〇⒏⒎⒍⒏〇〇〇@龍源國際電話@我要一千萬
- @龍源國際電話@⒈⒎〇⒏⒋⒉⒉⒉⒉⒈⒈@龍源國際@我要發大財@
- @龍源國際電話@⒈⒎〇⒏⒋⒉⒉⒉⒉⒈⒈@龍源國際微信@我要一千萬@
- Spring Boot + Mybatis 多資料來源配置實現讀寫分離Spring BootMyBatis
- Spring Boot 2.x基礎教程:使用JTA實現多資料來源的事務管理Spring Boot
- Flutter實戰之主題、國際化篇Flutter
- 使用Echarts來實現資料視覺化Echarts視覺化