Skip to content

Commit

Permalink
Fix Brazilian Portugese language loading (#2981)
Browse files Browse the repository at this point in the history
- Fix regression bug #1182
- Pass language and country as separate args for Locale constructor
  • Loading branch information
Siedlerchr authored and koppor committed Jul 9, 2017
1 parent 2ec520f commit 220d654
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed a bug which which could result in an exception when opening/saving files from/to a nonexistent directory [#2917](https://github.com/JabRef/jabref/issues/2917).
- We fixed a bug where recursive RegExpBased search found a file in a subdirectory multiple times and non-recursive RegExpBased search erroneously found files in subdirectories.
- We fixed a bug where new groups information was not stored on save [#2932](https://github.com/JabRef/jabref/issues/2932)
- We fixed a bug where the language files for Brazilian Portugese could not be loaded and the GUI localization remained in English [#1128](https://github.com/JabRef/jabref/issues/1182)

### Removed

Expand Down
25 changes: 15 additions & 10 deletions src/main/java/org/jabref/logic/l10n/Languages.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ public static Optional<Locale> convertToSupportedLocale(String language) {
Objects.requireNonNull(language);

if (!LANGUAGES.values().contains(language)) {
if (!language.contains("_")) {
return Optional.empty();
}

String lang = language.split("_")[0];
if (!LANGUAGES.values().contains(lang)) {
return Optional.empty();
}
return Optional.of(new Locale(lang));
return Optional.empty();
}
//Very important to split languages like pt_BR into two parts, because otherwise the country would be threated lowercase
//and create problems in loading
String[] languageParts = language.split("_");
Locale locale;
if (languageParts.length == 1) {
locale = new Locale(languageParts[0]);
} else if (languageParts.length == 2) {
locale = new Locale(languageParts[0], languageParts[1]);
} else {
locale = Locale.ENGLISH;
}

return Optional.of(locale);

return Optional.of(new Locale(language));
}

}
18 changes: 16 additions & 2 deletions src/test/java/org/jabref/logic/l10n/LanguagesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class LanguagesTest {

@Test
public void convertKnownLanguageOnly() {
assertEquals(Optional.of(new Locale("en")), Languages.convertToSupportedLocale("en"));
Expand All @@ -18,14 +20,26 @@ public void convertUnknownLanguage() {
assertEquals(Optional.empty(), Languages.convertToSupportedLocale("This is not a locale"));
}

@Test
public void convertKnownLanguageAndCountryCorrect() {
//Language and country code have to be separated see: https://stackoverflow.com/a/3318598
assertEquals(Optional.of(new Locale("pt", "BR")), Languages.convertToSupportedLocale("pt_BR"));
}

@Test
public void convertKnownLanguageAndCountryInCorrect() {
//Language and country code have to be separated see: https://stackoverflow.com/a/3318598
assertFalse(Optional.of(new Locale("pt_BR")).equals(Languages.convertToSupportedLocale("pt_BR")));
}

@Test
public void convertKnownLanguageAndCountryOnly() {
assertEquals(Optional.of(new Locale("en")), Languages.convertToSupportedLocale("en_US"));
assertEquals(Optional.empty(), Languages.convertToSupportedLocale("en_US"));
}

@Test
public void convertKnownLanguageAndUnknownCountry() {
assertEquals(Optional.of(new Locale("en")), Languages.convertToSupportedLocale("en_GB_unknownvariant"));
assertEquals(Optional.empty(), Languages.convertToSupportedLocale("en_GB_unknownvariant"));
}

@Test
Expand Down

0 comments on commit 220d654

Please sign in to comment.