项目实现国际化
复制收展Javapackage com.com.test.globalization;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @Desc
* @Author luolei
* @Date 2023/01/05 16:54
*/
public class MessageUtil {
private String languageCode = "";
private Properties properties;
String filePath = "D:\\ideaworkspace\\ssh\\src\\main\\resources\\i18n\\aims\\";
public static void main(String[] args) {
MessageUtil messageUtil = new MessageUtil(LanguageCode.CHINESE);
String aims_s_x0001 = messageUtil.getTranslate("AIMS_S_B0001");
System.out.println(aims_s_x0001);
messageUtil = new MessageUtil(LanguageCode.CHINESE_TW);
aims_s_x0001 = messageUtil.getTranslate("AIMS_S_B0001");
System.out.println(aims_s_x0001);
messageUtil = new MessageUtil(LanguageCode.US);
aims_s_x0001 = messageUtil.getTranslate("AIMS_S_B0001");
System.out.println(aims_s_x0001);
}
public MessageUtil(String languageCode){
this.languageCode = languageCode;
readProperties();
}
public String getTranslate(String msgKey) {
try {
return new String(properties.getProperty(msgKey).getBytes("ISO8859-1"), "UTF-8");
} catch (Exception var3) {
return null;
}
}
private void readProperties() {
properties = new Properties();
FileInputStream fis = null;
try {
String fileName = "messages_"+languageCode+".properties";
fis = new FileInputStream(filePath+fileName);
properties.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
class LanguageCode {
public final static String CHINESE = "zh_CN";
public final static String CHINESE_TW = "zh_TW";
public final static String US = "en_US";
}
}
- 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
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
消息文件
messages.properties
# 默认值#
AIMS_S_B0001=变更成功
AIMS_S_B0002=保存成功
messages_zh_CN.properties
# 默认值#
AIMS_S_B0001=变更成功
AIMS_S_B0002=保存成功
#Default#
AIMS_S_B0001=change succeeded
AIMS_S_B0002=save successfully
messages_zh_TW.properties
# 默認值#
AIMS_S_B0001=變更成功
AIMS_S_B0002=保存成功