[IMP] translations: simplify condition in qweb terms extraction
[odoo/odoo.git] / openerp / tools / amount_to_text_en.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import logging
23 from translate import _
24
25 _logger = logging.getLogger(__name__)
26
27 #-------------------------------------------------------------
28 #ENGLISH
29 #-------------------------------------------------------------
30
31 to_19 = ( 'Zero',  'One',   'Two',  'Three', 'Four',   'Five',   'Six',
32           'Seven', 'Eight', 'Nine', 'Ten',   'Eleven', 'Twelve', 'Thirteen',
33           'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
34 tens  = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
35 denom = ( '',
36           'Thousand',     'Million',         'Billion',       'Trillion',       'Quadrillion',
37           'Quintillion',  'Sextillion',      'Septillion',    'Octillion',      'Nonillion',
38           'Decillion',    'Undecillion',     'Duodecillion',  'Tredecillion',   'Quattuordecillion',
39           'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
40
41 def _convert_nn(val):
42     """convert a value < 100 to English.
43     """
44     if val < 20:
45         return to_19[val]
46     for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
47         if dval + 10 > val:
48             if val % 10:
49                 return dcap + '-' + to_19[val % 10]
50             return dcap
51
52 def _convert_nnn(val):
53     """
54         convert a value < 1000 to english, special cased because it is the level that kicks 
55         off the < 100 special case.  The rest are more general.  This also allows you to
56         get strings in the form of 'forty-five hundred' if called directly.
57     """
58     word = ''
59     (mod, rem) = (val % 100, val // 100)
60     if rem > 0:
61         word = to_19[rem] + ' Hundred'
62         if mod > 0:
63             word += ' '
64     if mod > 0:
65         word += _convert_nn(mod)
66     return word
67
68 def english_number(val):
69     if val < 100:
70         return _convert_nn(val)
71     if val < 1000:
72          return _convert_nnn(val)
73     for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
74         if dval > val:
75             mod = 1000 ** didx
76             l = val // mod
77             r = val - (l * mod)
78             ret = _convert_nnn(l) + ' ' + denom[didx]
79             if r > 0:
80                 ret = ret + ', ' + english_number(r)
81             return ret
82
83 def amount_to_text(number, currency):
84     number = '%.2f' % number
85     units_name = currency
86     list = str(number).split('.')
87     start_word = english_number(int(list[0]))
88     end_word = english_number(int(list[1]))
89     cents_number = int(list[1])
90     cents_name = (cents_number > 1) and 'Cents' or 'Cent'
91
92     return ' '.join(filter(None, [start_word, units_name, (start_word or units_name) and (end_word or cents_name) and 'and', end_word, cents_name]))
93
94
95 #-------------------------------------------------------------
96 # Generic functions
97 #-------------------------------------------------------------
98
99 _translate_funcs = {'en' : amount_to_text}
100     
101 #TODO: we should use the country AND language (ex: septante VS soixante dix)
102 #TODO: we should use en by default, but the translation func is yet to be implemented
103 def amount_to_text(nbr, lang='en', currency='euro'):
104     """ Converts an integer to its textual representation, using the language set in the context if any.
105     
106         Example::
107         
108             1654: thousands six cent cinquante-quatre.
109     """
110     import openerp.loglevels as loglevels
111 #    if nbr > 10000000:
112 #        _logger.warning(_("Number too large '%d', can not translate it"))
113 #        return str(nbr)
114     
115     if not _translate_funcs.has_key(lang):
116         _logger.warning(_("no translation function found for lang: '%s'"), lang)
117         #TODO: (default should be en) same as above
118         lang = 'en'
119     return _translate_funcs[lang](abs(nbr), currency)
120
121 if __name__=='__main__':
122     from sys import argv
123     
124     lang = 'nl'
125     if len(argv) < 2:
126         for i in range(1,200):
127             print i, ">>", int_to_text(i, lang)
128         for i in range(200,999999,139):
129             print i, ">>", int_to_text(i, lang)
130     else:
131         print int_to_text(int(argv[1]), lang)
132
133
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: