From 0828cc9d8e7af72c2e3a6b503926ea60884b9cd9 Mon Sep 17 00:00:00 2001
From: Walter Bender <walter@sugarlabs.org>
Date: Wed, 5 Jan 2011 17:49:39 -0500
Subject: [PATCH] adding i18n support for fraction separator
---
calculate.py | 14 +++++++++++++-
layout.py | 3 ++-
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/calculate.py b/calculate.py
index bda756c..1b89b88 100644
a
|
b
|
|
1 | | # -*- coding: UTF-8 -*- |
| 1 | # -*- coding: utf-8 -*- |
2 | 2 | # calculate.py, sugar calculator, by: |
3 | 3 | # Reinier Heeres <reinier@heeres.eu> |
4 | 4 | # Miguel Alvarez <miguel@laptop.org> |
… |
… |
import types |
24 | 24 | import os |
25 | 25 | from gettext import gettext as _ |
26 | 26 | import string |
| 27 | import re |
27 | 28 | import logging |
28 | 29 | _logger = logging.getLogger('Calculate') |
29 | 30 | |
… |
… |
from svgimage import SVGImage |
48 | 49 | from decimal import Decimal |
49 | 50 | from rational import Rational |
50 | 51 | |
| 52 | |
51 | 53 | def findchar(text, chars, ofs=0): |
52 | 54 | ''' |
53 | 55 | Find a character in set <chars> starting from offset ofs. |
… |
… |
class Calculate(ShareableActivity): |
448 | 450 | def process_async(self, eqn): |
449 | 451 | """Parse and process an equation asynchronously.""" |
450 | 452 | |
| 453 | def replace_fraction_seperator(self, text): |
| 454 | ''' |
| 455 | Replace the fraction separator with '.' |
| 456 | ''' |
| 457 | match = text.group(0) |
| 458 | return match.replace(self.ml.fraction_sep, '.') |
| 459 | |
451 | 460 | def process(self): |
452 | 461 | """Parse the equation entered and show the result""" |
453 | 462 | |
454 | 463 | s = unicode(self.text_entry.get_text()) |
455 | 464 | label = unicode(self.label_entry.get_text()) |
456 | 465 | _logger.debug('process(): parsing %r, label: %r', s, label) |
| 466 | # Replace the fraction separator with a period before parsing |
| 467 | # A simple replace won't work, e.g. plot(x*x,x=-1,0..1,) |
| 468 | s = re.sub(r'\d+\,\d+', self.replace_fraction_seperator, s) |
457 | 469 | try: |
458 | 470 | tree = self.parser.parse(s) |
459 | 471 | res = self.parser.evaluate(tree) |
diff --git a/layout.py b/layout.py
index d439a3a..9fb0cab 100644
a
|
b
|
class CalcLayout: |
47 | 47 | |
48 | 48 | mul_sym = self._parent.ml.mul_sym |
49 | 49 | div_sym = self._parent.ml.div_sym |
| 50 | fraction_sep = self._parent.ml.fraction_sep |
50 | 51 | |
51 | 52 | self.button_data = [ |
52 | 53 | # [x, y, width, label, bgcol, cb] |
… |
… |
class CalcLayout: |
63 | 64 | [2, 2, 1, '3', self.col_gray2, lambda w: self._parent.add_text('3')], |
64 | 65 | |
65 | 66 | [0, 3, 1, '0', self.col_gray2, lambda w: self._parent.add_text('0')], |
66 | | [1, 3, 1, '.', self.col_gray2, lambda w: self._parent.add_text('.')], |
| 67 | [1, 3, 1, fraction_sep, self.col_gray2, lambda w: self._parent.add_text(fraction_sep)], |
67 | 68 | |
68 | 69 | [3, 0, 3, _('Clear'), self.col_gray1, lambda w: self._parent.clear()], |
69 | 70 | |