Skip to content

Commit

Permalink
Fix persistence of Locale for i18n and use Format Category for number…
Browse files Browse the repository at this point in the history
… and date
  • Loading branch information
nroduit committed Sep 19, 2023
1 parent 41394ac commit 8da61e3
Show file tree
Hide file tree
Showing 45 changed files with 269 additions and 845 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -48,7 +50,6 @@
import org.weasis.core.api.media.data.TagW;
import org.weasis.core.api.media.data.TagW.TagType;
import org.weasis.core.api.util.FontItem;
import org.weasis.core.api.util.LocalUtil;
import org.weasis.core.api.util.ResourceUtil;
import org.weasis.core.ui.util.CalendarUtil;
import org.weasis.core.ui.util.TableColumnAdjuster;
Expand Down Expand Up @@ -282,11 +283,13 @@ private DateTableEditor buildDatePicker() {

CalendarUtil.adaptCalendarColors(settings);

settings.setFormatForDatesCommonEra(LocalUtil.getDateFormatter());
settings.setFormatForDatesBeforeCommonEra(LocalUtil.getDateFormatter());
settings.setFormatForDatesCommonEra(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
settings.setFormatForDatesBeforeCommonEra(
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));

settings.setFormatForDatesCommonEra(LocalUtil.getDateFormatter());
settings.setFormatForDatesBeforeCommonEra(LocalUtil.getDateFormatter());
settings.setFormatForDatesCommonEra(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
settings.setFormatForDatesBeforeCommonEra(
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
return d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
package org.weasis.acquire.explorer.core.bean;

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
import java.util.Locale;
import org.dcm4che3.data.Tag;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -84,9 +84,8 @@ static void setUp() {
taggable = Mockito.mock(Taggable.class);

Mockito.mockStatic(LocalUtil.class);
Mockito.when(LocalUtil.getDateFormatter()).thenReturn(dateformat);
Mockito.when(LocalUtil.getDateTimeFormatter()).thenReturn(dateformat);
Mockito.when(LocalUtil.getLocaleFormat()).thenReturn(Locale.ENGLISH);
Mockito.when(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)).thenReturn(dateformat);
Mockito.when(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)).thenReturn(dateformat);

Mockito.mockStatic(TagD.class);
Arrays.stream(GlobalTag.values()).forEach(GlobalTag::prepareMock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.weasis.core.api.util.LocalUtil;

class SeriesGroupTest {

Expand All @@ -34,7 +35,8 @@ static void setUp() {
@Test
void testToString() {
assertThat(s1.toString()).hasToString("Other"); // NON-NLS
assertThat(s2.toString()).hasToString(LocalUtil.getDateTimeFormatter().format(today));
assertThat(s2.toString())
.hasToString(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(today));
assertThat(s3.toString()).hasToString(TEST_SERIES);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.weasis.core.api.media.data.MediaSeries;
import org.weasis.core.api.media.data.TagUtil;
import org.weasis.core.api.media.data.TagW;
import org.weasis.core.api.util.LocalUtil;
import org.weasis.core.ui.editor.SeriesViewerFactory;
import org.weasis.core.ui.editor.ViewerPluginBuilder;
import org.weasis.core.ui.util.DefaultAction;
Expand All @@ -69,7 +68,7 @@ public abstract class AbstractThumbnailList<E extends MediaElement> extends JLis
public static final String SECTION_CHANGED = "SECTION_CHANGED"; // NON-NLS
public static final String DIRECTORY_SIZE = "DIRECTORY_SIZE"; // NON-NLS

private static final NumberFormat intGroupFormat = LocalUtil.getIntegerInstance();
private static final NumberFormat intGroupFormat = NumberFormat.getIntegerInstance();

static {
intGroupFormat.setGroupingUsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.text.NumberFormat;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import org.weasis.core.api.gui.util.GuiUtils;
import org.weasis.core.api.util.FontItem;
import org.weasis.core.api.util.LocalUtil;

public class CircularProgressBar extends JProgressBar {

Expand Down Expand Up @@ -122,7 +122,7 @@ private void draw(Graphics2D g2d) {
}

double angle = 360 - this.getValue() * 360.0 / range;
String text = LocalUtil.getPercentInstance().format((double) this.getValue() / range);
String text = NumberFormat.getPercentInstance().format((double) this.getValue() / range);
float textGap = GuiUtils.getScaleLength(TEXT_GAP);
float arcSize = getInsets().top - textGap;
double shift = arcSize / 2.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
import java.text.NumberFormat;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import org.weasis.core.api.util.LocalUtil;

public class DecFormatter {

private DecFormatter() {}

private static final NumberFormat df1 = LocalUtil.getNumberInstance(); // 1 decimals
private static final NumberFormat df2 = LocalUtil.getNumberInstance(); // 2 decimals
private static final NumberFormat df4 = LocalUtil.getNumberInstance(); // 4 decimals
private static final NumberFormat percent2 = LocalUtil.getPercentInstance();
private static final DecimalFormat decimalAndNumber =
new DecimalFormat("#,##0.#", LocalUtil.getDecimalFormatSymbols());
private static final NumberFormat df1 = NumberFormat.getNumberInstance(); // 1 decimals
private static final NumberFormat df2 = NumberFormat.getNumberInstance(); // 2 decimals
private static final NumberFormat df4 = NumberFormat.getNumberInstance(); // 4 decimals
private static final NumberFormat percent2 = NumberFormat.getPercentInstance();
private static final DecimalFormat decimalAndNumber = new DecimalFormat("#,##0.#");
// Scientific format with 4 decimals
private static final DecimalFormat dfSci =
new DecimalFormat("0.####E0", LocalUtil.getDecimalFormatSymbols()); // NON-NLS
private static final DecimalFormat dfSci = new DecimalFormat("0.####E0"); // NON-NLS

static {
df1.setMaximumFractionDigits(1);
Expand Down Expand Up @@ -61,12 +58,9 @@ public static String scientificFormat(Number val) {
}

public static DefaultFormatterFactory setPreciseDoubleFormat(double min, double max) {
NumberFormatter displayFormatter =
new NumberFormatter(new DecimalFormat("#,##0.##", LocalUtil.getDecimalFormatSymbols()));
NumberFormatter displayFormatter = new NumberFormatter(new DecimalFormat("#,##0.##"));
displayFormatter.setValueClass(Double.class);
NumberFormatter editFormatter =
new NumberFormatter(
new DecimalFormat("#,##0.0#############", LocalUtil.getDecimalFormatSymbols()));
NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#,##0.0#############"));
editFormatter.setValueClass(Double.class);
editFormatter.setMinimum(min);
editFormatter.setMaximum(max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -25,7 +27,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weasis.core.api.media.data.TagW.TagType;
import org.weasis.core.api.util.LocalUtil;

public final class TagUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(TagUtil.class);
Expand Down Expand Up @@ -119,13 +120,14 @@ public static Date dateTime(Date date, Date time) {

public static String formatDateTime(TemporalAccessor date) {
if (date instanceof LocalDate) {
return LocalUtil.getDateFormatter().format(date);
return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(date);
} else if (date instanceof LocalTime) {
return LocalUtil.getTimeFormatter().format(date);
return DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM).format(date);
} else if (date instanceof LocalDateTime || date instanceof ZonedDateTime) {
return LocalUtil.getDateTimeFormatter().format(date);
return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(date);
} else if (date instanceof Instant instant) {
return LocalUtil.getDateTimeFormatter().format(instant.atZone(ZoneId.systemDefault()));
return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.format(instant.atZone(ZoneId.systemDefault()));
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weasis.core.Messages;
import org.weasis.core.api.util.LocalUtil;
import org.weasis.core.util.StringUtil;

/**
Expand Down Expand Up @@ -495,9 +494,7 @@ protected static String formatValue(String value, boolean decimal, String format
if (pattern != null) {
fmLength += pattern.length() + 2;
try {
str =
new DecimalFormat(pattern, LocalUtil.getDecimalFormatSymbols())
.format(Double.parseDouble(str));
str = new DecimalFormat(pattern).format(Double.parseDouble(str));
} catch (NumberFormatException e) {
LOGGER.warn("Cannot apply pattern to decimal value", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Locale.Category;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
Expand Down Expand Up @@ -121,12 +122,7 @@ public boolean willGainFocus(CDockable dockable) {
}
}
String code = systemPreferences.getProperty(UICore.P_FORMAT_CODE);
if (StringUtil.hasLength(code)) {
Locale l = LocalUtil.textToLocale(code);
if (!l.equals(Locale.getDefault())) {
LocalUtil.setLocaleFormat(l);
}
}
Locale.setDefault(Category.FORMAT, LocalUtil.textToLocale(code));

String path = systemPreferences.getProperty("weasis.resources.path");
ResourceUtil.setResourcePath(path);
Expand Down
110 changes: 16 additions & 94 deletions weasis-core/src/main/java/org/weasis/core/api/util/LocalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,118 +9,40 @@
*/
package org.weasis.core.api.util;

import java.text.DateFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import org.weasis.core.util.StringUtil;

public class LocalUtil {

private static final DateTimeFormatter defaultDateFormatter =
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
private static final DateTimeFormatter defaultTimeFormatter =
DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
private static final DateTimeFormatter defaultDateTimeFormatter =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

private static Locale localeFormat = null;

private LocalUtil() {}

/**
* Returns the IETF BCP 47 language tag string according the <code>Locale</code> value.
*
* @return the IETF BCP 47 language tag string
*/
public static String localeToText(Locale value) {
if (value == null) {
return "en"; // NON-NLS
}
return value.toString();
return value.toLanguageTag();
}

/**
* Returns the <code>Locale</code> value according the IETF BCP 47 language tag or the suffix of
* the i18n jars. Null or empty string will return the ENGLISH <code>Locale</code>. The value
* "system " returns the system default <code>Locale</code>.
*
* @return the <code>Locale</code> value
*/
public static Locale textToLocale(String value) {
if (!StringUtil.hasText(value)) {
return Locale.ENGLISH;
}

if ("system".equals(value)) { // NON-NLS
return getSystemLocale();
}

String[] val = value.split("_", 3);
String language = val.length > 0 ? val[0] : "";
String country = val.length > 1 ? val[1] : "";
String variant = val.length > 2 ? val[2] : "";
return new Locale.Builder()
.setLanguage(language)
.setRegion(country)
.setVariant(variant)
.build();
}

public static Locale getSystemLocale() {
String language = System.getProperty("user.language", "en"); // NON-NLS
String country = System.getProperty("user.country", ""); // NON-NLS
String variant = System.getProperty("user.variant", ""); // NON-NLS
return new Locale.Builder()
.setLanguage(language)
.setRegion(country)
.setVariant(variant)
.build();
}

public static synchronized Locale getLocaleFormat() {
Locale l = LocalUtil.localeFormat;
if (l == null) {
l = Locale.getDefault();
if (!"system".equals(value)) { // NON-NLS
return Locale.forLanguageTag(value.replace("_", "-"));
}
return l;
}

public static synchronized void setLocaleFormat(Locale value) {
LocalUtil.localeFormat = value;
}

public static DecimalFormatSymbols getDecimalFormatSymbols() {
return DecimalFormatSymbols.getInstance(getLocaleFormat());
}

public static NumberFormat getNumberInstance() {
return NumberFormat.getNumberInstance(getLocaleFormat());
}

public static NumberFormat getIntegerInstance() {
return NumberFormat.getIntegerInstance(getLocaleFormat());
}

public static NumberFormat getPercentInstance() {
return NumberFormat.getPercentInstance(getLocaleFormat());
}

public static DateFormat getDateInstance(int style) {
return DateFormat.getDateInstance(style, getLocaleFormat());
}

public static DateTimeFormatter getDateFormatter() {
return defaultDateFormatter.withLocale(getLocaleFormat());
}

public static DateTimeFormatter getDateFormatter(FormatStyle style) {
return DateTimeFormatter.ofLocalizedDate(style).withLocale(getLocaleFormat());
}

public static DateTimeFormatter getTimeFormatter() {
return defaultTimeFormatter.withLocale(getLocaleFormat());
}

public static DateTimeFormatter getTimeFormatter(FormatStyle style) {
return DateTimeFormatter.ofLocalizedTime(style).withLocale(getLocaleFormat());
}

public static DateTimeFormatter getDateTimeFormatter() {
return defaultDateTimeFormatter.withLocale(getLocaleFormat());
}

public static DateTimeFormatter getDateTimeFormatter(FormatStyle style) {
return DateTimeFormatter.ofLocalizedDateTime(style).withLocale(getLocaleFormat());
return Locale.getDefault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.awt.geom.Point2D;
import java.util.List;
import java.util.Locale;
import java.util.Locale.Category;
import java.util.Objects;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
Expand All @@ -30,7 +32,6 @@
import org.weasis.core.api.image.util.Unit;
import org.weasis.core.api.media.data.ImageElement;
import org.weasis.core.api.media.data.MediaSeries;
import org.weasis.core.api.util.LocalUtil;
import org.weasis.core.ui.model.graphic.imp.line.LineGraphic;
import org.weasis.core.util.MathUtil;
import org.weasis.core.util.StringUtil;
Expand Down Expand Up @@ -73,7 +74,7 @@ void jbInit() {
setBorder(GuiUtils.getEmptyBorder(10, 15, 10, 15));

GuiUtils.setPreferredWidth(jTextFieldLineWidth, 170);
jTextFieldLineWidth.setLocale(LocalUtil.getLocaleFormat());
jTextFieldLineWidth.setLocale(Locale.getDefault(Category.FORMAT));
jTextFieldLineWidth.setFormatterFactory(
DecFormatter.setPreciseDoubleFormat(0.000005d, Double.MAX_VALUE));
jTextFieldLineWidth.setValue(1.0);
Expand Down
Loading

0 comments on commit 8da61e3

Please sign in to comment.