From f0317ba3829297b7a560acc09a7d5337fb7f385b Mon Sep 17 00:00:00 2001 From: Marcin Chwedczuk <0xmarcin+gh@gmail.com> Date: Sat, 16 Nov 2024 19:13:20 +0100 Subject: [PATCH] fix localization --- engine/src/main/java/module-info.java | 1 + .../main/java/mscalc/engine/CCalcEngine.java | 22 +++++++++- .../resource/JavaBundleResourceProvider.java | 41 +++++++++++++++++++ .../engine/resource/ResourceProvider.java | 5 +++ .../src/test/java/mscalc/CCalcEngineTest.java | 11 +++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 engine/src/main/java/mscalc/engine/resource/JavaBundleResourceProvider.java create mode 100644 engine/src/main/java/mscalc/engine/resource/ResourceProvider.java create mode 100644 engine/src/test/java/mscalc/CCalcEngineTest.java diff --git a/engine/src/main/java/module-info.java b/engine/src/main/java/module-info.java index 5665c2c..b134bda 100644 --- a/engine/src/main/java/module-info.java +++ b/engine/src/main/java/module-info.java @@ -3,6 +3,7 @@ exports mscalc.engine.ratpack; exports mscalc.engine; exports mscalc.engine.commands; + exports mscalc.engine.resource; requires java.xml; } \ No newline at end of file diff --git a/engine/src/main/java/mscalc/engine/CCalcEngine.java b/engine/src/main/java/mscalc/engine/CCalcEngine.java index 1a88476..d9169e5 100644 --- a/engine/src/main/java/mscalc/engine/CCalcEngine.java +++ b/engine/src/main/java/mscalc/engine/CCalcEngine.java @@ -2,6 +2,8 @@ import mscalc.engine.ratpack.RatPack; import mscalc.engine.ratpack.RatPack.AngleType; +import mscalc.engine.resource.JavaBundleResourceProvider; +import mscalc.engine.resource.ResourceProvider; import java.util.Collections; import java.util.HashMap; @@ -12,6 +14,13 @@ import static mscalc.engine.EngineStrings.*; public class CCalcEngine { + private static final Map engineStrings = new HashMap<>(); + + // TODO: Remove + static { + loadEngineStrings(new JavaBundleResourceProvider()); + } + // Unary operator Function Name table Element // since unary operators button names aren't exactly friendly for history purpose, @@ -87,7 +96,18 @@ public boolean hasAngleStrings() { entry(IDC_MOD, new FunctionNameElement(SIDS_MOD, "", "", "", "", "", SIDS_PROGRAMMER_MOD)) ); - private static final Map engineStrings = new HashMap<>(); + static void loadEngineStrings(ResourceProvider resourceProvider) + { + for (var sid : g_sids) + { + var locString = resourceProvider.getCEngineString(sid); + if (!locString.isEmpty()) + { + System.out.printf("Loaded: %s -> %s%n", sid, locString); + engineStrings.put(sid, locString); + } + } + } // returns the ptr to string representing the operator. Mostly same as the button, but few special cases for x^y etc. private static String GetString(int ids) diff --git a/engine/src/main/java/mscalc/engine/resource/JavaBundleResourceProvider.java b/engine/src/main/java/mscalc/engine/resource/JavaBundleResourceProvider.java new file mode 100644 index 0000000..378f267 --- /dev/null +++ b/engine/src/main/java/mscalc/engine/resource/JavaBundleResourceProvider.java @@ -0,0 +1,41 @@ +package mscalc.engine.resource; + +import java.text.DecimalFormatSymbols; +import java.util.Locale; +import java.util.ResourceBundle; + +public class JavaBundleResourceProvider implements ResourceProvider { + private final ResourceBundle bundle; + private final DecimalFormatSymbols symbols; + + public JavaBundleResourceProvider() { + var locale = Locale.US; + + this.bundle = ResourceBundle.getBundle("mscalc.engine.calc-engine", locale); + this.symbols = new DecimalFormatSymbols(locale); + } + + @Override + public String getCEngineString(String id) { + if (id.equals("sDecimal")) { + return Character.toString(symbols.getDecimalSeparator()); + } + + if (id.equals("sThousand")) { + return Character.toString(symbols.getGroupingSeparator()); + } + + if (id.equals("sGrouping")) + { + // The following groupings are the onces that CalcEngine supports. + // 0;0 0x000 - no grouping + // 3;0 0x003 - group every 3 digits + // 3;2;0 0x023 - group 1st 3 and then every 2 digits + // 4;0 0x004 - group every 4 digits + // 5;3;2;0 0x235 - group 5, then 3, then every 2 + return "3;0"; // Currently hardcoded, TODO: Fix it, check LocalizationSettings in original Calc + } + + return bundle.containsKey(id) ? bundle.getString(id) : ""; + } +} diff --git a/engine/src/main/java/mscalc/engine/resource/ResourceProvider.java b/engine/src/main/java/mscalc/engine/resource/ResourceProvider.java new file mode 100644 index 0000000..2ab4e89 --- /dev/null +++ b/engine/src/main/java/mscalc/engine/resource/ResourceProvider.java @@ -0,0 +1,5 @@ +package mscalc.engine.resource; + +public interface ResourceProvider { + String getCEngineString(String id); +} diff --git a/engine/src/test/java/mscalc/CCalcEngineTest.java b/engine/src/test/java/mscalc/CCalcEngineTest.java new file mode 100644 index 0000000..7da5902 --- /dev/null +++ b/engine/src/test/java/mscalc/CCalcEngineTest.java @@ -0,0 +1,11 @@ +package mscalc; + +import mscalc.engine.CCalcEngine; +import org.junit.jupiter.api.Test; + +public class CCalcEngineTest { + @Test + public void foo() { + CCalcEngine.OpCodeToString(100); + } +}