Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use browser locale for floating point numbers formatted String.format() in UI #857

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down Expand Up @@ -53,7 +54,6 @@ public void setFixedTimeZone(final String fixedTimeZone) {
this.fixedTimeZone = fixedTimeZone;
}


/**
* Localization information
*/
Expand All @@ -63,26 +63,26 @@ public static class Localization implements Serializable {
/**
* Default localization
*/
private String defaultLocal = "en";
private Locale defaultLocal = Locale.ENGLISH;

/**
* 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;
}

public List<String> getAvailableLocals() {
public List<Locale> getAvailableLocals() {
return availableLocals;
}

public void setDefaultLocal(final String defaultLocal) {
public void setDefaultLocal(final Locale defaultLocal) {
this.defaultLocal = defaultLocal;
}

public void setAvailableLocals(final List<String> availableLocals) {
public void setAvailableLocals(final List<Locale> availableLocals) {
this.availableLocals = availableLocals;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
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.Entry;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -154,9 +155,7 @@ private static String getPartWidth(final Long value, final Long totalValue, fina
final double minTotalSize = MINIMUM_PART_SIZE * noOfParts;
final double availableSize = PARENT_SIZE_IN_PCT - minTotalSize;
final double val = MINIMUM_PART_SIZE + (double) value / totalValue * availableSize;
// necessary due the format must contain a dot and other locals might
// use a comma
return String.format(Locale.ENGLISH, "%.3f", val) + "%";
return String.format(getCurrentLocale(), "%.3f", val) + "%";
}

private static String getPart(final int partIndex, final Status status, final Long value, final Long totalValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public static String formattingFinishedPercentage(final RolloutGroup rolloutGrou
default:
break;
}
return String.format("%.1f", tmpFinishedPercentage);
return String.format(getCurrentLocale(), "%.1f", tmpFinishedPercentage);
}

/**
Expand Down Expand Up @@ -574,14 +574,14 @@ public static String getArtifactoryDetailsLabelId(final String name, final Vaadi
*/
public static Locale getLocaleToBeUsed(final UiProperties.Localization localizationProperties,
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.
// Country, region and variant are ignored. "availableLocals" must only
// contain language codes without country or other extensions.
if (availableLocals.contains(desiredLocale.getLanguage())) {
if (availableLocals.contains(desiredLocale)) {
return desiredLocale;
}
return new Locale(localizationProperties.getDefaultLocal());
return localizationProperties.getDefaultLocal();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
a-sayyed marked this conversation as resolved.
Show resolved Hide resolved

@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);
}

}