Skip to content

Commit b5b9869

Browse files
Implemented localization of library messages (#3).
1 parent 735509c commit b5b9869

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ android {
2222
dependencies {
2323
compile fileTree(include: ['*.jar'], dir: 'libs')
2424
testCompile 'junit:junit:4.12'
25-
compile 'com.github.gianluca-nitti:java-expr-eval:v3.0'
25+
compile 'com.github.gianluca-nitti:java-expr-eval:v3.1'
2626
compile 'com.android.support:appcompat-v7:25+'
2727
compile 'com.android.support:support-v4:25+'
2828
}

app/src/main/java/com/github/gianlucanitti/expreval/ExprEval.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected void onCreate(Bundle savedInstanceState) {
3838
super.onCreate(savedInstanceState);
3939
setContentView(R.layout.activity_expr_eval);
4040
setSupportActionBar((Toolbar)findViewById(R.id.my_toolbar));
41+
LibraryLocalizer.localize(this);
4142
in = (EditText) findViewById(R.id.inputText);
4243
out = (TextView) findViewById(R.id.outputText);
4344
out.setMovementMethod(new ScrollingMovementMethod());

app/src/main/java/com/github/gianlucanitti/expreval/ExprEvalDialog.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ExprEvalDialog extends AppCompatActivity implements View.OnClickLis
1818
protected void onCreate(Bundle savedInstanceState) {
1919
super.onCreate(savedInstanceState);
2020
setContentView(R.layout.activity_expr_eval_dialog);
21+
LibraryLocalizer.localize(this);
2122
View okButton = findViewById(R.id.evalDialogOK);
2223
View cancelButton = findViewById(R.id.evalDialogCancel);
2324
okButton.setOnClickListener(this);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.gianlucanitti.expreval;
2+
3+
import android.content.Context;
4+
import static com.github.gianlucanitti.javaexpreval.LocalizationHelper.*;
5+
6+
public class LibraryLocalizer {
7+
8+
private static void setMessageResource(Context c, Message m, int id){
9+
setMessage(m, c.getString(id));
10+
}
11+
12+
public static void localize(Context c){
13+
setMessageResource(c, Message.CONTEXT_CLEARED, R.string.contextCleared);
14+
setMessageResource(c, Message.EMPTY_EXPR, R.string.emptyExpr);
15+
setMessageResource(c, Message.EMPTY_SYM_NAME, R.string.emptySymName);
16+
setMessageResource(c, Message.ERROR_PREFIX, R.string.errorPrefix);
17+
setMessageResource(c, Message.EVAL_STEP, R.string.evalStep);
18+
setMessageResource(c, Message.EXPR_END_REACHED, R.string.exprEndReached);
19+
setMessageResource(c, Message.FAILED_STORE_RESULT, R.string.failedStoreResult);
20+
setMessageResource(c, Message.FUNC_ASSIGNED, R.string.functionNewDef);
21+
setMessageResource(c, Message.INCORRECT_DELETE, R.string.incorrectDelete);
22+
setMessageResource(c, Message.INTERACTIVE_HELP, R.string.interactiveHelp);
23+
setMessageResource(c, Message.INVALID_OPERATOR, R.string.invalidOperator);
24+
setMessageResource(c, Message.INVALID_SYM_NAME, R.string.invalidSymName);
25+
setMessageResource(c, Message.ONLY_ONE_EQUALITY, R.string.onlyOneEquality);
26+
setMessageResource(c, Message.OPERATOR_EXPECTED, R.string.operatorExpected);
27+
setMessageResource(c, Message.OPERATOR_FOUND, R.string.operatorFound);
28+
setMessageResource(c, Message.PARENTHESIS_MISMATCH, R.string.parenthesisMismatch);
29+
setMessageResource(c, Message.READONLY_FUNC, R.string.readonlyFunc);
30+
setMessageResource(c, Message.READONLY_VAR, R.string.readonlyVar);
31+
setMessageResource(c, Message.RESERVED_WORD, R.string.reservedWord);
32+
setMessageResource(c, Message.REWRITE_STEP, R.string.rewriteStep);
33+
setMessageResource(c, Message.UNDEFINED_FUNC, R.string.undefinedFunc);
34+
setMessageResource(c, Message.UNDEFINED_VAR, R.string.undefinedVar);
35+
setMessageResource(c, Message.UNKNOWN_CHAR, R.string.unknownChar);
36+
setMessageResource(c, Message.VAR_ASSIGNED, R.string.varNewValue);
37+
setMessageResource(c, Message.VAR_DELETED, R.string.contextItemDeleted);
38+
}
39+
40+
}

app/src/main/res/values/strings.xml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<string name="exprInputHint" >Type expression or command</string>
88
<string name="evalBtn">Evaluate</string>
99
<string name="clearContextPrompt">Do you want to delete all non-readonly functions and variables?</string>
10-
<string name="contextCleared">All non-readonly variables and functions have been cleared.</string>
10+
<string name="contextCleared">All non-readonly variables and functions have been deleted.</string>
1111
<string name="helpMessage">
1212
In the input box you can type expressions, variable or function assignments (like &quot;a=5&quot; or &quot;log(x,b)=log(x)/log(b)&quot;) and commands.
1313
The available commands are &quot;help&quot;, &quot;context&quot;, &quot;clear&quot; and &quot;exit&quot;. Type &quot;help&quot; in the input box for more detailed instructions. In the output box, results are shown in &lt;font color=&quot;green&quot;&gt;green&lt;/font&gt; and errors in &lt;font color=&quot;red&quot;&gt;red&lt;/font&gt;.
@@ -29,23 +29,23 @@
2929
<item>Add function</item>
3030
</string-array>
3131
<string name="contextItemDeletePrompt">Do you want to delete "%s" ?</string>
32-
<string name="contextItemDeleted">"%s" has been deleted</string>
32+
<string name="contextItemDeleted">"%s" has been deleted.</string>
3333

3434
<!-- both EditFunctionDialogFragment and EditVariableDialogFragment -->
3535
<string name="name">Name</string>
3636
<string name="readonlyCheckbox">Read-only (if checked, you won\'t be able to edit or delete this until the application is restarted.)</string>
3737

3838
<!-- EditVariableDialogFragment -->
3939
<string name="value">Value</string>
40-
<string name="varNewValue" formatted="false">%1s is now %2s.</string>
40+
<string name="varNewValue">%1$s is now %2$s.</string>
4141

4242
<!-- EditFunctionDialogFragment -->
4343
<string name="expression">Expression</string>
4444
<string name="arguments">Arguments (tap to remove)</string>
4545
<string name="argumentHint">Type argument name</string>
4646
<string name="addArgument">Add argument</string>
4747
<string name="invalidArgName">Invalid argument name</string>
48-
<string name="functionNewDef" formatted="false">%1s is now defined as %2s.</string>
48+
<string name="functionNewDef">%1$s is now defined as %2$s.</string>
4949

5050
<!-- ExprEvalDialog activity -->
5151
<string name="expressionColon">Expression: </string>
@@ -54,4 +54,38 @@
5454
<string name="okToReplace">Tap OK to replace the expression with the result.</string>
5555
<string name="close">Close</string>
5656
<string name="evalFailed"> evaluation failed.</string>
57+
58+
<!-- Various messages used by the java-expr-eval library (https://github.com/gianluca-nitti/java-expr-eval) -->
59+
<string name="emptyExpr">An empty expression was found.</string>
60+
<string name="emptySymName">The empty string isn\'t a valid symbol identifier.</string>
61+
<string name="errorPrefix">Expression error: %s</string>
62+
<string name="evalStep">%1$s evaluates to %2$s</string>
63+
<string name="exprEndReached">A sub-expression was expected, but the end of the expression was reached.</string>
64+
<string name="failedStoreResult">Warning: failed to store result. Reason: %s</string>
65+
<string name="incorrectDelete">Incorrect syntax. To delete a function, the number of arguments must be specified (e.g. "fun(2)=" to delete "fun(x, y)").</string>
66+
<string name="interactiveHelp">
67+
Accepted statements are expressions, assignments and commands.
68+
An expression can be formed by integer or decimal numbers, the +,-,*,/,^ binary operators, variables, functions and parenthesis.
69+
A variable is a string of one or more letters and/or underscores. Variables can\'t be named as commands, which are reserved words.
70+
When an expression is successfully evaluated, it\'s result is displayed and automatically assigned to the "ans" variable, so it can be accessed from the next statement.
71+
A variable assignment is formed by a variable name followed by the = symbol and an expression, which is evaluated and bound to that variable.
72+
An empty assignment (in the form "someVariable=") deletes the variable.
73+
A function assignment is formed by a function name and its parameters, followed by the = symbol and an expression, which is bound to that function, e.g."sum(x,y)=x+y".
74+
A function can be deleted with an empty assignment; the number of arguments must be specified, e.g. "sum(2)=" to delete the function "sum" defined on two arguments.
75+
An assignment (of variable or function) can be prepended with the "readonly" word to prevent it to be modified or deleted, e.g. "readonly x=1", "readonly square(a)=a^2".
76+
The commands are: context (prints all the defined variables and functions), clear (deletes all the non-readonly variables and functions), help (shows this message) and exit (stops reading input).
77+
</string>
78+
<string name="invalidOperator">Unknown operator \'%s\'.</string>
79+
<string name="invalidSymName">"%1$s" isn\'t a valid symbol name because it contains the \'%2$s\' character.</string>
80+
<string name="onlyOneEquality">Only one = operator per command is allowed.</string>
81+
<string name="operatorExpected">An operator was expected, but an expression was found.</string>
82+
<string name="operatorFound">A sub-expression was expected, but an operator was found.</string>
83+
<string name="parenthesisMismatch">The numbers of opened and closed parenthesis don\'t match.</string>
84+
<string name="readonlyFunc">The "%1$s" function is defined as read-only for %1$s arguments.</string>
85+
<string name="readonlyVar">The "%s" variable is defined as read-only.</string>
86+
<string name="reservedWord">%s is a reserved word and can\'t be used as symbol name.</string>
87+
<string name="rewriteStep">%1$s can be rewritten as %2$s</string>
88+
<string name="undefinedFunc">The function "%1$s" is not defined for %2$s arguments.</string>
89+
<string name="undefinedVar">The variable "%s" is not defined.</string>
90+
<string name="unknownChar">Unrecognized character %s.</string>
5791
</resources>

0 commit comments

Comments
 (0)