Skip to content

Commit

Permalink
fix localization
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 16, 2024
1 parent c0c5744 commit f0317ba
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions engine/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports mscalc.engine.ratpack;
exports mscalc.engine;
exports mscalc.engine.commands;
exports mscalc.engine.resource;

requires java.xml;
}
22 changes: 21 additions & 1 deletion engine/src/main/java/mscalc/engine/CCalcEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +14,13 @@
import static mscalc.engine.EngineStrings.*;

public class CCalcEngine {
private static final Map<String, String> 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,
Expand Down Expand Up @@ -87,7 +96,18 @@ public boolean hasAngleStrings() {
entry(IDC_MOD, new FunctionNameElement(SIDS_MOD, "", "", "", "", "", SIDS_PROGRAMMER_MOD))
);

private static final Map<String, String> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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) : "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mscalc.engine.resource;

public interface ResourceProvider {
String getCEngineString(String id);
}
11 changes: 11 additions & 0 deletions engine/src/test/java/mscalc/CCalcEngineTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f0317ba

Please sign in to comment.