Skip to content

Commit

Permalink
Fix faulty encoding transformation, all message bundles should be enc…
Browse files Browse the repository at this point in the history
…oded in UTF-8. (#2192)
  • Loading branch information
breiler committed Apr 4, 2023
1 parent de1092f commit 18588b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2018 Will Winder
Copyright 2013-2023 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -32,23 +32,22 @@ This file is part of Universal Gcode Sender (UGS).

/**
* Localization messages.
*
*
* @author wwinder
*/
public class Localization {
private static final Logger logger = Logger.getLogger(Localization.class.getName());
public final static DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();
static {
dfs.setDecimalSeparator('.');
dfs.setMinusSign('-');
}

private static final Logger logger = Logger.getLogger(Localization.class.getName());
private static ResourceBundle bundle = null;
private static ResourceBundle english = null;

private static int englishKeyCount = 0;
private static String region = null;

static {
dfs.setDecimalSeparator('.');
dfs.setMinusSign('-');
}

/**
* Loads a given language. If no translations is found for the given language it will default to english.
*
Expand All @@ -64,7 +63,7 @@ synchronized public static boolean initialize(String language) {
* Loads a given language. If no translations is found for the given language it will default to english.
*
* @param language the language to load, ex: en, sv, de
* @param region the region of the language to load, ex: US, SE, DE
* @param region the region of the language to load, ex: US, SE, DE
* @return Returns false if some keys are missing compared to "en_US"
*/
synchronized public static boolean initialize(String language, String region) {
Expand All @@ -81,7 +80,7 @@ synchronized public static boolean initialize(String language, String region) {
* Loads the resource bundle with all translations for the given language and region
*
* @param language the language to load, ex: en, sv, de
* @param region the region of the language to load, ex: US, SE, DE
* @param region the region of the language to load, ex: US, SE, DE
* @throws MissingResourceException if the resource bundle couldn't be found
*/
private static void loadResourceBundle(String language, String region) throws MissingResourceException {
Expand Down Expand Up @@ -109,19 +108,17 @@ public static String getString(String id, String region) {
public static String getString(String id) {
String result = "";
try {
String val = bundle.getString(id);
result = new String(val.getBytes("ISO-8859-1"), "UTF-8");
result = bundle.getString(id);
} catch (Exception e) {
// Ignore this error, we will later try to fetch the string from the english bundle
}

if( StringUtils.isEmpty(StringUtils.trimToEmpty(result))) {
if (StringUtils.isEmpty(StringUtils.trimToEmpty(result))) {
try {
if (english == null) {
english = ResourceBundle.getBundle("resources.MessagesBundle", new Locale("en", "US"));
}
String val = english.getString(id);
result = new String(val.getBytes("ISO-8859-1"), "UTF-8");
result = english.getString(id);
} catch (Exception e) {
result = "<" + id + ">";
}
Expand All @@ -138,7 +135,7 @@ public static Map<String, String> getStrings() {

private static int getEnglishKeyCount() {
if (englishKeyCount > 0) return englishKeyCount;
ResourceBundle b= ResourceBundle.getBundle("resources.MessagesBundle", new Locale("en", "US"));
ResourceBundle b = ResourceBundle.getBundle("resources.MessagesBundle", new Locale("en", "US"));
englishKeyCount = getKeyCount(b);
return englishKeyCount;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void loadLocalizationThatDoesExistShouldLoad() {
assertEquals("SE", Locale.getDefault().getCountry());
}

@Test
public void loadCyrillicCharacters() {
Localization.initialize("uk", "UA");
assertEquals("Версія", Localization.getString("version"));
}

@Test
public void minusSignsShouldBeConvertedWithRightCharacter() {
NumberFormat formatter = new DecimalFormat("#.###", Localization.dfs);
Expand Down

0 comments on commit 18588b1

Please sign in to comment.