Skip to content

Commit

Permalink
[common]support locale read from env LANG (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Aug 9, 2022
1 parent 4ee56f0 commit 167c3fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
30 changes: 27 additions & 3 deletions common/src/main/java/com/usthe/common/util/ResourceBundleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,40 @@
@Slf4j
public class ResourceBundleUtil {

private static final ResourceBundleUtf8Control BUNDLE_UTF_8_CONTROL = new ResourceBundleUtf8Control();
private static final Integer LANG_REGION_LENGTH = 2;

static {
// set default locale by env
try {
String langEnv = System.getenv("LANG");
if (langEnv != null) {
String[] langArr = langEnv.split("\\.");
if (langArr.length >= 1) {
String[] regionArr = langArr[0].split("_");
if (regionArr.length == LANG_REGION_LENGTH) {
String language = regionArr[0];
String region = regionArr[1];
Locale locale = new Locale(language, region);
Locale.setDefault(locale);
}
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}

/**
* 根据bundle name 获取 resource bundle
* get resource bundle by bundle name
* @param bundleName bundle name
* @return resource bundle
*/
public static ResourceBundle getBundle(String bundleName) {
try {
return ResourceBundle.getBundle(bundleName, new ResourceBundleUtf8Control());
return ResourceBundle.getBundle(bundleName, BUNDLE_UTF_8_CONTROL);
} catch (MissingResourceException resourceException) {
return ResourceBundle.getBundle(bundleName, Locale.US, new ResourceBundleUtf8Control());
return ResourceBundle.getBundle(bundleName, Locale.US, BUNDLE_UTF_8_CONTROL);
}
}

Expand Down
5 changes: 3 additions & 2 deletions script/docker/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM openjdk:11.0.15-jre-slim-buster
FROM openjdk:11.0.16-jre-slim-buster

MAINTAINER tomsun28 "tomsun28@outlook.com"
MAINTAINER tancloud "tomsun28@outlook.com"

ADD hertzbeat-1.1.1.tar /opt/

ENV TZ=Asia/Shanghai
#ENV LANG=zh_CN.UTF-8

EXPOSE 1157

Expand Down

0 comments on commit 167c3fa

Please sign in to comment.