Merge "Replace the use of manually translated digits in Calculator."
diff --git a/res/layout/pad_numeric.xml b/res/layout/pad_numeric.xml
index c03f58b..127f495 100644
--- a/res/layout/pad_numeric.xml
+++ b/res/layout/pad_numeric.xml
@@ -15,7 +15,7 @@
   limitations under the License.
   -->
 
-<com.android.calculator2.CalculatorPadLayout
+<com.android.calculator2.CalculatorNumericPadLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/pad_numeric"
     style="@style/PadLayoutStyle.Numeric"
@@ -26,68 +26,57 @@
     <Button
         android:id="@+id/digit_7"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_7" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_8"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_8" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_9"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_9" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_4"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_4" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_5"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_5" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_6"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_6" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_1"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_1" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_2"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_2" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_3"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_3" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/dec_point"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/dec_point" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/digit_0"
         style="@style/PadButtonStyle.Numeric"
-        android:onClick="onButtonClick"
-        android:text="@string/digit_0" />
+        android:onClick="onButtonClick" />
 
     <Button
         android:id="@+id/eq"
@@ -96,4 +85,4 @@
         android:onClick="onButtonClick"
         android:text="@string/eq" />
 
-</com.android.calculator2.CalculatorPadLayout>
+</com.android.calculator2.CalculatorNumericPadLayout>
diff --git a/res/values-fa/config.xml b/res/values-fa/config.xml
new file mode 100644
index 0000000..519cece
--- /dev/null
+++ b/res/values-fa/config.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2015 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  -->
+
+<resources>
+    <!-- Whether to use localized digits in the numeric pad. -->
+    <bool name="use_localized_digits">true</bool>
+</resources>
diff --git a/res/values/config.xml b/res/values/config.xml
new file mode 100644
index 0000000..94bdfde
--- /dev/null
+++ b/res/values/config.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2015 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  -->
+
+<resources>
+    <!-- Whether to use localized digits in the numeric pad. -->
+    <bool name="use_localized_digits">false</bool>
+</resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index bcdd55e..11d4469 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -25,30 +25,6 @@
     <!-- Error displayed when expression contains a syntax error. [CHAR_LIMIT=14] -->
     <string name="error_syntax">Error</string>
 
-    <!-- Decimal separator (e.g. "1.23"). [CHAR_LIMIT=1] -->
-    <string name="dec_point">.</string>
-
-    <!-- Decimal representation of zero (e.g. "0", "10", "1.0", "1e0"). [CHAR_LIMIT=1] -->
-    <string name="digit_0">0</string>
-    <!-- Decimal representation of one (e.g. "1", "11", "1.1", "1e1"). [CHAR_LIMIT=1] -->
-    <string name="digit_1">1</string>
-    <!-- Decimal representation of two (e.g. "2", "22", "2.2", "2e2"). [CHAR_LIMIT=1] -->
-    <string name="digit_2">2</string>
-    <!-- Decimal representation of three (e.g. "3", "33", "3.3", "3e3"). [CHAR_LIMIT=1] -->
-    <string name="digit_3">3</string>
-    <!-- Decimal representation of four (e.g. "4", "44", "4.4", "4e4"). [CHAR_LIMIT=1] -->
-    <string name="digit_4">4</string>
-    <!-- Decimal representation of five (e.g. "5", "55", "5.5", "5e5"). [CHAR_LIMIT=1] -->
-    <string name="digit_5">5</string>
-    <!-- Decimal representation of six (e.g. "6", "66", "6.6", "6e6"). [CHAR_LIMIT=1] -->
-    <string name="digit_6">6</string>
-    <!-- Decimal representation of seven (e.g. "7", "77", "7.7", "7e7"). [CHAR_LIMIT=1] -->
-    <string name="digit_7">7</string>
-    <!-- Decimal representation of eight (e.g. "8", "88", "8.8", "8e8"). [CHAR_LIMIT=1] -->
-    <string name="digit_8">8</string>
-    <!-- Decimal representation of eight (e.g. "9", "99", "9.9", "9e9"). [CHAR_LIMIT=1] -->
-    <string name="digit_9">9</string>
-
     <!-- Abbrev. name of cosine function (e.g. "cos(π)". [CHAR_LIMIT=3] -->
     <string name="fun_cos">cos</string>
     <!-- Natural logarithm function (e.g. "ln(2)"). [CHAR_LIMIT=3] -->
diff --git a/src/com/android/calculator2/CalculatorExpressionTokenizer.java b/src/com/android/calculator2/CalculatorExpressionTokenizer.java
index b9c91e2..7145fa8 100644
--- a/src/com/android/calculator2/CalculatorExpressionTokenizer.java
+++ b/src/com/android/calculator2/CalculatorExpressionTokenizer.java
@@ -18,7 +18,11 @@
 
 import android.content.Context;
 
+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.Map.Entry;
 
@@ -29,18 +33,22 @@
     public CalculatorExpressionTokenizer(Context context) {
         mReplacementMap = new HashMap<>();
 
-        mReplacementMap.put(".", context.getString(R.string.dec_point));
+        Locale locale = context.getResources().getConfiguration().locale;
+        if (!context.getResources().getBoolean(R.bool.use_localized_digits)) {
+            locale = new Locale.Builder()
+                .setLocale(locale)
+                .setUnicodeLocaleKeyword("nu", "latn")
+                .build();
+        }
 
-        mReplacementMap.put("0", context.getString(R.string.digit_0));
-        mReplacementMap.put("1", context.getString(R.string.digit_1));
-        mReplacementMap.put("2", context.getString(R.string.digit_2));
-        mReplacementMap.put("3", context.getString(R.string.digit_3));
-        mReplacementMap.put("4", context.getString(R.string.digit_4));
-        mReplacementMap.put("5", context.getString(R.string.digit_5));
-        mReplacementMap.put("6", context.getString(R.string.digit_6));
-        mReplacementMap.put("7", context.getString(R.string.digit_7));
-        mReplacementMap.put("8", context.getString(R.string.digit_8));
-        mReplacementMap.put("9", context.getString(R.string.digit_9));
+        final DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
+        final char zeroDigit = symbols.getZeroDigit();
+
+        mReplacementMap.put(".", String.valueOf(symbols.getDecimalSeparator()));
+
+        for (int i = 0; i <= 9; ++i) {
+            mReplacementMap.put(Integer.toString(i), String.valueOf((char) (i + zeroDigit)));
+        }
 
         mReplacementMap.put("/", context.getString(R.string.op_div));
         mReplacementMap.put("*", context.getString(R.string.op_mul));
diff --git a/src/com/android/calculator2/CalculatorNumericPadLayout.java b/src/com/android/calculator2/CalculatorNumericPadLayout.java
new file mode 100644
index 0000000..ece0433
--- /dev/null
+++ b/src/com/android/calculator2/CalculatorNumericPadLayout.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.calculator2;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.widget.Button;
+import android.view.View;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.util.Locale;
+
+public class CalculatorNumericPadLayout extends CalculatorPadLayout {
+
+    public CalculatorNumericPadLayout(Context context) {
+        this(context, null);
+    }
+
+    public CalculatorNumericPadLayout(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public CalculatorNumericPadLayout(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    @Override
+    public void onFinishInflate() {
+        super.onFinishInflate();
+
+        Locale locale = getResources().getConfiguration().locale;
+        if (!getResources().getBoolean(R.bool.use_localized_digits)) {
+            locale = new Locale.Builder()
+                .setLocale(locale)
+                .setUnicodeLocaleKeyword("nu", "latn")
+                .build();
+        }
+
+        final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
+        final char zeroDigit = symbols.getZeroDigit();
+        for (int childIndex = getChildCount() - 1; childIndex >= 0; --childIndex) {
+            final View v = getChildAt(childIndex);
+            if (v instanceof Button) {
+                final Button b = (Button) v;
+                switch (b.getId()) {
+                    case R.id.digit_0:
+                        b.setText(String.valueOf(zeroDigit));
+                        break;
+                    case R.id.digit_1:
+                        b.setText(String.valueOf((char) (zeroDigit + 1)));
+                        break;
+                    case R.id.digit_2:
+                        b.setText(String.valueOf((char) (zeroDigit + 2)));
+                        break;
+                    case R.id.digit_3:
+                        b.setText(String.valueOf((char) (zeroDigit + 3)));
+                        break;
+                    case R.id.digit_4:
+                        b.setText(String.valueOf((char) (zeroDigit + 4)));
+                        break;
+                    case R.id.digit_5:
+                        b.setText(String.valueOf((char) (zeroDigit + 5)));
+                        break;
+                    case R.id.digit_6:
+                        b.setText(String.valueOf((char) (zeroDigit + 6)));
+                        break;
+                    case R.id.digit_7:
+                        b.setText(String.valueOf((char) (zeroDigit + 7)));
+                        break;
+                    case R.id.digit_8:
+                        b.setText(String.valueOf((char) (zeroDigit + 8)));
+                        break;
+                    case R.id.digit_9:
+                        b.setText(String.valueOf((char) (zeroDigit + 9)));
+                        break;
+                    case R.id.dec_point:
+                        b.setText(String.valueOf(symbols.getDecimalSeparator()));
+                        break;
+                }
+            }
+        }
+    }
+}
+