From 5acdeb559477e99db986e5836aec9d8202104555 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 28 Sep 2010 16:14:39 +0000 Subject: added support for locale decimal_point --- (limited to 'TurtleArt') diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index ffa50b2..163f7bf 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -47,9 +47,10 @@ _logger = logging.getLogger('turtleart-activity') # A class for the list of blocks and everything they share in common # class Blocks: - def __init__(self, font_scale_factor = 1): + def __init__(self, font_scale_factor=1, decimal_point='.'): self.list = [] self.font_scale_factor = font_scale_factor + self.decimal_point = decimal_point def get_block(self, i): if i < 0 or i > len(self.list)-1: @@ -93,6 +94,7 @@ class Blocks: class Block: def __init__(self, block_list, sprite_list, name, x, y, type='block', values=[], scale=BLOCK_SCALE, colors=["#FF0000","#A00000"]): + self.block_list = block_list self.spr = None self.shapes = [None, None] self.name = name @@ -115,7 +117,7 @@ class Block: self.name = OLD_NAMES[self.name] for i in range(len(self._font_size)): - self._font_size[i] *= self.scale*block_list.font_scale_factor + self._font_size[i] *= self.scale*self.block_list.font_scale_factor for v in (values): self.values.append(v) @@ -126,7 +128,7 @@ class Block: self.name not in EXPANDABLE_BLOCKS and \ self.name not in EXPANDABLE_ARGS and \ self.name not in ['string', 'sandwichtop', 'sandwichtop_no_label']: - for b in block_list.list: + for b in self.block_list.list: if b.scale == self.scale and b.name == self.name: copy_block = b break @@ -135,7 +137,7 @@ class Block: if name in PRIMITIVES: self.primitive = PRIMITIVES[self.name] - block_list.append_to_list(self) + self.block_list.append_to_list(self) # We may want to highlight a block... def highlight(self): @@ -313,7 +315,11 @@ class Block: len(self.values) > 0: for i, v in enumerate(self.values): if v is not None: - self._set_labels(i, str(v)) + if self.name == 'number': + self._set_labels(i, + str(v).replace('.', self.block_list.decimal_point)) + else: + self._set_labels(i, str(v)) elif self.name in BLOCK_NAMES: for i, n in enumerate(BLOCK_NAMES[self.name]): self._set_labels(i, n) diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 43886f0..d69379a 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -80,7 +80,7 @@ def numtype(x): def str_to_num(x): """ Try to comvert a string to a number """ - xx = convert(x, float) + xx = convert(x.replace(self.tw.decimal_point,'.'), float) if type(xx) is float: return xx else: @@ -215,7 +215,6 @@ def tarandom(x, y): return(int(round(uniform(x, y),0))) xx, xflag = chr_to_ord(x) yy, yflag = chr_to_ord(y) - print xx, xflag, yy, yflag if xflag and yflag: return chr(int(round(uniform(xx, yy),0))) if not xflag: @@ -1010,7 +1009,8 @@ class LogoCode: elif type(n) == int: self.tw.showlabel('status', n) else: - self.tw.showlabel('status', round_int(n)) + self.tw.showlabel('status', + str(round_int(n)).replace('.', self.tw.decimal_point)) def prim_kbinput(self): """ Query keyboard """ diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index 170eaa3..0b2b0e6 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -29,6 +29,8 @@ import os import os.path from math import atan2, pi DEGTOR = 2*pi/360 + +import locale from gettext import gettext as _ try: @@ -124,6 +126,8 @@ class TurtleArtWindow(): self.mouse_x = 0 self.mouse_y = 0 + self.decimal_point = locale.localeconv()['decimal_point'] + self.orientation = HORIZONTAL_PALETTE if olpc_xo_1(): self.lead = 1.0 @@ -176,7 +180,8 @@ class TurtleArtWindow(): self.drag_pos = 0, 0 self.paste_offset = 20 - self.block_list = Blocks(self.scale) + self.block_list = Blocks(font_scale_factor=self.scale, + decimal_point=self.decimal_point) if self.interactive_mode: self.sprite_list = Sprites(self.window, self.area, self.gc) else: @@ -1829,7 +1834,11 @@ class TurtleArtWindow(): newnum = '-' + oldnum else: newnum = oldnum - elif keyname == 'period' and '.' not in oldnum: + elif keyname == 'comma' and self.decimal_point == ',' and \ + ',' not in oldnum: + newnum = oldnum + ',' + elif keyname == 'period' and self.decimal_point == '.' and \ + '.' not in oldnum: newnum = oldnum + '.' elif keyname == 'BackSpace': if len(oldnum) > 0: @@ -1848,9 +1857,11 @@ class TurtleArtWindow(): newnum = oldnum if newnum == '.': newnum = '0.' + if newnum == ',': + newnum = '0,' if len(newnum) > 0 and newnum != '-': try: - float(newnum) + float(newnum.replace(self.decimal_point, '.')) except ValueError, e: newnum = oldnum self.selected_blk.spr.set_label(newnum + CURSOR) @@ -1999,11 +2010,11 @@ class TurtleArtWindow(): def _number_check(self): """ Make sure a 'number' block contains a number. """ n = self.selected_blk.spr.labels[0].replace(CURSOR, '') - if n in ['-', '.', '-.']: + if n in ['-', '.', '-.', ',']: n = 0 - if n is not None: + elif n is not None: try: - f = float(n) + f = float(n.replace(self.decimal_point, '.')) if f > 1000000: n = 1 self.showlabel("#overflowerror") @@ -2016,7 +2027,7 @@ class TurtleArtWindow(): else: n = 0 self.selected_blk.spr.set_label(n) - self.selected_blk.values[0] = n + self.selected_blk.values[0] = n.replace(self.decimal_point, '.') def _string_check(self): s = self.selected_blk.spr.labels[0].replace(CURSOR, '') -- cgit v0.9.1