Translate from translated languages to MATH.
Fixes b/6116353 Calculator doesn't calculate sin(0) when language is set to Spanish
Change-Id: I40ecf66e8021e914eaee9c603191d022d0b757ff
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 958a6c4..e5038a9 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -59,11 +59,16 @@
<string name="enter">\u2193</string>
<!-- Displayed on buttons on screen for the sin function. -->
<string name="sin">sin</string>
+ <!-- Do not translate. What we replace translated SIN with for calculating sin.-->
+ <string translate="false" name="sin_mathematical_value">sin</string>
<!-- Displayed on buttons on screen for the cos function. -->
<string name="cos">cos</string>
+ <!-- Do not translate. What we replace translated COS with for calculating cos. -->
+ <string translate="false" name="cos_mathematical_value">cos</string>
<!-- Displayed on buttons on screen for the tan function. -->
<string name="tan">tan</string>
-
+ <!-- Do not translate. What we replace translated TAN with for calculating tan. -->
+ <string translate="false" name="tan_mathematical_value">tan</string>
<!-- Do not translate. Unicode pi sign; don't translate. Displayed as button on screen. -->
<string name="pi">\u03c0</string>
<!-- Displayed on buttons on screen for the e function. -->
diff --git a/src/com/android/calculator2/Logic.java b/src/com/android/calculator2/Logic.java
index 690e892..e2657fe 100644
--- a/src/com/android/calculator2/Logic.java
+++ b/src/com/android/calculator2/Logic.java
@@ -22,12 +22,15 @@
import android.view.KeyEvent;
import android.widget.EditText;
import android.content.Context;
+import android.content.res.Resources;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map.Entry;
+import java.util.Set;
import org.javia.arity.Symbols;
import org.javia.arity.SyntaxException;
-import org.javia.arity.Util;
class Logic {
private CalculatorDisplay mDisplay;
@@ -60,9 +63,12 @@
}
private Listener mListener;
+ private Context mContext;
+ private Set<Entry<String, String>> mTranslationsSet;
Logic(Context context, History history, CalculatorDisplay display) {
- mErrorString = context.getResources().getString(R.string.error);
+ mContext = context;
+ mErrorString = mContext.getResources().getString(R.string.error);
mHistory = history;
mDisplay = display;
mDisplay.setLogic(this);
@@ -230,7 +236,8 @@
input = input.substring(0, size - 1);
--size;
}
-
+ // Find and replace any translated mathematical functions.
+ input = replaceTranslations(input);
double value = mSymbols.eval(input);
String result = "";
@@ -243,6 +250,29 @@
return result.replace('-', MINUS).replace(INFINITY, INFINITY_UNICODE);
}
+ private void addTranslation(HashMap<String, String> map, int t, int m) {
+ Resources res = mContext.getResources();
+ String translated = res.getString(t);
+ String math = res.getString(m);
+ if (!TextUtils.equals(translated, math)) {
+ map.put(translated, math);
+ }
+ }
+
+ private String replaceTranslations(String input) {
+ if (mTranslationsSet == null) {
+ HashMap<String, String> map = new HashMap<String, String>();
+ addTranslation(map, R.string.sin, R.string.sin_mathematical_value);
+ addTranslation(map, R.string.cos, R.string.cos_mathematical_value);
+ addTranslation(map, R.string.tan, R.string.tan_mathematical_value);
+ mTranslationsSet = map.entrySet();
+ }
+ for (Entry<String, String> entry : mTranslationsSet) {
+ input = input.replace(entry.getKey(), entry.getValue());
+ }
+ return input;
+ }
+
private String tryFormattingWithPrecision(double value, int precision) {
// The standard scientific formatter is basically what we need. We will
// start with what it produces and then massage it a bit.