发布于 

i118n国际化

前言

无需添加额外依赖,最多为节省代码量使用了lombok插件,在pom.xml引入

1
2
3
4
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

新增代码

在工具包里增加以下类

LocaleConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.touchsmail.common.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import java.util.Locale;

@Configuration
public class LocaleConfig {

@Bean
public ResourceBundleMessageSource messageSource() {
// Locale.setDefault(Locale.CHINA);
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
//设置国际化文件存储路径和名称 i18n目录,messages文件名
source.setBasenames("i18n/messages");
//设置根据key如果没有获取到对应的文本信息,则返回key作为信息
source.setUseCodeAsDefaultMessage(true);
//设置字符编码
source.setDefaultEncoding("UTF-8");
return source;
}

@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINA); // 设置默认的Locale,英文Locale.US
return localeResolver;
}

}

I18nUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.touchsmail.common.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class I18nUtils {

/**
* 使用懒加载方式实例化messageSource国际化工具
*/
private static class Lazy {
private static final MessageSource messageSource = SpringUtil2.getBean(MessageSource.class);
}

private static MessageSource getInstance() {
return Lazy.messageSource;
}

/**
* 获取国际化消息
*
* @param code 消息键
* @return 国际化消息
*/
public static String getMessage(String code) {
Locale locale = LocaleContextHolder.getLocale();
return getInstance().getMessage(code, null, locale);
}

/**
* 获取带参数的国际化消息
*
* @param code 消息键
* @param args 参数
* @return 国际化消息
*/
public static String getMessage(String code, Object... args) {
Locale locale = LocaleContextHolder.getLocale();
return getInstance().getMessage(code, args, locale);
}
}


SpringUtil2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.touchsmail.common.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SpringUtil2 implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;

@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
if (SpringUtil2.applicationContext == null) {
SpringUtil2.applicationContext = applicationContext;
}
}

/**
* 通过class获取Bean.
*/
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}

语言包

在resources目录下新建i118n目录,右键i118n目录->新建->资源包(Resource Bundle)添加国际化文件
右边加号添加2个,en_US存放英语,zh_CH存放中文

另外记得把idea的文件编码格式改成utf8,要不然中文可能会乱码,idea-设置-编辑-文件编码,属性文件默认编码,全局编码,项目编码都设置为UTF-8

1
2
3
4
5
resources
-i118n
-messages.properties
-messages_en_US.properties
-messages_zh_CN.properties

messages_zh_CN.properties

1
2
3
4
5
return.ok=操作成功!
return.failed=操作失败!
return.loginOk=登录成功!
return.pwd_error=密码错误!
return.account_lock=账号已被锁定{0}分钟,请稍后重试!

messages_en_US.properties

1
2
3
4
5
return.ok=Success!
return.failed=Fail!
return.loginOk=Login succeeded!
return.pwd_error=PW error!
return.account_lock=The account has been locked for {0} minutes. Please try again later!

调用

1
I18nUtils.getMessage()

前端传值

1
2
3
4
5
6
7
8
9
curl --location --request POST 'http://localhost:6001/manager/user/login' \
--header 'Authorization: Bearer plZw9T2kbOgTJIBqnZielUUUwkhBm0zSg9AIJ4JH8VBFleCKaD0hvNIUwZ9iYjsJTrI882MvvvP9Qbn5Ea6bgpLKbaEKefCcnQcnxFdDCMa4jCq7kBjunJ48SDW7HVED' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Accept-Language: zh-CN' \
--header 'Host: localhost:6001' \
--header 'Connection: keep-alive' \
--data-raw '{}'
1
// 即header 中添加 'Accept-Language: zh-CN'