This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 787
/
I18nConfigOptionsProvider.java
92 lines (85 loc) · 4.01 KB
/
I18nConfigOptionsProvider.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.config.core.internal.i18n;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.eclipse.smarthome.config.core.ConfigOptionProvider;
import org.eclipse.smarthome.config.core.ParameterOption;
import org.osgi.service.component.annotations.Component;
/**
* {@link ConfigOptionProvider} that provides a list of
*
* @author Simon Kaufmann - initial contribution and API
* @author Erdoan Hadzhiyusein - Added time zone
*
*/
@Component(immediate = true)
public class I18nConfigOptionsProvider implements ConfigOptionProvider {
private static final String NO_OFFSET_FORMAT = "(GMT) %s";
private static final String NEGATIVE_OFFSET_FORMAT = "%s (GMT%d:%02d)";
private static final String POSITIVE_OFFSET_FORMAT = "%s (GMT+%d:%02d)";
@Override
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) {
if (uri.toString().equals("system:i18n")) {
Locale translation = locale != null ? locale : Locale.getDefault();
switch (param) {
case "language":
return getAvailable(locale,
l -> new ParameterOption(l.getLanguage(), l.getDisplayLanguage(translation)));
case "region":
return getAvailable(locale,
l -> new ParameterOption(l.getCountry(), l.getDisplayCountry(translation)));
case "variant":
return getAvailable(locale,
l -> new ParameterOption(l.getVariant(), l.getDisplayVariant(translation)));
case "timezone":
Collection<ParameterOption> collection = new LinkedList<ParameterOption>();
String[] ids = TimeZone.getAvailableIDs();
List<TimeZone> timeZones = Arrays.asList(ids).stream().map(TimeZone::getTimeZone)
.collect(Collectors.toList());
Collections.sort(timeZones, (s1, s2) -> s2.getRawOffset() - s1.getRawOffset());
// sorting the time zones by offset
for (TimeZone instance : timeZones) {
collection.add(new ParameterOption(instance.getID(), getTimeZoneRepresentation(instance)));
}
return collection;
default:
return null;
}
}
return null;
}
private static String getTimeZoneRepresentation(TimeZone tz) {
long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset()) - TimeUnit.HOURS.toMinutes(hours);
minutes = Math.abs(minutes);
String result = "";
if (hours > 0) {
result = String.format(POSITIVE_OFFSET_FORMAT, tz.getID(), hours, minutes);
} else if (hours < 0) {
result = String.format(NEGATIVE_OFFSET_FORMAT, tz.getID(), hours, minutes);
} else {
result = String.format(NO_OFFSET_FORMAT, tz.getDisplayName(Locale.getDefault()));
}
return result;
}
private Collection<ParameterOption> getAvailable(Locale locale, Function<Locale, ParameterOption> mapFunction) {
return Arrays.stream(Locale.getAvailableLocales()).map(l -> mapFunction.apply(l)).distinct()
.sorted(Comparator.comparing(a -> a.getLabel())).collect(Collectors.toList());
}
}