supported lang files

This commit is contained in:
huangyuhui
2016-02-01 15:21:38 +08:00
parent ac7f025952
commit 0f18e7c1a1
10 changed files with 1588 additions and 10 deletions

View File

@@ -17,6 +17,7 @@
*/
package org.jackhuang.hellominecraft.util;
import org.jackhuang.hellominecraft.util.lang.SupportedLocales;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

View File

@@ -0,0 +1,75 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hellominecraft.util.lang;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
*
* @author huangyuhui
*/
public class Localization {
private static final String ROOT_LOCATION = "/org/jackhuang/hellominecraft/lang/I18N%s.lang";
private static final Map<Locale, Localization> INSTANCE = new HashMap<>();
private final Map<String, String> lang;
private Localization(Locale locale) {
InputStream is = Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, "_" + locale.getLanguage() + "_" + locale.getCountry()));
if (is == null)
is = Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, "_" + locale.getLanguage()));
if (is == null)
is = Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, ""));
if (is == null)
throw new RuntimeException("LANG FILE MISSING");
this.lang = new HashMap<>();
try {
String[] strings = IOUtils.readFully(is).toString().split("\n");
for (String s : strings)
if (!s.isEmpty() && s.charAt(0) != 35) {
int i = s.indexOf("=");
if (i == -1)
continue;
lang.put(s.substring(0, i), s.substring(i + 1));
}
} catch (IOException ex) {
HMCLog.err("LANG FILE MISSING", ex);
}
}
public synchronized String localize(String key) {
String s = lang.get(key);
return s == null ? key : s;
}
public static Localization get(Locale l) {
if (!INSTANCE.containsKey(l))
INSTANCE.put(l, new Localization(l));
return INSTANCE.get(l);
}
}

View File

@@ -15,10 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hellominecraft.util;
package org.jackhuang.hellominecraft.util.lang;
import java.util.Locale;
import java.util.ResourceBundle;
/**
*
@@ -29,17 +28,16 @@ public enum SupportedLocales {
public Locale self;
private String showString, customized;
private ResourceBundle bundle;
private Localization bundle;
private SupportedLocales(Locale self, String customized) {
this.self = self;
try {
bundle = ResourceBundle.getBundle("org/jackhuang/hellominecraft/lang/I18N", self);
showString = bundle.getString("lang");
bundle = Localization.get(self);
showString = bundle.localize("lang");
this.customized = customized;
} catch (Throwable t) {
showString = self.toString();
showString = name();
t.printStackTrace();
}
}
@@ -55,7 +53,7 @@ public enum SupportedLocales {
public String translate(String key, Object... format) {
try {
return bundle.getString(key);
return bundle.localize(key);
} catch (Exception ex) {
ex.printStackTrace();
return key;