1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 #-------------------------------------------------------------
24 #-------------------------------------------------------------
27 to_19_fr = ( 'zéro', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six',
28 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize',
29 'quatorze', 'quinze', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf' )
30 tens_fr = ( 'vingt', 'trente', 'quarante', 'Cinquante', 'Soixante', 'Soixante-dix', 'Quatre-vingts', 'Quatre-vingt Dix')
32 'Mille', 'Millions', 'Milliards', 'Billions', 'Quadrillions',
33 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
34 'Décillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
35 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Icosillion', 'Vigintillion' )
37 def _convert_nn_fr(val):
38 """ convert a value < 100 to French
42 for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_fr)):
45 return dcap + '-' + to_19_fr[val % 10]
48 def _convert_nnn_fr(val):
49 """ convert a value < 1000 to french
51 special cased because it is the level that kicks
52 off the < 100 special case. The rest are more general. This also allows you to
53 get strings in the form of 'forty-five hundred' if called directly.
56 (mod, rem) = (val % 100, val // 100)
58 word = to_19_fr[rem] + ' Cent'
62 word = word + _convert_nn_fr(mod)
65 def french_number(val):
67 return _convert_nn_fr(val)
69 return _convert_nnn_fr(val)
70 for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_fr))):
75 ret = _convert_nnn_fr(l) + ' ' + denom_fr[didx]
77 ret = ret + ', ' + french_number(r)
80 def amount_to_text_fr(number, currency):
81 number = '%.2f' % number
83 list = str(number).split('.')
84 start_word = french_number(abs(int(list[0])))
85 end_word = french_number(int(list[1]))
86 cents_number = int(list[1])
87 cents_name = (cents_number > 1) and ' Cents' or ' Cent'
88 final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
91 #-------------------------------------------------------------
93 #-------------------------------------------------------------
95 to_19_nl = ( 'Nul', 'Een', 'Twee', 'Drie', 'Vier', 'Vijf', 'Zes',
96 'Zeven', 'Acht', 'Negen', 'Tien', 'Elf', 'Twaalf', 'Dertien',
97 'Veertien', 'Vijftien', 'Zestien', 'Zeventien', 'Achttien', 'Negentien' )
98 tens_nl = ( 'Twintig', 'Dertig', 'Veertig', 'Vijftig', 'Zestig', 'Zeventig', 'Tachtig', 'Negentig')
100 'Duizend', 'Miljoen', 'Miljard', 'Triljoen', 'Quadriljoen',
101 'Quintillion', 'Sextiljoen', 'Septillion', 'Octillion', 'Nonillion',
102 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
103 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
105 def _convert_nn_nl(val):
106 """ convert a value < 100 to Dutch
110 for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_nl)):
113 return dcap + '-' + to_19_nl[val % 10]
116 def _convert_nnn_nl(val):
117 """ convert a value < 1000 to Dutch
119 special cased because it is the level that kicks
120 off the < 100 special case. The rest are more general. This also allows you to
121 get strings in the form of 'forty-five hundred' if called directly.
124 (mod, rem) = (val % 100, val // 100)
126 word = to_19_nl[rem] + ' Honderd'
130 word = word + _convert_nn_nl(mod)
133 def dutch_number(val):
135 return _convert_nn_nl(val)
137 return _convert_nnn_nl(val)
138 for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_nl))):
143 ret = _convert_nnn_nl(l) + ' ' + denom_nl[didx]
145 ret = ret + ', ' + dutch_number(r)
148 def amount_to_text_nl(number, currency):
149 number = '%.2f' % number
150 units_name = currency
151 list = str(number).split('.')
152 start_word = dutch_number(int(list[0]))
153 end_word = dutch_number(int(list[1]))
154 cents_number = int(list[1])
155 cents_name = (cents_number > 1) and 'cent' or 'cent'
156 final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
159 #-------------------------------------------------------------
161 #-------------------------------------------------------------
163 _translate_funcs = {'fr' : amount_to_text_fr, 'nl' : amount_to_text_nl}
165 def add_amount_to_text_function(lang, func):
166 _translate_funcs[lang] = func
168 #TODO: we should use the country AND language (ex: septante VS soixante dix)
169 #TODO: we should use en by default, but the translation func is yet to be implemented
170 def amount_to_text(nbr, lang='fr', currency='euro'):
171 """ Converts an integer to its textual representation, using the language set in the context if any.
175 1654: mille six cent cinquante-quatre.
179 # print "WARNING: number too large '%d', can't translate it!" % (nbr,)
182 if not _translate_funcs.has_key(lang):
184 print "WARNING: no translation function found for lang: '%s'" % (lang,)
185 #TODO: (default should be en) same as above
187 return _translate_funcs[lang](abs(nbr), currency)
189 if __name__=='__main__':
194 for i in range(1,200):
195 print i, ">>", amount_to_text(i, lang)
196 for i in range(200,999999,139):
197 print i, ">>", amount_to_text(i, lang)
199 print amount_to_text(int(argv[1]), lang)
202 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: