Added options for language & regional settings #3755
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/org.eclipse.smarthome.config.core.internal.i18n.I18nConfigOptionsProvider.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* 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; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
/** | ||
* {@link ConfigOptionProvider} that provides a list of | ||
* | ||
* @author Simon Kaufmann - initial contribution and API | ||
* | ||
*/ | ||
@Component(immediate = true) | ||
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")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a param with a null name? how should that work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No clue if param is never null in the callers. Just the contract was not clear about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, the contract in fact is pretty clear: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. I only looked into |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the contract, we need to return empty list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right - javadoc and caller implementation don't match There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a syntactic sugar: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that's personal flavor, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. definitely. It's upto you. |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the contract doesn't specify any throws clause, we cannot add any invariants here. Hence we could do this ->
"system:i18n".equals(String.valueOf(uri))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uri is a non-null argument (see caller)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a quick look on the contract and the contract doesn't specify uri to be non null. If you think, the callers will never use null, you can ignore this.