Localization fix

This commit is contained in:
Bane Stojkovic
2026-10-17 15:45:39 +02:00
parent bb9f50e07a
commit 8ec6900597
3 changed files with 93 additions and 4 deletions
@@ -2,16 +2,26 @@ package org.diylc.netlist;
import org.diylc.common.ComponentType;
import org.diylc.core.IDIYComponent;
import org.diylc.core.measures.AbstractMeasure;
import org.diylc.presenter.ComponentProcessor;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import static org.diylc.netlist.AbstractNetlistAnalyzer.find;
public class SpiceSumarizer {
// measures format with the user's locale for display, but SPICE only accepts '.' as the decimal
// separator
private static final Format VALUE_FORMAT =
new DecimalFormat("0.####", DecimalFormatSymbols.getInstance(Locale.ROOT));
public static String summarize(Netlist netlist, boolean useHTML) throws TreeException {
// grab all components that are in the netlist
@@ -119,14 +129,23 @@ public class SpiceSumarizer {
sb.append(typeName);
}
} else {
if (c.getValue().toString().contains(" ")) {
sb.append("\"").append(c.getValue()).append("\"");
String value = formatValue(c.getValue());
if (value.contains(" ")) {
sb.append("\"").append(value).append("\"");
} else {
sb.append(c.getValue());
sb.append(value);
}
}
}
private static String formatValue(Object value) {
if (value instanceof AbstractMeasure) {
AbstractMeasure<?> measure = (AbstractMeasure<?>) value;
return VALUE_FORMAT.format(measure.getValue()) + measure.getUnit();
}
return value.toString();
}
private static String formatSpiceNode(int i) {
return String.format("N%03d" , i);
}
@@ -20,8 +20,10 @@ package org.diylc.components.passive;
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
@@ -37,7 +39,10 @@ public class CapacitorDatasheetService {
private static CapacitorDatasheetService instance;
private static NumberFormat format = new DecimalFormat("##.###");
// lookup keys are matched against the datasheet files, which always use '.' as the decimal
// separator
private static NumberFormat format =
new DecimalFormat("##.###", DecimalFormatSymbols.getInstance(Locale.ROOT));
private Map<String, CapacitorDatasheet> cache = new HashMap<String, CapacitorDatasheet>();
@@ -0,0 +1,65 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2025 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.netlist;
import java.util.Arrays;
import java.util.Locale;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.diylc.components.passive.Resistor;
import org.diylc.core.IDIYComponent;
import org.diylc.core.measures.Resistance;
import org.diylc.core.measures.ResistanceUnit;
public class SpiceSumarizerTests {
private Locale defaultLocale;
@Before
public void setUp() {
defaultLocale = Locale.getDefault();
}
@After
public void tearDown() {
Locale.setDefault(defaultLocale);
}
@Test
public void testDecimalValueIgnoresLocale() throws TreeException {
Locale.setDefault(Locale.GERMANY);
Resistor resistor = new Resistor();
resistor.setName("R1");
resistor.setValue(new Resistance(4.7d, ResistanceUnit.K));
Netlist netlist = new Netlist(Arrays.<IDIYComponent<?>>asList(resistor));
netlist.add(new Group(new Node(resistor, 0), new Node(resistor, 1)));
String spice = SpiceSumarizer.summarize(netlist, false);
Assert.assertTrue(spice, spice.contains(" 4.7K "));
}
}