Skip to content

Commit

Permalink
Change inferred i18n key for add-ons + alternative i18n key
Browse files Browse the repository at this point in the history
Fix openhab/openhab-webui#2641

The i18n key for add-on configuration parameter should now start with "addon.config.".
To maintain compatibility with thousands of existing translations, an alternative i18n key starting with "<type>.config" is accepted when the add-on type is either io, persistence, voice or ui.

Signed-off-by: Laurent Garnier <lg.hc@free.fr>

u#
  • Loading branch information
lolodomo committed Jul 15, 2024
1 parent 918b4fa commit 7bee99d
Showing 1 changed file with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.net.URI;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -31,37 +32,36 @@
* @author Dennis Nobel - Initial contribution
* @author Alex Tugarev - Extended for pattern and option label
* @author Thomas Höfer - Extended for unit label
* @author Laurent Garnier - Changed inferred key for add-ons + alternative key
*/
@NonNullByDefault
public class ConfigDescriptionI18nUtil {

private final TranslationProvider i18nProvider;

private static final Pattern DELIMITER = Pattern.compile("[:=\\s]");
private static final Set<String> ADDON_TYPES = Set.of("automation", "binding", "io", "misc", "persistence", "voice",
"ui");
private static final Set<String> ADDON_TYPES_WITH_ALTERNATIVE_KEY = Set.of("io", "persistence", "voice", "ui");

public ConfigDescriptionI18nUtil(TranslationProvider i18nProvider) {
this.i18nProvider = i18nProvider;
}

public @Nullable String getParameterPattern(Bundle bundle, URI configDescriptionURI, String parameterName,
@Nullable String defaultPattern, @Nullable Locale locale) {
String key = I18nUtil.stripConstantOr(defaultPattern,
() -> inferKey(configDescriptionURI, parameterName, "pattern"));
return i18nProvider.getText(bundle, key, defaultPattern, locale);
return getParameterValue(bundle, configDescriptionURI, parameterName, "pattern", defaultPattern, locale);
}

public @Nullable String getParameterDescription(Bundle bundle, URI configDescriptionURI, String parameterName,
@Nullable String defaultDescription, @Nullable Locale locale) {
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferKey(configDescriptionURI, parameterName, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
return getParameterValue(bundle, configDescriptionURI, parameterName, "description", defaultDescription,
locale);
}

public @Nullable String getParameterLabel(Bundle bundle, URI configDescriptionURI, String parameterName,
@Nullable String defaultLabel, @Nullable Locale locale) {
String key = I18nUtil.stripConstantOr(defaultLabel,
() -> inferKey(configDescriptionURI, parameterName, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
return getParameterValue(bundle, configDescriptionURI, parameterName, "label", defaultLabel, locale);
}

public @Nullable String getParameterOptionLabel(Bundle bundle, URI configDescriptionURI, String parameterName,
Expand All @@ -70,10 +70,8 @@ public ConfigDescriptionI18nUtil(TranslationProvider i18nProvider) {
return defaultOptionLabel;
}

String key = I18nUtil.stripConstantOr(defaultOptionLabel,
() -> inferKey(configDescriptionURI, parameterName, "option." + optionValue));

return i18nProvider.getText(bundle, key, defaultOptionLabel, locale);
return getParameterValue(bundle, configDescriptionURI, parameterName, "option." + optionValue,
defaultOptionLabel, locale);
}

public @Nullable String getParameterUnitLabel(Bundle bundle, URI configDescriptionURI, String parameterName,
Expand All @@ -84,14 +82,28 @@ public ConfigDescriptionI18nUtil(TranslationProvider i18nProvider) {
return label;
}
}
String key = I18nUtil.stripConstantOr(defaultUnitLabel,
() -> inferKey(configDescriptionURI, parameterName, "unitLabel"));
return i18nProvider.getText(bundle, key, defaultUnitLabel, locale);
return getParameterValue(bundle, configDescriptionURI, parameterName, "unitLabel", defaultUnitLabel, locale);
}

private @Nullable String getParameterValue(Bundle bundle, URI configDescriptionURI, String parameterName,
String parameterAttribute, @Nullable String defaultValue, @Nullable Locale locale) {
String key = I18nUtil.stripConstantOr(defaultValue,
() -> inferKey(true, configDescriptionURI, parameterName, parameterAttribute));
String value = i18nProvider.getText(bundle, key, null, locale);
if (value == null && ADDON_TYPES_WITH_ALTERNATIVE_KEY.contains(configDescriptionURI.getScheme())) {
key = I18nUtil.stripConstantOr(defaultValue,
() -> inferKey(false, configDescriptionURI, parameterName, parameterAttribute));
value = i18nProvider.getText(bundle, key, null, locale);
}
return value != null ? value : defaultValue;
}

private String inferKey(URI configDescriptionURI, String parameterName, String lastSegment) {
private String inferKey(boolean checkAddonType, URI configDescriptionURI, String parameterName,
String lastSegment) {
String prefix = checkAddonType && ADDON_TYPES.contains(configDescriptionURI.getScheme()) ? "addon"
: configDescriptionURI.getScheme();
String uri = configDescriptionURI.getSchemeSpecificPart().replace(":", ".");
return configDescriptionURI.getScheme() + ".config." + uri + "." + parameterName + "." + lastSegment;
return prefix + ".config." + uri + "." + parameterName + "." + lastSegment;
}

private boolean isValidPropertyKey(@Nullable String key) {
Expand Down

0 comments on commit 7bee99d

Please sign in to comment.