forked from openhab/openhab-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added i18n feature for profiles (openhab#785)
Signed-off-by: Christoph Weitkamp <[email protected]>
- Loading branch information
Showing
8 changed files
with
350 additions
and
28 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
58 changes: 58 additions & 0 deletions
58
...rc/main/java/org/eclipse/smarthome/core/thing/internal/profiles/i18n/ProfileI18nUtil.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,58 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.core.thing.internal.profiles.i18n; | ||
|
||
import java.util.Locale; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.smarthome.core.i18n.I18nUtil; | ||
import org.eclipse.smarthome.core.i18n.TranslationProvider; | ||
import org.eclipse.smarthome.core.thing.profiles.Profile; | ||
import org.eclipse.smarthome.core.thing.profiles.ProfileType; | ||
import org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID; | ||
import org.eclipse.smarthome.core.thing.profiles.i18n.ProfileTypeI18nLocalizationService; | ||
import org.osgi.framework.Bundle; | ||
|
||
/** | ||
* A utility service which localizes {@link Profile}s. | ||
* Falls back to a localized {@link ProfileType} for label and description when not given otherwise. | ||
* | ||
* @see {@link ProfileTypeI18nLocalizationService} | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ProfileI18nUtil { | ||
|
||
private final TranslationProvider i18nProvider; | ||
|
||
/** | ||
* Create a new util instance and pass the appropriate dependencies. | ||
* | ||
* @param i18nProvider an instance of {@link TranslationProvider}. | ||
*/ | ||
public ProfileI18nUtil(TranslationProvider i18nProvider) { | ||
this.i18nProvider = i18nProvider; | ||
} | ||
|
||
public @Nullable String getProfileLabel(Bundle bundle, ProfileTypeUID profileTypeUID, String defaultLabel, | ||
@Nullable Locale locale) { | ||
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferProfileTypeKey(profileTypeUID, "label")); | ||
return i18nProvider.getText(bundle, key, defaultLabel, locale); | ||
} | ||
|
||
private String inferProfileTypeKey(ProfileTypeUID profileTypeUID, String lastSegment) { | ||
return "profile-type." + profileTypeUID.getBindingId() + "." + profileTypeUID.getId() + "." + lastSegment; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
...va/org/eclipse/smarthome/core/thing/profiles/i18n/ProfileTypeI18nLocalizationService.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,66 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.core.thing.profiles.i18n; | ||
|
||
import java.util.Locale; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.smarthome.core.i18n.TranslationProvider; | ||
import org.eclipse.smarthome.core.thing.internal.profiles.i18n.ProfileI18nUtil; | ||
import org.eclipse.smarthome.core.thing.profiles.ProfileType; | ||
import org.eclipse.smarthome.core.thing.profiles.ProfileTypeBuilder; | ||
import org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID; | ||
import org.eclipse.smarthome.core.thing.profiles.StateProfileType; | ||
import org.eclipse.smarthome.core.thing.profiles.TriggerProfileType; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
/** | ||
* This OSGi service could be used to localize a {@link ProfileType} using the i18n mechanism of the openHAB framework. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@Component(service = ProfileTypeI18nLocalizationService.class) | ||
@NonNullByDefault | ||
public class ProfileTypeI18nLocalizationService { | ||
|
||
private final ProfileI18nUtil profileI18nUtil; | ||
|
||
@Activate | ||
public ProfileTypeI18nLocalizationService(final @Reference TranslationProvider i18nProvider) { | ||
this.profileI18nUtil = new ProfileI18nUtil(i18nProvider); | ||
} | ||
|
||
public ProfileType createLocalizedProfileType(Bundle bundle, ProfileType profileType, @Nullable Locale locale) { | ||
ProfileTypeUID profileTypeUID = profileType.getUID(); | ||
String defaultLabel = profileType.getLabel(); | ||
String label = profileI18nUtil.getProfileLabel(bundle, profileTypeUID, defaultLabel, locale); | ||
|
||
if (profileType instanceof StateProfileType) { | ||
return ProfileTypeBuilder.newTrigger(profileTypeUID, label != null ? label : defaultLabel) | ||
.withSupportedItemTypes(profileType.getSupportedItemTypes()) | ||
.withSupportedItemTypesOfChannel(((StateProfileType) profileType).getSupportedItemTypesOfChannel()) | ||
.build(); | ||
} else if (profileType instanceof TriggerProfileType) { | ||
return ProfileTypeBuilder.newTrigger(profileTypeUID, label != null ? label : defaultLabel) | ||
.withSupportedItemTypes(profileType.getSupportedItemTypes()) | ||
.withSupportedChannelTypeUIDs(((TriggerProfileType) profileType).getSupportedChannelTypeUIDs()) | ||
.build(); | ||
} else { | ||
return profileType; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
bundles/org.openhab.core.thing/src/main/resources/ESH-INF/i18n/SystemProfiles_de.properties
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,3 @@ | ||
profile-type.system.default.label = Standard | ||
profile-type.system.follow.label = Folgen | ||
profile-type.system.offset.label = Versatz |
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
Oops, something went wrong.