Skip to content

Commit

Permalink
Add support for German monts at the normalizer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Jan 18, 2018
1 parent 50471c6 commit 0294b01
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We now show a small notification icon in the entry editor when we detect data inconsistency or other problems. [#3145](https://github.com/JabRef/jabref/issues/3145)
- We added [oaDOI](https://oadoi.org/) as a fulltext provider, so that JabRef is now able to provide fulltexts for more than 90 million open-access articles.
- We changed one default of [Cleanup entries dialog](http://help.jabref.org/en/CleanupEntries): Per default, the PDF are not moved to the default file directory anymore. [#3619](https://github.com/JabRef/jabref/issues/3619)
- We enhanced the cleanup functionality to normalize month names in other languages such as German, too.
- We added the export of the the `translator` field to the according MS-Office XML field. [#1750, comment](https://github.com/JabRef/jabref/issues/1750#issuecomment-357350986)
- We changed the import of the MS-Office XML fields `bookauthor` and `translator`. Both are now imported to their corresponding bibtex/biblatex fields.
- We improved the export of the `address` and `location` field to the MS-Office XML fields. If the address field does not contain a comma, it is treated as single value and exported to the field `city`. [#1750, comment](https://github.com/JabRef/jabref/issues/1750#issuecomment-357539167)
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/org/jabref/model/entry/Month.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.jabref.model.entry;

import java.text.DateFormatSymbols;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

import org.jabref.model.strings.LatexToUnicodeAdapter;
import org.jabref.model.strings.StringUtil;

/**
Expand Down Expand Up @@ -93,8 +97,44 @@ public static Optional<Month> parse(String value) {
int number = Integer.parseInt(value);
return Month.getMonthByNumber(number);
} catch (NumberFormatException e) {
return Optional.empty();
return parseLocalizedMonth(value);
}
}

/**
* Parses a month having the string in a localized form such as "Juli"
*
* @param value the text value - must not me null
* @return the corresponding month instance, empty if none was found
*/
private static Optional<Month> parseLocalizedMonth(String value) {
Objects.requireNonNull(value);

// Normalize month name to unicode, which is returned by DateFormatSymbols.getMonths() - see below
value = new LatexToUnicodeAdapter().format(value);

Optional<Month> month = Optional.empty();

if (value.equalsIgnoreCase("Maerz")) {
// this value is not treated by LatexToUnicodeAdapter, so a special handling is required.
month = Month.getMonthByNumber(3);
} else {
Locale[] availableLocales = Locale.getAvailableLocales();
int i = 0;
do {
String[] months = new DateFormatSymbols(availableLocales[i]).getMonths();
int j = 0;
do {
if (value.equalsIgnoreCase(months[j])) {
month = Month.getMonthByNumber(j + 1);
}
j++;
} while (!month.isPresent() && (j < months.length));
i++;
} while (!month.isPresent() && (i < availableLocales.length));
}

return month;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,24 @@ public void formatExample() {
Assert.assertEquals("#dec#", formatter.format(formatter.getExampleInput()));
}

@Test
public void formatGermanMarch() {
Assert.assertEquals("#mar#", formatter.format("Maerz"));
}

@Test
public void formatGermanMarchInUtf8() {
Assert.assertEquals("#mar#", formatter.format("März"));
}

@Test
public void formatGermanMarchInLaTeX() {
Assert.assertEquals("#mar#", formatter.format("M\\\"arz"));
}

@Test
public void formatGermanMarchInLaTeXWithBraces() {
Assert.assertEquals("#mar#", formatter.format("M{\\\"a}rz"));
}

}

0 comments on commit 0294b01

Please sign in to comment.