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