Spring 国际化

组件

Locale

Locale对象代表具体的地理,政治或文化地区。设置语言区域如zh,CN,对语言敏感操作定制特定的信息。

public final class Locale implements Cloneable, Serializable {
	// 语言、国家
	public Locale(String language, String country) {
        this(language, country, "");
    }
    
	public static Locale getDefault() {
		// 默认语言、区域、国家,通过jvm变量user.language、user.region、user.country获取
		return defaultLocale;
	}
}

MessageSource

用于支持信息的国际化和包含参数的信息的替换

public interface MessageSource {
	/**
	*code表示资源文件中的属性
	*args表示替换参数
	*未找到资源的默认值
	*当前语言区域
	*/
	String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
	String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
}

工具类

NumberFormat

根据locale格式化数字

public abstract class NumberFormat extends Format  {
	// 数字类型
	private static final int NUMBERSTYLE = 0;
	// 货币类型
    private static final int CURRENCYSTYLE = 1;
	// 百分类型
    private static final int PERCENTSTYLE = 2;
	// 科学计数类型
    private static final int SCIENTIFICSTYLE = 3;
	// 整数类型
    private static final int INTEGERSTYLE = 4;
	
	// 构造方法,默认NUMBERSTYLE
	public static NumberFormat getInstance(Locale inLocale) {
		return getInstance(inLocale, NUMBERSTYLE);
	}
}	

DateFormat

根据locale格式化日期

public abstract class DateFormat extends Format {
	// 构造方法
	public final static DateFormat getDateInstance()
    {
        return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT));
    }
}        

MessageFormat

提供了占位符{ArgumentIndex ,FormatType,FormatStyle }来显示消息

public abstract class NumberFormat extends Format  {
	// 默认构造,传入模式
	public MessageFormat(String pattern) {
        this.locale = Locale.getDefault(Locale.Category.FORMAT);
        applyPattern(pattern);
    }
}	

ResourceBundle

资源束包含区域特定的对象。用来加载区域敏感资源

public void test() {
    Locale locale = Locale.getDefault();
    // 调用类加载器在classpath中搜索获得资源
    ResourceBundle bundle = ResourceBundle.getBundle("i18n/list", locale);
    System.out.println(bundle.getString("login.name"));
}

使用

配置文件

<!--id必须为messageSource-->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <!--文件编码-->
    <property name="fileEncodings" value="UTF-8"/>
    <!--资源路径-->
    <property name="basename" value="i18n/list"/>
    <!--找不到key则返回key,不抛异常-->
    <property name="useCodeAsDefaultMessage" value="true" />
    <!--动态加载资源文件-->
    <property name="cacheMillis" value="5000"/>
</bean>

获得资源

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:bean.xml");
    MessageSource source = context.getBean("messageSource", MessageSource.class);
    String message1 = source.getMessage("login.name", new String[]{"占位"}, Locale.CHINA);
    String message = source.getMessage("login.name", new String[]{"占位"}, Locale.US);
    System.out.println("中文:" + message1);
    System.out.println("英文:" + message);
}
  • 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、付费专栏及课程。

余额充值