This repository has been archived by the owner on May 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config option provider for language & region settings
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 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
59 changes: 59 additions & 0 deletions
59
...t/java/org/eclipse/smarthome/config/core/internal/i18n/I18nConfigOptionsProviderTest.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,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)); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
bundles/config/org.eclipse.smarthome.config.core/OSGI-INF/I18nConfigOptionsProvider.xml
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,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> |
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
49 changes: 49 additions & 0 deletions
49
.../main/java/org/eclipse/smarthome/config/core/internal/i18n/I18nConfigOptionsProvider.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,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()); | ||
} | ||
|
||
} |