Search in sources :

Example 6 with ParameterOption

use of org.eclipse.smarthome.config.core.ParameterOption in project smarthome by eclipse.

the class ConfigI18nLocalizationService method getLocalizedOptions.

/**
 * Localize parameter options.
 *
 * @param originalOptions the parameter options that should be localized
 * @param bundle the bundle the i18n resources are located
 * @param configDescriptionURI the config description URI
 * @param parameterName the name of the parameter
 * @param locale the locale it should be localized to
 * @return a list with parameter option. If an option could not be localized (e.g. no translation is found), the
 *         non-localized one is added to the list.
 */
public List<ParameterOption> getLocalizedOptions(final List<ParameterOption> originalOptions, final Bundle bundle, final URI configDescriptionURI, final String parameterName, @Nullable final Locale locale) {
    if (originalOptions == null || originalOptions.isEmpty()) {
        return originalOptions;
    }
    final List<ParameterOption> localizedOptions = new ArrayList<>();
    for (final ParameterOption option : originalOptions) {
        final String localizedLabel = this.configDescriptionParamI18nUtil.getParameterOptionLabel(bundle, configDescriptionURI, parameterName, /* key */
        option.getValue(), /* fallback */
        option.getLabel(), locale);
        final ParameterOption localizedOption = new ParameterOption(option.getValue(), localizedLabel);
        localizedOptions.add(localizedOption);
    }
    return localizedOptions;
}
Also used : ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ArrayList(java.util.ArrayList)

Example 7 with ParameterOption

use of org.eclipse.smarthome.config.core.ParameterOption in project smarthome by eclipse.

the class I18nConfigOptionsProvider method processParamType.

private Collection<ParameterOption> processParamType(String param, Locale locale, Locale translation) {
    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":
            Comparator<TimeZone> byOffset = (t1, t2) -> {
                return t1.getRawOffset() - t2.getRawOffset();
            };
            Comparator<TimeZone> byID = (t1, t2) -> {
                return t1.getID().compareTo(t2.getID());
            };
            return ZoneId.getAvailableZoneIds().stream().map(TimeZone::getTimeZone).sorted(byOffset.thenComparing(byID)).map(tz -> {
                return new ParameterOption(tz.getID(), getTimeZoneRepresentation(tz));
            }).collect(Collectors.toList());
        default:
            return null;
    }
}
Also used : Arrays(java.util.Arrays) ConfigOptionProvider(org.eclipse.smarthome.config.core.ConfigOptionProvider) TimeZone(java.util.TimeZone) Collection(java.util.Collection) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) TimeUnit(java.util.concurrent.TimeUnit) Component(org.osgi.service.component.annotations.Component) Locale(java.util.Locale) URI(java.net.URI) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) Comparator(java.util.Comparator) TimeZone(java.util.TimeZone) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption)

Example 8 with ParameterOption

use of org.eclipse.smarthome.config.core.ParameterOption in project smarthome by eclipse.

the class VoiceManagerTest method getParameterOptionsForTheDefaultVoice.

@Test
public void getParameterOptionsForTheDefaultVoice() throws URISyntaxException {
    BundleContext context = bundleContext;
    ttsService = new TTSServiceStub(context);
    registerService(ttsService);
    boolean isVoiceStubInTheOptions = false;
    Collection<ParameterOption> options = voiceManager.getParameterOptions(new URI("system:voice"), "defaultVoice", null);
    assertNotNull(options);
    for (ParameterOption option : options) {
        String optionValue = option.getValue();
        String optionLabel = option.getLabel();
        String voiceStubId = voice.getUID();
        String voiceLabel = voice.getLabel();
        if (optionValue.equals(voiceStubId) && optionLabel.equals(voiceLabel + " - " + voice.getLocale().getDisplayName())) {
            isVoiceStubInTheOptions = true;
        }
    }
    assertTrue(isVoiceStubInTheOptions);
}
Also used : TTSServiceStub(org.eclipse.smarthome.core.voice.internal.TTSServiceStub) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) URI(java.net.URI) BundleContext(org.osgi.framework.BundleContext) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 9 with ParameterOption

use of org.eclipse.smarthome.config.core.ParameterOption in project smarthome by eclipse.

the class VoiceManagerTest method getParameterOptionsForTheDefaultHli.

@Test
public void getParameterOptionsForTheDefaultHli() throws URISyntaxException {
    hliStub = new HumanLanguageInterpreterStub();
    registerService(hliStub);
    boolean isHliStubInTheOptions = false;
    Collection<ParameterOption> options = voiceManager.getParameterOptions(new URI("system:voice"), "defaultHLI", null);
    assertNotNull(options);
    for (ParameterOption option : options) {
        String optionValue = option.getValue();
        String optionLabel = option.getLabel();
        String hliStubId = hliStub.getId();
        String hliLabel = hliStub.getLabel(null);
        if (optionValue.equals(hliStubId) && optionLabel.equals(hliLabel)) {
            isHliStubInTheOptions = true;
        }
    }
    assertTrue(isHliStubInTheOptions);
}
Also used : ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) HumanLanguageInterpreterStub(org.eclipse.smarthome.core.voice.internal.HumanLanguageInterpreterStub) URI(java.net.URI) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 10 with ParameterOption

use of org.eclipse.smarthome.config.core.ParameterOption in project smarthome by eclipse.

the class ConfigI18nLocalizationService method getLocalizedConfigDescriptionParameter.

/**
 * Localize a config description parameter.
 *
 * @param bundle the bundle the i18n resources are located
 * @param configDescription the config description the parameter is part of
 * @param parameter the parameter that should be localized
 * @param locale the locale it should be localized to
 * @return a localized parameter on success, a non-localized one on error (e.g. no translation is found).
 */
public ConfigDescriptionParameter getLocalizedConfigDescriptionParameter(final Bundle bundle, final ConfigDescription configDescription, final ConfigDescriptionParameter parameter, @Nullable final Locale locale) {
    final URI configDescriptionURI = configDescription.getUID();
    final String parameterName = parameter.getName();
    final String label = this.configDescriptionParamI18nUtil.getParameterLabel(bundle, configDescriptionURI, parameterName, parameter.getLabel(), locale);
    final String description = this.configDescriptionParamI18nUtil.getParameterDescription(bundle, configDescriptionURI, parameterName, parameter.getDescription(), locale);
    final String pattern = this.configDescriptionParamI18nUtil.getParameterPattern(bundle, configDescriptionURI, parameterName, parameter.getPattern(), locale);
    final String unitLabel = this.configDescriptionParamI18nUtil.getParameterUnitLabel(bundle, configDescriptionURI, parameterName, parameter.getUnit(), parameter.getUnitLabel(), locale);
    final List<ParameterOption> options = getLocalizedOptions(parameter.getOptions(), bundle, configDescriptionURI, parameterName, locale);
    final ConfigDescriptionParameter localizedParameter = ConfigDescriptionParameterBuilder.create(parameterName, parameter.getType()).withMinimum(parameter.getMinimum()).withMaximum(parameter.getMaximum()).withStepSize(parameter.getStepSize()).withPattern(pattern).withRequired(parameter.isRequired()).withReadOnly(parameter.isReadOnly()).withMultiple(parameter.isMultiple()).withContext(parameter.getContext()).withDefault(parameter.getDefault()).withLabel(label).withDescription(description).withOptions(options).withFilterCriteria(parameter.getFilterCriteria()).withGroupName(parameter.getGroupName()).withAdvanced(parameter.isAdvanced()).withVerify(parameter.isVerifyable()).withLimitToOptions(parameter.getLimitToOptions()).withMultipleLimit(parameter.getMultipleLimit()).withUnit(parameter.getUnit()).withUnitLabel(unitLabel).build();
    return localizedParameter;
}
Also used : ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI)

Aggregations

ParameterOption (org.eclipse.smarthome.config.core.ParameterOption)16 ArrayList (java.util.ArrayList)9 URI (java.net.URI)8 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)5 Test (org.junit.Test)5 List (java.util.List)4 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)4 BigDecimal (java.math.BigDecimal)2 FilterCriteria (org.eclipse.smarthome.config.core.FilterCriteria)2 AudioSink (org.eclipse.smarthome.core.audio.AudioSink)2 TTSServiceStub (org.eclipse.smarthome.core.voice.internal.TTSServiceStub)2 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 ZoneId (java.time.ZoneId)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Locale (java.util.Locale)1 TimeZone (java.util.TimeZone)1 TimeUnit (java.util.concurrent.TimeUnit)1