Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[common]support locale read from env LANG #231

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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