Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
config option provider for language & region settings
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
  • Loading branch information
sjsf committed Jun 27, 2017
1 parent 598ceef commit d6b71d4
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
Expand Up @@ -3,6 +3,7 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/test/groovy"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
<classpathentry kind="output" path="target/test-classes"/>
</classpath>
@@ -0,0 +1,59 @@
/**
* 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 static org.junit.Assert.*;

import java.net.URI;
import java.util.Locale;

import org.eclipse.smarthome.config.core.ParameterOption;
import org.junit.Before;
import org.junit.Test;

/**
* Tests for {@link I18nConfigOptionsProvider}
*
* @author Simon Kaufmann - initial contribution and API
*
*/
public class I18nConfigOptionsProviderTest {

private I18nConfigOptionsProvider provider;
private ParameterOption expectedLangEN = new ParameterOption("en", "English");
private ParameterOption expectedLangFR = new ParameterOption("en", "anglais");
private ParameterOption expectedCntryEN = new ParameterOption("US", "United States");
private ParameterOption expectedCntryFR = new ParameterOption("US", "Etats-Unis");
private URI uriI18N;

@Before
public void setup() throws Exception {
provider = new I18nConfigOptionsProvider();
uriI18N = new URI("system:i18n");
}

@Test
public void testLanguage() throws Exception {
assertTrue(provider.getParameterOptions(uriI18N, "language", Locale.US).contains(expectedLangEN));
assertTrue(provider.getParameterOptions(uriI18N, "language", Locale.FRENCH).contains(expectedLangFR));
assertFalse(provider.getParameterOptions(uriI18N, "language", null).isEmpty());
}

@Test
public void testRegion() throws Exception {
assertTrue(provider.getParameterOptions(uriI18N, "region", Locale.US).contains(expectedCntryEN));
assertTrue(provider.getParameterOptions(uriI18N, "region", Locale.FRENCH).contains(expectedCntryFR));
assertFalse(provider.getParameterOptions(uriI18N, "region", null).isEmpty());
}

@Test
public void testUnknownParameter() throws Exception {
assertNull(provider.getParameterOptions(uriI18N, "unknown", Locale.US));
}

}
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.eclipse.smarthome.config.core.internal.i18n.I18nConfigOptionsProvider">
<implementation class="org.eclipse.smarthome.config.core.internal.i18n.I18nConfigOptionsProvider"/>
<service>
<provide interface="org.eclipse.smarthome.config.core.ConfigOptionProvider"/>
</service>

</scr:component>
Expand Up @@ -45,4 +45,42 @@ public String toString() {
return this.getClass().getSimpleName() + " [value=\"" + value + "\", label=\"" + label + "\"]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((label == null) ? 0 : label.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ParameterOption other = (ParameterOption) obj;
if (label == null) {
if (other.label != null) {
return false;
}
} else if (!label.equals(other.label)) {
return false;
}
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}

}
@@ -0,0 +1,49 @@
/**
* 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.Comparator;
import java.util.Locale;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.smarthome.config.core.ConfigOptionProvider;
import org.eclipse.smarthome.config.core.ParameterOption;

/**
* {@link ConfigOptionProvider} that provides a list of
*
* @author Simon Kaufmann - initial contribution and API
*
*/
public class I18nConfigOptionsProvider implements ConfigOptionProvider {

@Override
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) {
if (uri.toString().equals("system:i18n")) {
Locale translation = locale != null ? locale : Locale.getDefault();
if (param.equals("language")) {
return getAvailable(locale, l -> new ParameterOption(l.getLanguage(), l.getDisplayLanguage(translation)));
} else if (param.equals("region")) {
return getAvailable(locale, l -> new ParameterOption(l.getCountry(), l.getDisplayCountry(translation)));
} else if (param.equals("variant")) {
return getAvailable(locale, l -> new ParameterOption(l.getVariant(), l.getDisplayVariant(translation)));
}
}
return null;
}

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());
}

}

0 comments on commit d6b71d4

Please sign in to comment.