Added localization to String.format (#857)

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>
This commit is contained in:
Ahmed Sayed
2019-09-02 08:45:14 +02:00
committed by Stefan Behl
parent 9f76f6c371
commit a85dafaad5
4 changed files with 94 additions and 15 deletions

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.ui;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -53,7 +54,6 @@ public class UiProperties implements Serializable {
this.fixedTimeZone = fixedTimeZone; this.fixedTimeZone = fixedTimeZone;
} }
/** /**
* Localization information * Localization information
*/ */
@@ -63,26 +63,26 @@ public class UiProperties implements Serializable {
/** /**
* Default localization * Default localization
*/ */
private String defaultLocal = "en"; private Locale defaultLocal = Locale.ENGLISH;
/** /**
* List of available localizations * List of available localizations
*/ */
private List<String> availableLocals = Collections.singletonList("en"); private List<Locale> availableLocals = Collections.singletonList(Locale.ENGLISH);
public String getDefaultLocal() { public Locale getDefaultLocal() {
return defaultLocal; return defaultLocal;
} }
public List<String> getAvailableLocals() { public List<Locale> getAvailableLocals() {
return availableLocals; return availableLocals;
} }
public void setDefaultLocal(final String defaultLocal) { public void setDefaultLocal(final Locale defaultLocal) {
this.defaultLocal = defaultLocal; this.defaultLocal = defaultLocal;
} }
public void setAvailableLocals(final List<String> availableLocals) { public void setAvailableLocals(final List<Locale> availableLocals) {
this.availableLocals = availableLocals; this.availableLocals = availableLocals;
} }
} }

View File

@@ -8,7 +8,8 @@
*/ */
package org.eclipse.hawkbit.ui.rollout; package org.eclipse.hawkbit.ui.rollout;
import java.util.Locale; import static org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil.getCurrentLocale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -154,9 +155,7 @@ public final class DistributionBarHelper {
final double minTotalSize = MINIMUM_PART_SIZE * noOfParts; final double minTotalSize = MINIMUM_PART_SIZE * noOfParts;
final double availableSize = PARENT_SIZE_IN_PCT - minTotalSize; final double availableSize = PARENT_SIZE_IN_PCT - minTotalSize;
final double val = MINIMUM_PART_SIZE + (double) value / totalValue * availableSize; final double val = MINIMUM_PART_SIZE + (double) value / totalValue * availableSize;
// necessary due the format must contain a dot and other locals might return String.format(getCurrentLocale(), "%.3f", val) + "%";
// use a comma
return String.format(Locale.ENGLISH, "%.3f", val) + "%";
} }
private static String getPart(final int partIndex, final Status status, final Long value, final Long totalValue, private static String getPart(final int partIndex, final Status status, final Long value, final Long totalValue,

View File

@@ -478,7 +478,7 @@ public final class HawkbitCommonUtil {
default: default:
break; break;
} }
return String.format("%.1f", tmpFinishedPercentage); return String.format(getCurrentLocale(), "%.1f", tmpFinishedPercentage);
} }
/** /**
@@ -574,14 +574,14 @@ public final class HawkbitCommonUtil {
*/ */
public static Locale getLocaleToBeUsed(final UiProperties.Localization localizationProperties, public static Locale getLocaleToBeUsed(final UiProperties.Localization localizationProperties,
final Locale desiredLocale) { final Locale desiredLocale) {
final List<String> availableLocals = localizationProperties.getAvailableLocals(); final List<Locale> availableLocals = localizationProperties.getAvailableLocals();
// ckeck if language code of UI locale matches an available local. // ckeck if language code of UI locale matches an available local.
// Country, region and variant are ignored. "availableLocals" must only // Country, region and variant are ignored. "availableLocals" must only
// contain language codes without country or other extensions. // contain language codes without country or other extensions.
if (availableLocals.contains(desiredLocale.getLanguage())) { if (availableLocals.contains(desiredLocale)) {
return desiredLocale; return desiredLocale;
} }
return new Locale(localizationProperties.getDefaultLocal()); return localizationProperties.getDefaultLocal();
} }
/** /**

View File

@@ -0,0 +1,80 @@
/**
* Copyright (c) 2019 Bosch Software Innovations GmbH and others.
*
* 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.hawkbit.ui.utils;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import java.util.Arrays;
import java.util.Locale;
import org.eclipse.hawkbit.ui.UiProperties;
import org.junit.Test;
import org.mockito.Mockito;
import com.vaadin.ui.UI;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
@Feature("Unit Tests - Localization helper")
@Story("Test the locale configuration and prioritization")
public class HawkbitCommonUtilTest {
@Test
@Description("getCurrentLocale should return the set Locale in the UI if found, otherwise the default System Locale")
public void getCurrentLocaleShouldReturnSetUILocaleOrDefaultSystemLocale() {
final UI ui = Mockito.mock(UI.class);
// GIVEN
UI.setCurrent(null);
// WHEN
final Locale currentLocale = HawkbitCommonUtil.getCurrentLocale();
// THEN
assertEquals(Locale.getDefault(), currentLocale);
// GIVEN
UI.setCurrent(ui);
doReturn(Locale.GERMAN).when(ui).getLocale();
// WHEN
final Locale currentLocale2 = HawkbitCommonUtil.getCurrentLocale();
// THEN
assertEquals(Locale.GERMAN, currentLocale2);
}
@Test
@Description("If a default locale is set in the environment, then it should take perceedence over requested browser locale")
public void getLocaleToBeUsedShouldReturnDefaultLocalIfSet() {
final UiProperties.Localization localizationProperties = Mockito.mock(UiProperties.Localization.class);
// GIVEN
doReturn(Locale.GERMAN).when(localizationProperties).getDefaultLocal();
// WHEN
final Locale localeToBeUsed = HawkbitCommonUtil.getLocaleToBeUsed(localizationProperties, Locale.CHINESE);
// THEN
assertEquals(Locale.GERMAN, localeToBeUsed);
}
@Test
@Description("If no default locale is set in the environment, then the requested browser locale may be used if supported")
public void getLocaleToBeUsedShouldReturnRequestedLocalIfSupportedAndNoDefaultIsSet() {
final UiProperties.Localization localizationProperties = Mockito.mock(UiProperties.Localization.class);
// GIVEN
doReturn(null).when(localizationProperties).getDefaultLocal();
doReturn(Arrays.asList(Locale.ENGLISH, Locale.GERMAN)).when(localizationProperties).getAvailableLocals();
// WHEN
final Locale localeToBeUsed = HawkbitCommonUtil.getLocaleToBeUsed(localizationProperties, Locale.GERMAN);
// THEN
assertEquals(Locale.GERMAN, localeToBeUsed);
}
}