Skip to content

Commit

Permalink
Auto detect user language
Browse files Browse the repository at this point in the history
  • Loading branch information
jyhsu2000 committed Jul 23, 2019
1 parent 3fa8aba commit ad60683
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
5 changes: 2 additions & 3 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 60 additions & 20 deletions src/main/java/club/kid7/bannermaker/configuration/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import club.kid7.bannermaker.BannerMaker;
import club.kid7.bannermaker.util.MessageUtil;
import club.kid7.pluginutilities.configuration.KConfigManager;
import org.apache.commons.lang.LocaleUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -11,13 +12,16 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Language {
private static Language instance = null;
private final BannerMaker bm;
private FileConfiguration defaultLanguageConfigResource;
private FileConfiguration languageConfigResource;
private String language = "en";
private Locale locale = Locale.ENGLISH;

public Language(BannerMaker bm) {
this.bm = bm;
Expand All @@ -28,25 +32,28 @@ public void loadLanguage() {
//從設定檔取得語言
String configFileName = "config.yml";
FileConfiguration config = KConfigManager.get(configFileName);
String defaultLanguage = "en";
language = defaultLanguage;
Locale defaultLocale = Locale.ENGLISH;
String language = "auto";
if (config != null && config.contains("Language")) {
language = (String) config.get("Language");
}
//轉換語言名稱
locale = parseLocale(language);

//載入預設語言包(但不儲存於資料夾)
try {
Reader defaultLanguageInputStreamReader = new InputStreamReader(bm.getResource(getFileName(defaultLanguage).replace('\\', '/')), StandardCharsets.UTF_8);
Reader defaultLanguageInputStreamReader = new InputStreamReader(bm.getResource(getFileName(defaultLocale).replace('\\', '/')), StandardCharsets.UTF_8);
defaultLanguageConfigResource = YamlConfiguration.loadConfiguration(defaultLanguageInputStreamReader);
} catch (Exception ignored) {
}
//嘗試當前語言資源檔(但不儲存於資料夾)
try {
Reader languageInputStreamReader = new InputStreamReader(bm.getResource(getFileName(language).replace('\\', '/')), StandardCharsets.UTF_8);
Reader languageInputStreamReader = new InputStreamReader(bm.getResource(getFileName(locale).replace('\\', '/')), StandardCharsets.UTF_8);
languageConfigResource = YamlConfiguration.loadConfiguration(languageInputStreamReader);
} catch (Exception ignored) {
}
//嘗試載入語言包檔案
String fileName = getFileName(language);
String fileName = getFileName(locale);
File file = new File(bm.getDataFolder(), fileName);
//檢查檔案是否存在
if (!file.exists()) {
Expand All @@ -55,21 +62,21 @@ public void loadLanguage() {
bm.saveResource(fileName, false);
} catch (Exception e) {
//若無該語言之語言包,則使用預設語言
language = defaultLanguage;
assert config != null;
config.set("Language", language);
KConfigManager.save(configFileName);
locale = defaultLocale;
// assert config != null;
// config.set("Language", language);
// KConfigManager.save(configFileName);
}
}
//載入語言包
KConfigManager.load(getFileName(language));
KConfigManager.load(getFileName(locale));
//檢查語言包
checkConfig(language);
bm.getLogger().info("Language: " + language);
checkConfig(locale);
bm.getLogger().info("Language: " + locale);
}

private String getFileName(String lang) {
return "language" + File.separator + lang + ".yml";
private String getFileName(Locale locale) {
return "language" + File.separator + locale.toString() + ".yml";
}

public static String tl(String path, Object... args) {
Expand All @@ -80,11 +87,11 @@ public static String tl(String path, Object... args) {
}

private String get(String path, Object... args) {
FileConfiguration config = KConfigManager.get(getFileName(language));
FileConfiguration config = KConfigManager.get(getFileName(locale));
if (!config.contains(path) || !config.isString(path)) {
//若無法取得,則自該語言資源檔取得,並儲存於語系檔
config.set(path, getFromLanguageResource(path, args));
KConfigManager.save(getFileName(language));
KConfigManager.save(getFileName(locale));
}
return replaceArgument((String) config.get(path), args);
}
Expand Down Expand Up @@ -120,9 +127,9 @@ public String getIgnoreColors(String path, Object... args) {
return ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', translatedString));
}

private void checkConfig(String lang) {
private void checkConfig(Locale checkLocale) {
//當前語言設定檔
FileConfiguration config = KConfigManager.get(getFileName(lang));
FileConfiguration config = KConfigManager.get(getFileName(checkLocale));
//根據預設語言資源檔檢查
int newSettingCount = 0;
for (String key : defaultLanguageConfigResource.getKeys(true)) {
Expand All @@ -145,8 +152,41 @@ private void checkConfig(String lang) {
newSettingCount++;
}
if (newSettingCount > 0) {
KConfigManager.save(getFileName(lang));
KConfigManager.save(getFileName(checkLocale));
bm.getServer().getConsoleSender().sendMessage(MessageUtil.format(true, tl("config.add-setting", newSettingCount)));
}
}

private static Locale parseLocale(String localeName) {
//自動
if (localeName == null || localeName.equalsIgnoreCase("auto") || localeName.length() == 0) {
return Locale.getDefault();
}
//嘗試直接搜尋
try {
return LocaleUtils.toLocale(localeName);
} catch (IllegalArgumentException ignored) {
}
//剖析重組後搜尋
Pattern pattern = Pattern.compile("^(.*?)(?:[-_](.*))?$");
Matcher matcher = pattern.matcher(localeName);
if (matcher.find()) {
String languageName = matcher.group(1).toLowerCase();
String countryName = matcher.group(2) != null ? matcher.group(2).toUpperCase() : null;
String fullLocaleName = languageName;
if (countryName != null) {
fullLocaleName += "_" + matcher.group(2).toUpperCase();
}
try {
return LocaleUtils.toLocale(fullLocaleName);
} catch (IllegalArgumentException ignored) {
try {
return LocaleUtils.toLocale(languageName);
} catch (IllegalArgumentException ignored2) {
}
}
}
//預設語言
return Locale.ENGLISH;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Language: en
Language: auto
Economy:
Enable: true
Price: 100
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Portuguese(pt-br)
# Portuguese(pt_BR)
# For BannerMaker v1.6.0
# Contributor(s): Rhander
general:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simplified Chinese(zh-cn)
# Simplified Chinese(zh_CN)
# For BannerMaker v2.1.0
# Contributor(s): KID
general:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Traditional Chinese(zh-tw)
# Traditional Chinese(zh_TW)
# For BannerMaker v2.1.0
# Contributor(s): KID
general:
Expand Down

0 comments on commit ad60683

Please sign in to comment.