mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 04:08:01 +00:00
Merge branch 'master' into 'master'
[Mobile] Fix ExceptionInInitializerError forge.screens.home.NewGameMenu$NewGameScreen See merge request core-developers/forge!4641
This commit is contained in:
@@ -16,146 +16,154 @@ import java.util.ResourceBundle;
|
|||||||
|
|
||||||
public class Localizer {
|
public class Localizer {
|
||||||
|
|
||||||
private static Localizer instance;
|
private static Localizer instance;
|
||||||
|
|
||||||
private List<LocalizationChangeObserver> observers = new ArrayList<>();
|
private List<LocalizationChangeObserver> observers = new ArrayList<>();
|
||||||
|
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
private ResourceBundle resourceBundle;
|
private ResourceBundle resourceBundle;
|
||||||
|
|
||||||
public static Localizer getInstance() {
|
public static Localizer getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
synchronized (Localizer.class) {
|
synchronized (Localizer.class) {
|
||||||
instance = new Localizer();
|
instance = new Localizer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Localizer() {
|
private Localizer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(String localeID, String languagesDirectory) {
|
public void initialize(String localeID, String languagesDirectory) {
|
||||||
setLanguage(localeID, languagesDirectory);
|
setLanguage(localeID, languagesDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String convert(String value, String fromEncoding, String toEncoding) throws UnsupportedEncodingException {
|
public String convert(String value, String fromEncoding, String toEncoding) throws UnsupportedEncodingException {
|
||||||
return new String(value.getBytes(fromEncoding), toEncoding);
|
return new String(value.getBytes(fromEncoding), toEncoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String charset(String value, String charsets[]) {
|
public String charset(String value, String charsets[]) {
|
||||||
String probe = StandardCharsets.UTF_8.name();
|
String probe = StandardCharsets.UTF_8.name();
|
||||||
for(String c : charsets) {
|
for(String c : charsets) {
|
||||||
Charset charset = Charset.forName(c);
|
Charset charset = Charset.forName(c);
|
||||||
if(charset != null) {
|
if(charset != null) {
|
||||||
try {
|
try {
|
||||||
if (value.equals(convert(convert(value, charset.name(), probe), probe, charset.name()))) {
|
if (value.equals(convert(convert(value, charset.name(), probe), probe, charset.name()))) {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
} catch(UnsupportedEncodingException ignored) {}
|
} catch(UnsupportedEncodingException ignored) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return StandardCharsets.UTF_8.name();
|
return StandardCharsets.UTF_8.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMessage(final String key, final Object... messageArguments) {
|
public String getMessage(final String key, String defaultValue, final Object... messageArguments) {
|
||||||
MessageFormat formatter = null;
|
try {
|
||||||
|
return getMessage(key, messageArguments);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//FIXME: localizer should return default value from english locale or it will crash some GUI element like the NewGameMenu->NewGameScreen Popup when returned null...
|
||||||
|
public String getMessage(final String key, final Object... messageArguments) {
|
||||||
|
MessageFormat formatter = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//formatter = new MessageFormat(resourceBundle.getString(key.toLowerCase()), locale);
|
//formatter = new MessageFormat(resourceBundle.getString(key.toLowerCase()), locale);
|
||||||
formatter = new MessageFormat(resourceBundle.getString(key), locale);
|
formatter = new MessageFormat(resourceBundle.getString(key), locale);
|
||||||
} catch (final IllegalArgumentException | MissingResourceException e) {
|
} catch (final IllegalArgumentException | MissingResourceException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formatter == null) {
|
if (formatter == null) {
|
||||||
System.err.println("INVALID PROPERTY: '" + key + "' -- Translation Needed?");
|
System.err.println("INVALID PROPERTY: '" + key + "' -- Translation Needed?");
|
||||||
return "INVALID PROPERTY: '" + key + "' -- Translation Needed?";
|
return "INVALID PROPERTY: '" + key + "' -- Translation Needed?";
|
||||||
}
|
}
|
||||||
|
|
||||||
formatter.setLocale(locale);
|
formatter.setLocale(locale);
|
||||||
|
|
||||||
String formattedMessage = "CHAR ENCODING ERROR";
|
String formattedMessage = "CHAR ENCODING ERROR";
|
||||||
final String[] charsets = { "ISO-8859-1", "UTF-8" };
|
final String[] charsets = { "ISO-8859-1", "UTF-8" };
|
||||||
//Support non-English-standard characters
|
//Support non-English-standard characters
|
||||||
String detectedCharset = charset(resourceBundle.getString(key), charsets);
|
String detectedCharset = charset(resourceBundle.getString(key), charsets);
|
||||||
|
|
||||||
final int argLength = messageArguments.length;
|
final int argLength = messageArguments.length;
|
||||||
Object[] syncEncodingMessageArguments = new Object[argLength];
|
Object[] syncEncodingMessageArguments = new Object[argLength];
|
||||||
//when messageArguments encoding not equal resourceBundle.getString(key),convert to equal
|
//when messageArguments encoding not equal resourceBundle.getString(key),convert to equal
|
||||||
//avoid convert to a have two encoding content formattedMessage string.
|
//avoid convert to a have two encoding content formattedMessage string.
|
||||||
for (int i = 0; i < argLength; i++) {
|
for (int i = 0; i < argLength; i++) {
|
||||||
String objCharset = charset(messageArguments[i].toString(), charsets);
|
String objCharset = charset(messageArguments[i].toString(), charsets);
|
||||||
try {
|
try {
|
||||||
syncEncodingMessageArguments[i] = convert(messageArguments[i].toString(), objCharset, detectedCharset);
|
syncEncodingMessageArguments[i] = convert(messageArguments[i].toString(), objCharset, detectedCharset);
|
||||||
} catch (UnsupportedEncodingException ignored) {
|
} catch (UnsupportedEncodingException ignored) {
|
||||||
System.err.println("Cannot Convert '" + messageArguments[i].toString() + "' from '" + objCharset + "' To '" + detectedCharset + "'");
|
System.err.println("Cannot Convert '" + messageArguments[i].toString() + "' from '" + objCharset + "' To '" + detectedCharset + "'");
|
||||||
return "encoding '" + key + "' translate string failure";
|
return "encoding '" + key + "' translate string failure";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
formattedMessage = new String(formatter.format(syncEncodingMessageArguments).getBytes(detectedCharset), StandardCharsets.UTF_8);
|
formattedMessage = new String(formatter.format(syncEncodingMessageArguments).getBytes(detectedCharset), StandardCharsets.UTF_8);
|
||||||
} catch(UnsupportedEncodingException ignored) {}
|
} catch(UnsupportedEncodingException ignored) {}
|
||||||
|
|
||||||
return formattedMessage;
|
return formattedMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLanguage(final String languageRegionID, final String languagesDirectory) {
|
public void setLanguage(final String languageRegionID, final String languagesDirectory) {
|
||||||
|
|
||||||
String[] splitLocale = languageRegionID.split("-");
|
String[] splitLocale = languageRegionID.split("-");
|
||||||
|
|
||||||
Locale oldLocale = locale;
|
Locale oldLocale = locale;
|
||||||
locale = new Locale(splitLocale[0], splitLocale[1]);
|
locale = new Locale(splitLocale[0], splitLocale[1]);
|
||||||
|
|
||||||
//Don't reload the language if nothing changed
|
//Don't reload the language if nothing changed
|
||||||
if (oldLocale == null || !oldLocale.equals(locale)) {
|
if (oldLocale == null || !oldLocale.equals(locale)) {
|
||||||
|
|
||||||
File file = new File(languagesDirectory);
|
File file = new File(languagesDirectory);
|
||||||
URL[] urls = null;
|
URL[] urls = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
urls = new URL[] { file.toURI().toURL() };
|
urls = new URL[] { file.toURI().toURL() };
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassLoader loader = new URLClassLoader(urls);
|
ClassLoader loader = new URLClassLoader(urls);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
resourceBundle = ResourceBundle.getBundle(languageRegionID, new Locale(splitLocale[0], splitLocale[1]), loader);
|
resourceBundle = ResourceBundle.getBundle(languageRegionID, new Locale(splitLocale[0], splitLocale[1]), loader);
|
||||||
} catch (NullPointerException | MissingResourceException e) {
|
} catch (NullPointerException | MissingResourceException e) {
|
||||||
//If the language can't be loaded, default to US English
|
//If the language can't be loaded, default to US English
|
||||||
resourceBundle = ResourceBundle.getBundle("en-US", new Locale("en", "US"), loader);
|
resourceBundle = ResourceBundle.getBundle("en-US", new Locale("en", "US"), loader);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Language '" + resourceBundle.toString() + "' loaded successfully.");
|
System.out.println("Language '" + resourceBundle.toString() + "' loaded successfully.");
|
||||||
|
|
||||||
notifyObservers();
|
notifyObservers();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Language> getLanguages() {
|
public List<Language> getLanguages() {
|
||||||
//TODO List all languages by getting their files
|
//TODO List all languages by getting their files
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerObserver(LocalizationChangeObserver observer) {
|
public void registerObserver(LocalizationChangeObserver observer) {
|
||||||
observers.add(observer);
|
observers.add(observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyObservers() {
|
private void notifyObservers() {
|
||||||
for (LocalizationChangeObserver observer : observers) {
|
for (LocalizationChangeObserver observer : observers) {
|
||||||
observer.localizationChanged();
|
observer.localizationChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Language {
|
public static class Language {
|
||||||
public String languageName;
|
public String languageName;
|
||||||
public String languageID;
|
public String languageID;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,13 +24,13 @@ public class NewGameMenu extends FPopupMenu {
|
|||||||
final static Localizer localizer = Localizer.getInstance();
|
final static Localizer localizer = Localizer.getInstance();
|
||||||
|
|
||||||
public enum NewGameScreen {
|
public enum NewGameScreen {
|
||||||
Constructed(localizer.getMessage("lblConstructed"), FSkinImage.MENU_CONSTRUCTED, ConstructedScreen.class),
|
Constructed(localizer.getMessage("lblConstructed", "Constructed"), FSkinImage.MENU_CONSTRUCTED, ConstructedScreen.class),
|
||||||
BoosterDraft(localizer.getMessage("lblBoosterDraft"), FSkinImage.MENU_DRAFT, NewDraftScreen.class),
|
BoosterDraft(localizer.getMessage("lblBoosterDraft", "Booster Draft"), FSkinImage.MENU_DRAFT, NewDraftScreen.class),
|
||||||
SealedDeck(localizer.getMessage("lblSealedDeck"), FSkinImage.MENU_SEALED, NewSealedScreen.class),
|
SealedDeck(localizer.getMessage("lblSealedDeck", "Sealed Deck"), FSkinImage.MENU_SEALED, NewSealedScreen.class),
|
||||||
QuestMode(localizer.getMessage("lblQuestMode"), FSkinImage.QUEST_ZEP, NewQuestScreen.class),
|
QuestMode(localizer.getMessage("lblQuestMode", "Quest Mode"), FSkinImage.QUEST_ZEP, NewQuestScreen.class),
|
||||||
PuzzleMode(localizer.getMessage("lblPuzzleMode"), FSkinImage.MENU_PUZZLE, PuzzleScreen.class),
|
PuzzleMode(localizer.getMessage("lblPuzzleMode", "Puzzle Mode"), FSkinImage.MENU_PUZZLE, PuzzleScreen.class),
|
||||||
PlanarConquest(localizer.getMessage("lblPlanarConquest"), FSkinImage.MENU_GALAXY, NewConquestScreen.class),
|
PlanarConquest(localizer.getMessage("lblPlanarConquest", "Planar Conquest"), FSkinImage.MENU_GALAXY, NewConquestScreen.class),
|
||||||
Gauntlet(localizer.getMessage("lblGauntlet"), FSkinImage.MENU_GAUNTLET, NewGauntletScreen.class);
|
Gauntlet(localizer.getMessage("lblGauntlet", "Gauntlet"), FSkinImage.MENU_GAUNTLET, NewGauntletScreen.class);
|
||||||
|
|
||||||
private final FMenuItem item;
|
private final FMenuItem item;
|
||||||
private final Class<? extends FScreen> screenClass;
|
private final Class<? extends FScreen> screenClass;
|
||||||
@@ -51,7 +51,7 @@ public class NewGameMenu extends FPopupMenu {
|
|||||||
if (screen == null) { //don't initialize screen until it's opened the first time
|
if (screen == null) { //don't initialize screen until it's opened the first time
|
||||||
try {
|
try {
|
||||||
screen = screenClass.newInstance();
|
screen = screenClass.newInstance();
|
||||||
screen.setHeaderCaption(localizer.getMessage("lblNewGame") + " - " + item.getText());
|
screen.setHeaderCaption(localizer.getMessage("lblNewGame", "New Game") + " - " + item.getText());
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
Reference in New Issue
Block a user