spring国际化

1 篇文章 0 订阅

spring的国际化:

ApplicationContext接口因为继承了MessageSource接口,因此具备国际化的功能。

spring的国际化是建立在java程序国际化的基础上,都是将程序中需要国际化的信息写入资源文件,而代码中仅仅使用各资源文件的key。


1、bean.xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  	<property name="basenames">
  		<list>
  			<value>message</value>
  		</list>
  	</property>
  </bean>

2、资源文件message.properties
hello=欢迎你,{0}
和message_en_US.properties
hello=welcome,{0}

3、该资源文件包含了非西欧文字,所有使用native2ascii使该资源文件国际化,命令如下(载cmd中切换到资源文件所在的目录下):
native2ascii message.properties message_en_US.properties
运行该命令后,会生成message_zh_CN.properties,该文件将作为简体中文实际的国际化资源文件,此时,可自适应简体中文和英文的语言环境

4、在main方法中测试
public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext("config/bean_*.xml");
		String[] a = {"楠楠"};
		String bendi = ac.getMessage("hello", a, Locale.getDefault());
		System.out.println(bendi);
		
	}

5、运行结果
欢迎你,楠楠


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring的i18n国际化功能可以通过配置properties资源文件来实现。通常,我们会在src/main/resources目录下创建一个i18n文件夹,并在其中创建各类语言的properties资源文件,例如i18n/messages.properties、i18n/messages_en_US.properties、i18n/messages_zh_CN.properties等。\[1\] 在配置文件中,我们可以使用yaml格式进行配置。例如,可以使用spring.messages.basename属性来指定properties文件的路径,如spring.messages.basename: i18n.login。这样,Spring就会根据配置的路径去读取相应的国际化资源文件。\[2\] 另外,如果你在前端使用jQuery,你也可以使用jQuery.i18n.properties插件来实现国际化。该插件可以根据用户指定的语言和国家编码来解析对应的.properties资源文件,从而实现前端的国际化功能。\[3\] #### 引用[.reference_title] - *1* [spring中i18n国际化处理多语言](https://blog.csdn.net/shenyunsese/article/details/128326378)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot -> 国际化(i18n)](https://blog.csdn.net/rod0320/article/details/110086280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spring国际化i18n](https://blog.csdn.net/daobuxinzi/article/details/127982064)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值