Mohammed Shekha(Open ERP) has proposed merging 
lp:~openerp-dev/openobject-client-web/6.0-opw-16966-msh into 
lp:openobject-client-web/6.0.

Requested reviews:
  OpenERP Core Team (openerp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-client-web/6.0-opw-16966-msh/+merge/75522

Hello,

Add the feature of decimal point, thousand separator and grouping according to 
user's language preference.

Previously web-client uses default decimal point, thousand separator and 
grouping of locale by using babel.

Now changed the code so that whatever decimal point, thousand separator and 
grouping user have set in Administration -> Translation -> Language, it will be 
applied.

Thanks.
-- 
https://code.launchpad.net/~openerp-dev/openobject-client-web/6.0-opw-16966-msh/+merge/75522
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-client-web/6.0-opw-16966-msh.
=== modified file 'addons/openerp/utils/rpc.py'
--- addons/openerp/utils/rpc.py	2011-02-08 18:57:20 +0000
+++ addons/openerp/utils/rpc.py	2011-09-15 11:24:15 +0000
@@ -28,6 +28,7 @@
 
 from tiny_socket import TinySocket
 from tiny_socket import TinySocketError
+from openobject.i18n import format
 
 class NotLoggedIn(openobject.errors.TinyError, openobject.errors.AuthenticationError): pass
 
@@ -349,6 +350,8 @@
         if lang_ids:
             self.storage['lang'] = self.execute(
                     'object', 'execute', 'res.lang', 'read', lang_ids[0], [])
+            lang_data = self.storage['lang']
+            format.set_locale_cache(lang_data)
 
     def execute(self, obj, method, *args):
         if not self.is_logged():

=== modified file 'openobject/i18n/format.py'
--- openobject/i18n/format.py	2011-03-22 07:56:34 +0000
+++ openobject/i18n/format.py	2011-09-15 11:24:15 +0000
@@ -290,32 +290,113 @@
 
     return fixed_domain
 
-def format_decimal(value, digits=2):
+LOCALE_CACHE = {
+               'date_format':'%m/%d/%Y',
+               'time_format':'%H:%M:%S',
+               'grouping':[],
+               'decimal_point':'.',
+               'thousands_sep': ','
+                }
+
+def get_lang_float_format(locale_lang,monetary=False):
+    thousands_sep = LOCALE_CACHE.get('thousands_sep') or numbers.get_group_symbol(locale_lang)
+    decimal_point = LOCALE_CACHE.get('decimal_point')
+    grouping      = LOCALE_CACHE.get('grouping')
+    return (grouping, thousands_sep, decimal_point)
+
+
+def format_decimal(value, digits=2, grouping=True, monetary=False):
     locale = get_locale()
-    v = ("%%.%df" % digits) % value
-    if not digits:
-        return numbers.format_number(value, locale=locale)
-    num, decimals = v.split(".", 1)
-
-    if num == "-0":
-        val = "-0"
-    else:
-        val = numbers.format_number(int(num), locale=locale)
-
-    return val + unicode(numbers.get_decimal_symbol(locale) + decimals)
-
+    formatted = ("%%.%df" % digits) % value
+    lang_grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False)
+    
+    seps = 0
+    parts = formatted.split('.')
+
+    if grouping:
+        parts[0], seps = group(parts[0], monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep)
+
+    formatted = decimal_point.join(parts)
+    while seps:
+        sp = formatted.find(' ')
+        if sp == -1: break
+        formatted = formatted[:sp] + formatted[sp+1:]
+        seps -= 1
+    return formatted
+
+
+def group(value, monetary=False, grouping=False, thousands_sep=''):
+
+    """ This function will convert the value in appropriate format after applying
+        thousands_sep, grouping etc
+
+        @param value:The value to be converted
+        @param monetary:True or False by default False
+        @param grouping:True or False by default False
+        @param thousands_sep: The symbol to be applied at the thousand's place by default blank
+
+        @return: The converted value"""
+
+    grouping = eval(grouping)
+    if not grouping:
+        return (value, 0)
+
+    result = ""
+    seps = 0
+    spaces = ""
+
+    if value[-1] == ' ':
+        sp = value.find(' ')
+        spaces = value[sp:]
+        value = value[:sp]
+
+    while value and grouping:
+        # if grouping is -1, we are done
+        if grouping[0] == -1:
+            break
+        # 0: re-use last group ad infinitum
+        elif grouping[0] != 0:
+            #process last group
+            group = grouping[0]
+            grouping = grouping[1:]
+        if result:
+            result = value[-group:] + thousands_sep + result
+            seps += 1
+        else:
+            result = value[-group:]
+        value = value[:-group]
+        if value and value[-1] not in "0123456789":
+            # the leading string is only spaces and signs
+            return value + result + spaces, seps
+    if not result:
+        return value + spaces, seps
+    if value:
+        result = value + thousands_sep + result
+        seps += 1
+    return result + spaces, seps         
+        
+
+def set_locale_cache(lang_data={}):
+    try:
+        if lang_data:
+            if 'id' in lang_data:
+                del lang_data['id']
+            LOCALE_CACHE.update(lang_data)
+    except:
+        pass
+
+    
 def parse_decimal(value):
-
+    
+    locale = get_locale()
     if isinstance(value, basestring):
-
+        
         value = ustr(value)
-
+        
+        grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False)
         #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char)
         value = value.replace(' ', '')
-        try:
-            value = numbers.parse_decimal(value, locale=get_locale())
-        except ValueError:
-            pass
+        value = value.replace(thousands_sep, '').replace(decimal_point, '.')
 
     if not isinstance(value, float):
         return float(value)

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to