forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocaleUtils.java
More file actions
43 lines (36 loc) · 1.17 KB
/
LocaleUtils.java
File metadata and controls
43 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
public class LocaleUtils {
public static Optional<List<Locale.LanguageRange>> parseRanges(final String value) {
if (value == null) {
return Optional.empty();
}
// remove trailing ';' well-formed vs ill-formed
String wellFormed = value;
if (wellFormed.endsWith(";")) {
wellFormed = wellFormed.substring(0, wellFormed.length() - 1);
}
try {
return Optional.of(Locale.LanguageRange.parse(wellFormed)
.stream()
.sorted(comparing(Locale.LanguageRange::getWeight).reversed())
.collect(toList()));
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
public static Optional<List<Locale>> parseLocales(final String value) {
return parseRanges(value).map(l -> l.stream()
.map(r -> Locale.forLanguageTag(r.getRange()))
.collect(toList()));
}
}