隨著全球化的加速發(fā)展,Web應用的多語言支持變得越來越重要。對于開發(fā)者來說,如何實現(xiàn)應用的國際化成為了一個不可忽視的問題。作為Java Web開發(fā)的重要框架,Spring MVC在處理國際化方面有著豐富的功能和靈活的解決方案。本文將探討Spring MVC的國際化部分內(nèi)容,并通過自己的在工作中的真實使用體驗來幫助大家理解和應用這些功能。
1、添加相關(guān)依賴
如果是Maven項目中,可以在pom.xml
文件中添加以下依賴:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.10</version>
</dependency>
2、配置MessageSourceBean
在Spring的配置文件applicationContext.xml
中,可以通過以下兩種方式進行注入并加載資源文件。
方式一:ReloadableResourceBundleMessageSource
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages/messages"/> <!-- 消息資源文件的基本名稱 -->
<property name="defaultEncoding" value="UTF-8"/> <!-- 默認編碼 -->
</bean>
方式二:ResourceBundleMessageSource
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages/messages"/> <!-- 消息資源文件的基本名稱 -->
<property name="defaultEncoding" value="UTF-8"/> <!-- 默認編碼 -->
</bean>
另外還可以通過代碼的方式進行bean的引入
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages/messages"); // 消息資源文件的位置
messageSource.setDefaultEncoding("UTF-8"); // 設置默認編碼
return messageSource;
}
3、添加消息資源文件
在src/main/resources
的位置下創(chuàng)建消息資源文件夾messages
,用于存儲不同語言的消息。
例如,在messages
文件夾下新建messages_en.properties
的英文消息資源文件和messages_zh_CN.properties
的中文消息資源文件。在文件中定義消息鍵和對應的值。 注意:存放位置可根據(jù)自己項目規(guī)范進行修改
英文 messages_en.properties
001=hello word!
中文 messages_zh_CN.properties
001=你好,世界!
默認 messages.properties
001=你好,世界!
4、消息工具類(代碼應用)
創(chuàng)建MessageUtil.java
工具類文章來源:http://www.zghlxwxcb.cn/news/detail-799471.html
package com.cloud.core.util;
import java.util.Locale;
import org.springframework.context.MessageSource;
import com.cloud.core.holder.ContextHolder;
public class MessageUtil {
private static MessageSource messageSource;
//ContextHolder為Spring容器上下文信息類,可通過繼承ContextLoaderListener進行獲取
static {
messageSource = (MessageSource) ContextHolder.getBean("messageSource");
}
/**
* @Comments :獲取消息
* @param code 在*.properties文件中的消息鍵
* @param locale 本地化語言,也可根據(jù)需要進行擴展
* @return
* @Author :程序員云筆記
* @Date :2024年1月5日 上午11:00:29
*/
public static String getMessage(String code, Locale locale) {
return messageSource.getMessage(code, null, locale);
}
/**
* @Comments :獲取消息
* @param code 在*.properties文件中的消息鍵
* @param args 參數(shù)數(shù)組,其中的參數(shù)將被填充,用于替換消息中的{0}、{1},詳見java.text.MessageFormat
* @param locale 本地化語言,也可根據(jù)需要進行擴展
* @return
* @Author :程序員云筆記
* @Date :2024年1月5日 上午11:01:30
*/
public static String getMessage(String code, Object[] args, Locale locale) {
return messageSource.getMessage(code, args, locale);
}
}
//以下是此工具類用到的部分代碼
public class WebAppContextListener extends ContextLoaderListener {
public void contextDestroyed(ServletContextEvent event) {
}
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
ContextHolder.setContext(context);
}
}
public class ContextHolder {
private static ApplicationContext context;
public static void setContext(ApplicationContext ctx) {
if(context == null){
context = ctx;
}
}
public static Object getBean(String name) {
return context.getBean(name);
}
}
5、在代碼中直接使用
MessageUtil.getMessage("001",Locale.ENGLISH);
MessageUtil.getMessage("001",Locale.CHINA);
MessageUtil.getMessage("001",Locale.getDefault());
//輸出以下消息
hello word!
你好,世界!
你好,世界!
以上就是本次項目國際化所有代碼,有不足之處歡迎大家指導。文章來源地址http://www.zghlxwxcb.cn/news/detail-799471.html
到了這里,關(guān)于Spring MVC(三) 國際化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!