-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangxiaoyan
committed
Jun 11, 2024
1 parent
135c2e3
commit e3688e2
Showing
5 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
landlords-common/src/main/java/org/nico/ratel/landlords/helper/I18nHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.nico.ratel.landlords.helper; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Locale; | ||
import java.util.MissingResourceException; | ||
import java.util.ResourceBundle; | ||
|
||
|
||
public class I18nHelper { | ||
|
||
private I18nHelper() { | ||
} | ||
|
||
private static final String I18N_BUNDLE_NAME = "messages"; | ||
|
||
private static ResourceBundle messageBundle; | ||
|
||
private static volatile boolean enabled = false; | ||
|
||
/** | ||
* 启用并加载国际化信息,语言地区使用系统默认 | ||
*/ | ||
public static void enable() { | ||
enable(Locale.getDefault()); | ||
} | ||
|
||
/** | ||
* 启用并加载国际化,指定语言地区 | ||
* | ||
* @param locale 语言地区 | ||
*/ | ||
public static void enable(Locale locale) { | ||
if (enabled) { | ||
System.err.println("[warning] i18n resource has already loaded."); | ||
return; | ||
} | ||
try { | ||
messageBundle = ResourceBundle.getBundle(I18N_BUNDLE_NAME, locale); | ||
if (locale != messageBundle.getLocale()) { | ||
System.err.printf("[warning] missing i18n resource for %s, set locale to %s\n", | ||
locale, messageBundle.getLocale()); | ||
} | ||
} catch (Exception e) { | ||
System.err.println("[error] load i18n resource error, exception: " + e.getMessage()); | ||
throw e; | ||
} | ||
I18nHelper.enabled = true; | ||
} | ||
|
||
/** | ||
* 更新并重新加载国际化资源 | ||
* | ||
* @param locale 语言地区 | ||
*/ | ||
public static void refresh(Locale locale) { | ||
if (!enabled) { | ||
throw new IllegalStateException("i18n not enabled!"); | ||
} | ||
ResourceBundle bundle; | ||
try { | ||
bundle = ResourceBundle.getBundle(I18N_BUNDLE_NAME, locale); | ||
if (locale != bundle.getLocale()) { | ||
throw new IllegalStateException("i18n resource of " + locale + " not found"); | ||
} | ||
} catch (Exception e) { | ||
if (e instanceof IllegalArgumentException) { | ||
throw e; | ||
} | ||
throw new RuntimeException("load i18n resource of " + locale + "failed, exception: " + e.getMessage()); | ||
} | ||
messageBundle = bundle; | ||
} | ||
|
||
/** | ||
* 翻译文本 | ||
* <p>支持格式化,格式化方式采用String.format(format, Object...args)<p/> | ||
* <p>不在字典内的key将原样显示</p> | ||
* | ||
* @param key 待翻译语言key | ||
* @param args 参数 | ||
*/ | ||
public static String translate(String key, Object... args) { | ||
if (!enabled) { | ||
return format(key, args); | ||
} | ||
try { | ||
String t = messageBundle.getString(key); | ||
return format(new String(t.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8), args); | ||
} catch (MissingResourceException e) { | ||
return format(key, args); | ||
} | ||
} | ||
|
||
/** | ||
* 获取当前地区语言 | ||
*/ | ||
public static Locale getCurrentLocale() { | ||
return messageBundle == null ? null : messageBundle.getLocale(); | ||
} | ||
|
||
private static String format(String template, Object... args) { | ||
return String.format(template, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
landlords-common/src/main/resources/messages_en_US.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pls_select_srv=Please select a server: | ||
srv_addr_not_exist=The server address does not exist! | ||
try_connect_%s_failed_%s=Try connected %s failed: %s |
3 changes: 3 additions & 0 deletions
3
landlords-common/src/main/resources/messages_zh_CN.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pls_select_srv=请选择一个服务器: | ||
srv_addr_not_exist=服务器地址不存在! | ||
try_connect_%s_failed_%s=尝试连接服务器 %s 失败,原因: %s |