Gem currency_to_words
Provides a view helper for Rails that translate a currency amount to words (eg. to_words(200)
=> ‘dvě stě’).
This helper comes with two supported locales (Czech and Polish) but it is possible and easy to implement your own (see “Implementing a new locale”).
Installation
Add the gem to your Gemfile.
gem 'currency_to_words'
Usage
Install gem and create locale file in your project where you want to provide translation from currency to words. You can use locale file from I18n files below section or create your own.
Examples
In your views write for example:
currency_to_words(235.58)
I18n files
Czech locale
cs:
number:
currency_in_words:
format: '%n'
options_precision:
precision: 2
delimiter: ''
significant: false
strip_insignificant_zeros: false
separator: '.'
raise: false
connector: ' a '
negative_format: '(%n)'
skip_and: true
ones: [nula, jedna, dvě, tři, čtyři, pět, šest, sedm, osm, devět]
teens: [deset, jedenáct, dvanáct, třináct, čtrnáct, patnáct, šestnáct, sedmnáct, osmnáct, devatenáct]
tens: [nil, nil,'dvacet','třicet','čtyřicet','padesát','šedesát','sedmdesát', 'osmdesát','devadesát']
hundreds: [sto, dvě stě, tři sta, čtyři sta, pět set, šest set, sedm set, osm set, devět set]
thousands_ones: [_, jeden, dva, tři, čtyři, pět, šest, sedm, osm, devět]
megs: [_, thousand, milion, bilion, trilion]
thousand:
one: 'tisíc'
few: 'tisíce'
other: 'tisíc'
milion:
one: 'milion'
few: 'miliony'
other: 'milionů'
bilion:
one: 'bilion'
few: 'biliony'
other: 'bilionů'
trilion:
one: 'trilion'
few: 'triliony'
other: 'trilionů'
currencies:
integer:
one: 'koruna'
few: 'koruny'
other: 'korun'
decimal:
one: 'haléř'
few: 'haléře'
other: 'haléřů'
Missing locale?
If you need locale that is not supported yet, you can easily add one. All you need to do is translated locale file and create class in /lib. Convention is that the class has to be named [locale]Currency.
So for example EnCurrency or FrCurrency. So override or duplicate the existing class and add diffrences of your language. Thats it!
Inspiration
This project is heavily inspirated by bcarrere/currency-in-words and uses blocks of code from this repository.
Reason for creating my own repository was requirement to use I18n gem for “currency to words” translation and also for settings. This means, that this gem can be configured from locale file and translation is not hardcoded to source of library.
Contribution
Did you created locales that is not supported here yet? Please write me e-mail if you want contribute at [email protected].
Copyright
Copyright © 2014 Jakub Malina. See LICENSE.txt for further details.