-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(I18n): add internationalization to the program
* add internationalization class I18n
- Loading branch information
1 parent
6fa8de0
commit 823f8a2
Showing
8 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,51 @@ | ||
package cn.harryh.arkpets.i18n; | ||
|
||
|
||
import cn.harryh.arkpets.ArkConfig; | ||
import cn.harryh.arkpets.utils.Logger; | ||
|
||
import java.util.*; | ||
|
||
|
||
public final class I18n { | ||
|
||
private I18n() { | ||
} | ||
|
||
public static Locales.SupportedLocale getCurrentLocale() { | ||
try { | ||
return Locales.getLocaleByName(Objects.requireNonNull(ArkConfig.getConfig()).prefer_language); | ||
} catch (IllegalStateException e) { | ||
return Locales.DEFAULT; | ||
} | ||
} | ||
|
||
public static ResourceBundle getResourceBundle() { | ||
return getCurrentLocale().getResourceBundle(); | ||
} | ||
|
||
public static String i18n(String key, Object... formatArgs) { | ||
try { | ||
return String.format(getResourceBundle().getString(key), formatArgs); | ||
} catch (MissingResourceException e) { | ||
Logger.error("I18n", "Cannot find key " + key + " in resource bundle" + e); | ||
} catch (IllegalFormatException e) { | ||
Logger.error("I18n", "Illegal format string, key=" + key + ", args=" + Arrays.toString(formatArgs) + e); | ||
} | ||
|
||
return key + Arrays.toString(formatArgs); | ||
} | ||
|
||
public static String i18n(String key) { | ||
try { | ||
return getResourceBundle().getString(key); | ||
} catch (MissingResourceException e) { | ||
Logger.error("I18n", "Cannot find key " + key + " in resource bundle", e); | ||
return key; | ||
} | ||
} | ||
|
||
public static boolean hasKey(String key) { | ||
return getResourceBundle().containsKey(key); | ||
} | ||
} |
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,77 @@ | ||
package cn.harryh.arkpets.i18n; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.ResourceBundle; | ||
|
||
|
||
public final class Locales { | ||
public static final SupportedLocale DEFAULT = new SupportedLocale(Locale.getDefault(), "lang.default"); | ||
/** | ||
* English | ||
*/ | ||
public static final SupportedLocale EN = new SupportedLocale(Locale.ROOT); | ||
/** | ||
* Traditional Chinese | ||
*/ | ||
public static final SupportedLocale ZH_TW = new SupportedLocale(Locale.TRADITIONAL_CHINESE); | ||
/** | ||
* Simplified Chinese | ||
*/ | ||
public static final SupportedLocale ZH_CN = new SupportedLocale(Locale.SIMPLIFIED_CHINESE); | ||
|
||
private Locales() { | ||
} | ||
|
||
public static SupportedLocale getLocaleByName(String name) { | ||
if (name == null) return DEFAULT; | ||
return switch (name.toLowerCase(Locale.ROOT)) { | ||
case "en" -> EN; | ||
case "zh_tw" -> ZH_TW; | ||
case "zh_cn" -> ZH_CN; | ||
default -> DEFAULT; | ||
}; | ||
} | ||
|
||
public static List<String> getSupportedLanguages() { | ||
return List.of("en", "zh_tw", "zh_cn"); | ||
} | ||
|
||
public static String getNameByLocale(SupportedLocale locale) { | ||
if (locale == EN) return "en"; | ||
if (locale == ZH_TW) return "zh_tw"; | ||
if (locale == ZH_CN) return "zh_cn"; | ||
if (locale == DEFAULT) return "def"; | ||
throw new IllegalArgumentException("Unknown locale: " + locale); | ||
} | ||
|
||
public static class SupportedLocale { | ||
private final Locale locale; | ||
private final String name; | ||
private final ResourceBundle resourceBundle; | ||
|
||
SupportedLocale(Locale locale) { | ||
this(locale, null); | ||
} | ||
|
||
SupportedLocale(Locale locale, String name) { | ||
this.locale = locale; | ||
this.name = name; | ||
resourceBundle = ResourceBundle.getBundle("lang.I18N", locale); | ||
} | ||
|
||
public Locale getLocale() { | ||
return locale; | ||
} | ||
|
||
public ResourceBundle getResourceBundle() { | ||
return resourceBundle; | ||
} | ||
|
||
public String getName(ResourceBundle newResourceBundle) { | ||
if (name == null) return resourceBundle.getString("lang"); | ||
else return newResourceBundle.getString(name); | ||
} | ||
|
||
} | ||
} |
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 @@ | ||
app.title = Ark pet |
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 @@ | ||
app.title = Ark Pet |
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 @@ | ||
app.title = Ark pet |
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