-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.js
227 lines (219 loc) · 9.23 KB
/
lang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const speedLabel = document.getElementById("speed-label");
const expressionInputLabel = document.getElementById("expr-label");
const canvasWidthLabel = document.getElementById("canvas-width-label");
const canvasHeightLabel = document.getElementById("canvas-height-label");
const langSelectLabel = document.getElementById("lang-select-label");
const langSelect = document.getElementById("lang-select");
const langSwitcher = document.getElementById("lang-switcher");
const description = document.getElementById("description");
const title = document.getElementsByTagName("title")[0];
const opListSummary = document.getElementById("op-list-summary");
let currentLang = "en";
let langStrings = {
"en": {
name: "English",
dir: "ltr",
stepBack: "Step Back",
stepForward: "Step Forward",
resume: "Resume",
pause: "Pause",
play: "Play",
animationSpeed: "Animation Speed",
height: "Height:",
width: "Width:",
changeCanvasSize: "Change Canvas Size",
expressionInputLabel: "Mathematical expression to evaluate:",
langSelectLabel: "Switch Language:",
langSwitcherFloat: "right",
canvasHeightWarning: "Canvas is not high enough to draw all data - Adjust its height from the controls to the left",
discardLeftParenthesis: "Popping left parenthesis",
mismatchedParentheses: "Mismatched parentheses",
invalidSyntax: "Invalid syntax",
operations: "operations",
output: "output",
description: 'A demo of the <a href="https://en.wikipedia.org/wiki/Shunting-yard_algorithm">Shunting-yard algorithm</a> for parsing mathematical expressions',
title: "A demo of the Shunting-yard algorithm",
opListSummary: "Supported operations",
pushingOperand: function (n) {
return `Pushing operand ${n} to output stack`;
},
pushingOperator: function (op, operandsNum) {
let label = (operandsNum > 0 || (op.indexOf(')') !== -1 || op.indexOf('(') !== -1)) ? "operator" : "constant";
return `Pushing ${label} '${op}' to operations stack`;
},
evaluatingConstant: function (op) {
return `Evaluating constant '${op}'`;
},
evaluatingUnaryOperation: function (op, operand) {
let expr;
if (op === "!") {
expr = `'${operand}${op}'`;
} else {
expr = `'${op}(${operand})'`;
}
expr = force_ltr(expr);
return `Evaluating operation ${expr}`;
},
evaluatingOperation: function (op, opr1, opr2) {
let expr = force_ltr(`${opr1} ${op} ${opr2}`);
return `Evaluating operation '${expr}'`;
},
pushingOperationResult: function (res) {
return `Pushing operation result '${res}' to output stack`;
},
expressionResult: function (res) {
return `Expression evaluation finished. Result is '${res}'`;
},
expectedOperandForUnary: function (op) {
return `Found no operand for operator '${op}', which takes one operand`;
},
expectedOperandForBinary: function (op) {
return `Found no operands for operator '${op}', which takes two operands`;
},
notValidAsUnary: function (op) {
let msg = `Operator '${op}' is not valid as unary`;
if (op.indexOf('+') !== -1 || op.indexOf('-') !== -1) {
msg += ' here';
}
return msg;
},
evaluatingExpression: function (expr) {
return `Evaluating "${expr}"`;
},
},
"ar": {
name: "العربية",
dir: "rtl",
stepBack: "الرجوع بخطوة",
stepForward: "التقدم بخطوة",
resume: "استمرار",
pause: "إيقاف",
play: "تشغيل",
animationSpeed: "سرعة العرض",
height: "الارتفاع:",
width: "العرض:",
changeCanvasSize: "تغيير حجم مساحة الرسم",
expressionInputLabel: "العبارة الرياضية التي تريد حسابها:",
langSelectLabel: "تغيير اللغة:",
langSwitcherFloat: "left",
canvasHeightWarning: "ارتفاع مساحة الرسم غير كافٍ لرسم كل البيانات - اضبط ارتفاعها من الخيارات على اليمين",
discardLeftParenthesis: "طرح القوس الأيسر",
mismatchedParentheses: "هناك أقواس ناقصة",
invalidSyntax: "هناك خطأ في العبارة",
operations: "مكدس العمليات",
output: "مكدس المخرجات",
description: 'استعراض لخوارزمية <a href="https://www.abdnh.net/shunting-yard-algorithm">ساحة تحويل السكك الحديدية</a> لحساب العبارات الرياضية',
title: "استعراض لخوارزمية ساحة تحويل السكك الحديدية",
opListSummary: "العمليات المدعومة",
pushingOperand: function (n) {
return `دفع المعامل ${n} إلى مكدس المخرجات`;
},
pushingOperator: function (op, operandsNum) {
let label = (operandsNum > 0 || (op.indexOf(')') !== -1 || op.indexOf('(') !== -1)) ? "العملية" : "الثابت";
return `دفع ${label} '${op}' إلى مكدس العمليات`;
},
evaluatingConstant: function (op) {
return `حساب الثابت '${op}'`;
},
evaluatingUnaryOperation: function (op, operand) {
let expr;
if (op === "!") {
expr = `'${operand}${op}'`;
} else {
expr = `'${op}(${operand})'`;
}
expr = force_ltr(expr);
return `حساب العملية ${expr}`;
},
evaluatingOperation: function (op, opr1, opr2) {
let expr = force_ltr(`${opr1} ${op} ${opr2}`);
return `حساب العملية '${expr}'`;
},
pushingOperationResult: function (res) {
return `دفع نتيجة العملية '${res}' إلى مكدس المخرجات`;
},
expressionResult: function (res) {
return `تم حساب نتيجة العبارة. النتيجة هي '${res}'`;
},
expectedOperandForUnary: function (op) {
return `تعذر إيجاد معامل لعملية '${op}'، والتي تأخذ معاملا واحدا`;
},
expectedOperandForBinary: function (op) {
return `تعذر إيجاد معاملات لعملية '${op}'، والتي تأخذ معاملين`;
},
notValidAsUnary: function (op) {
let msg = `العملية '${op}' غير صالحة كعملية أحادية`;
if (op.indexOf('+') !== -1 || op.indexOf('-') !== -1) {
msg += ' هنا';
}
return msg;
},
evaluatingExpression: function (expr) {
return `حساب العبارة "${expr}"`;
},
}
}
function setLang(lang) {
if (!langStrings.hasOwnProperty(lang)) return;
currentLang = lang;
let strings = langStrings[lang];
document.documentElement.lang = lang;
document.body.dir = strings.dir;
stepBackButton.textContent = strings.stepBack;
stepForwardButton.textContent = strings.stepForward;
resumeButton.textContent = strings.resume;
playButton.textContent = strings.play;
canvasDimensionsButton.textContent = strings.changeCanvasSize;
expressionInputLabel.textContent = strings.expressionInputLabel;
speedLabel.textContent = strings.animationSpeed;
canvasWidthLabel.textContent = strings.width;
canvasHeightLabel.textContent = strings.height;
langSelectLabel.textContent = strings.langSelectLabel;
langSwitcher.style.float = strings.langSwitcherFloat;
description.innerHTML = strings.description;
title.textContent = strings.title;
opListSummary.textContent = strings.opListSummary;
setCanvasDimensions(document.body.clientWidth, document.body.clientHeight);
}
function force_ltr(s) {
return `\u200e${s}\u200e`;
}
function getString(name) {
let prop = langStrings[currentLang][name];
if (typeof prop === "function") {
return prop(...Array.from(arguments).slice(1).map(e => {
if (typeof e === "string") {
return force_ltr(e);
} else return e;
}));
}
else {
return prop;
}
}
function detectLang() {
let params = (new URL(document.location)).searchParams;
let paramLang = params.get("lang");
if (paramLang) {
setLang(paramLang);
}
else if (navigator.language.startsWith('ar')) {
setLang("ar");
}
}
function populateLangSelector() {
for (let [lang, strings] of Object.entries(langStrings)) {
let option = document.createElement("option");
option.value = lang;
option.textContent = strings.name;
langSelect.appendChild(option);
if (currentLang === lang) {
option.selected = true;
}
}
langSelect.addEventListener("input", () => {
setLang(langSelect.value);
});
}
detectLang();
populateLangSelector();