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 # convert a value < 100 to French.
38 def _convert_nn_fr(val):
41 for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_fr)):
44 return dcap + '-' + to_19_fr[val % 10]
47 # convert a value < 1000 to french, special cased because it is the level that kicks
48 # off the < 100 special case. The rest are more general. This also allows you to
49 # get strings in the form of 'forty-five hundred' if called directly.
50 def _convert_nnn_fr(val):
52 (mod, rem) = (val % 100, val // 100)
54 word = to_19_fr[rem] + ' Cent'
58 word = word + _convert_nn_fr(mod)
61 def french_number(val):
63 return _convert_nn_fr(val)
65 return _convert_nnn_fr(val)
66 for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_fr))):
71 ret = _convert_nnn_fr(l) + ' ' + denom_fr[didx]
73 ret = ret + ', ' + french_number(r)
76 def amount_to_text_fr(number, currency):
77 number = '%.2f' % number
79 list = str(number).split('.')
80 start_word = french_number(abs(int(list[0])))
81 end_word = french_number(int(list[1]))
82 cents_number = int(list[1])
83 cents_name = (cents_number > 1) and ' Cents' or ' Cent'
84 final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
87 #-------------------------------------------------------------
89 #-------------------------------------------------------------
91 to_19_nl = ( 'Nul', 'Een', 'Twee', 'Drie', 'Vier', 'Vijf', 'Zes',
92 'Zeven', 'Acht', 'Negen', 'Tien', 'Elf', 'Twaalf', 'Dertien',
93 'Veertien', 'Vijftien', 'Zestien', 'Zeventien', 'Achttien', 'Negentien' )
94 tens_nl = ( 'Twintig', 'Dertig', 'Veertig', 'Vijftig', 'Zestig', 'Zeventig', 'Tachtig', 'Negentig')
96 'Duizend', 'Miljoen', 'Miljard', 'Triljoen', 'Quadriljoen',
97 'Quintillion', 'Sextiljoen', 'Septillion', 'Octillion', 'Nonillion',
98 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
99 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
101 # convert a value < 100 to Dutch.
102 def _convert_nn_nl(val):
105 for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_nl)):
108 return dcap + '-' + to_19_nl[val % 10]
111 # convert a value < 1000 to Dutch, special cased because it is the level that kicks
112 # off the < 100 special case. The rest are more general. This also allows you to
113 # get strings in the form of 'forty-five hundred' if called directly.
114 def _convert_nnn_nl(val):
116 (mod, rem) = (val % 100, val // 100)
118 word = to_19_nl[rem] + ' Honderd'
122 word = word + _convert_nn_nl(mod)
125 def dutch_number(val):
127 return _convert_nn_nl(val)
129 return _convert_nnn_nl(val)
130 for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_nl))):
135 ret = _convert_nnn_nl(l) + ' ' + denom_nl[didx]
137 ret = ret + ', ' + dutch_number(r)
140 def amount_to_text_nl(number, currency):
141 number = '%.2f' % number
142 units_name = currency
143 list = str(number).split('.')
144 start_word = dutch_number(int(list[0]))
145 end_word = dutch_number(int(list[1]))
146 cents_number = int(list[1])
147 cents_name = (cents_number > 1) and 'cent' or 'cent'
148 final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
151 #-------------------------------------------------------------
153 #-------------------------------------------------------------
155 _translate_funcs = {'fr' : amount_to_text_fr, 'nl' : amount_to_text_nl}
157 def add_amount_to_text_function(lang, func):
158 _translate_funcs[lang] = func
160 #TODO: we should use the country AND language (ex: septante VS soixante dix)
161 #TODO: we should use en by default, but the translation func is yet to be implemented
162 def amount_to_text(nbr, lang='fr', currency='euro'):
164 Converts an integer to its textual representation, using the language set in the context if any.
166 1654: mille six cent cinquante-quatre.
170 # print "WARNING: number too large '%d', can't translate it!" % (nbr,)
173 if not _translate_funcs.has_key(lang):
175 print "WARNING: no translation function found for lang: '%s'" % (lang,)
176 #TODO: (default should be en) same as above
178 return _translate_funcs[lang](abs(nbr), currency)
180 if __name__=='__main__':
185 for i in range(1,200):
186 print i, ">>", amount_to_text(i, lang)
187 for i in range(200,999999,139):
188 print i, ">>", amount_to_text(i, lang)
190 print amount_to_text(int(argv[1]), lang)
193 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: