-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse negative numbers in Norwegian (and 59 other languages)
The DecimalFormatSymbols for Norwegian and 59 other languages use the minus-sign (unicode 8722) instead of the hyphen-minus sign (ascii 45). While technically correct, Gherkin is written on regular keyboards and there is no practical way to write a minus-sign. By patching the `DecimalFormatSymbols` with a regular minus sign we solve this problem. Additionally, for the same reason, the non-breaking space (ascii 160) and right single quotation mark (unicode 8217) for thousands separators are also patched with either a period or colon. Fixes: #287
- Loading branch information
1 parent
b3f0892
commit ca1ecd2
Showing
8 changed files
with
220 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
java/src/main/java/io/cucumber/cucumberexpressions/KeyboardFriendlyDecimalFormatSymbols.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.cucumber.cucumberexpressions; | ||
|
||
import java.text.DecimalFormatSymbols; | ||
import java.util.Locale; | ||
|
||
/** | ||
* A set of localized decimal symbols that can be written on a regular keyboard. | ||
* <p> | ||
* Note quite complete, feel free to make a suggestion. | ||
*/ | ||
class KeyboardFriendlyDecimalFormatSymbols { | ||
|
||
static DecimalFormatSymbols getInstance(Locale locale) { | ||
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); | ||
|
||
// Replace the minus sign with minus-hyphen as available on most keyboards. | ||
if (symbols.getMinusSign() == '\u2212') { | ||
symbols.setMinusSign('-'); | ||
} | ||
|
||
if (symbols.getDecimalSeparator() == '.') { | ||
// For locales that use the period as the decimal separator | ||
// always use the comma for thousands. The alternatives are | ||
// not available on a keyboard | ||
symbols.setGroupingSeparator(','); | ||
} else if (symbols.getDecimalSeparator() == ',') { | ||
// For locales that use the comma as the decimal separator | ||
// always use the period for thousands. The alternatives are | ||
// not available on a keyboard | ||
symbols.setGroupingSeparator('.'); | ||
} | ||
return symbols; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...c/test/java/io/cucumber/cucumberexpressions/KeyboardFriendlyDecimalFormatSymbolsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.cucumber.cucumberexpressions; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.text.DecimalFormatSymbols; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static java.util.stream.Collectors.groupingBy; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
class KeyboardFriendlyDecimalFormatSymbolsTest { | ||
|
||
@Test | ||
void listMinusSigns(){ | ||
System.out.println("Original minus signs:"); | ||
listMinusSigns(DecimalFormatSymbols::getInstance); | ||
System.out.println(); | ||
System.out.println("Friendly minus signs:"); | ||
listMinusSigns(KeyboardFriendlyDecimalFormatSymbols::getInstance); | ||
System.out.println(); | ||
} | ||
|
||
private static void listMinusSigns(Function<Locale, DecimalFormatSymbols> supplier) { | ||
getAvailableLocalesAsStream() | ||
.collect(groupingBy(locale -> supplier.apply(locale).getMinusSign())) | ||
.forEach((c, locales) -> System.out.println(render(c) + " " + render(locales))); | ||
} | ||
|
||
@Test | ||
void listDecimalAndGroupingSeparators(){ | ||
System.out.println("Original decimal and group separators:"); | ||
listDecimalAndGroupingSeparators(DecimalFormatSymbols::getInstance); | ||
System.out.println(); | ||
System.out.println("Friendly decimal and group separators:"); | ||
listDecimalAndGroupingSeparators(KeyboardFriendlyDecimalFormatSymbols::getInstance); | ||
System.out.println(); | ||
} | ||
|
||
private static void listDecimalAndGroupingSeparators(Function<Locale, DecimalFormatSymbols> supplier) { | ||
getAvailableLocalesAsStream() | ||
.collect(groupingBy(locale -> { | ||
DecimalFormatSymbols symbols = supplier.apply(locale); | ||
return new SimpleEntry<>(symbols.getDecimalSeparator(), symbols.getGroupingSeparator()); | ||
})) | ||
.entrySet() | ||
.stream() | ||
.sorted(comparing(entry -> entry.getKey().getKey())) | ||
.forEach((entry) -> { | ||
SimpleEntry<Character, Character> characters = entry.getKey(); | ||
List<Locale> locales = entry.getValue(); | ||
System.out.println(render(characters.getKey()) + " " + render(characters.getValue()) + " " + render(locales)); | ||
}); | ||
} | ||
|
||
@Test | ||
void listExponentSigns(){ | ||
System.out.println("Original exponent signs:"); | ||
listExponentSigns(DecimalFormatSymbols::getInstance); | ||
System.out.println(); | ||
System.out.println("Friendly exponent signs:"); | ||
listExponentSigns(KeyboardFriendlyDecimalFormatSymbols::getInstance); | ||
System.out.println(); | ||
} | ||
|
||
private static void listExponentSigns(Function<Locale, DecimalFormatSymbols> supplier) { | ||
getAvailableLocalesAsStream() | ||
.collect(groupingBy(locale -> supplier.apply(locale).getExponentSeparator())) | ||
.forEach((s, locales) -> { | ||
if (s.length() == 1) { | ||
System.out.println(render(s.charAt(0)) + " " + render(locales)); | ||
} else { | ||
System.out.println(s + " " + render(locales)); | ||
} | ||
}); | ||
} | ||
|
||
private static Stream<Locale> getAvailableLocalesAsStream() { | ||
return Arrays.stream(DecimalFormatSymbols.getAvailableLocales()); | ||
} | ||
|
||
private static String render(Character character) { | ||
return character + " (" + (int) character + ")"; | ||
} | ||
|
||
private static String render(List<Locale> locales) { | ||
return locales.size() + ": " + locales.stream() | ||
.sorted(comparing(Locale::getDisplayName)) | ||
.map(Locale::getDisplayName) | ||
.collect(toList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters