/currency_to_words

Provides a view helper for Rails that translate a currency amount to words.

Primary LanguageRubyOtherNOASSERTION

Currency to words

Provides a view helper for Rails that translate a currency amount to words (eg. to_words(200) => ‘dvě stě korun’).

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

Warning: Be sure that you are using correct syntax of arrays. I have had problem with “Argument error” after production server restart. Reason was that I did not have spaces in the arrays. So the correct syntax is …‘čtyřicet’, ‘padesát’… and so on.

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éřů'

Polish locale

pl:
  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
      delimiter: true
      ones: ['zero', 'jeden', 'dwa', 'trzy', 'cztery', 'pięć', 'sześć', 'siedem', 'osiem', 'dziewięć']
      teens: ['dziesięć', 'jedenaście', 'dwanaście', 'trzynaście', 'czternaście', 'pietnaście', 'szesnaście', 'siedemnaście', 'osiemnaście', 'dziewiętnaście']
      tens: [nil, nil, 'dwadzieścia', 'trzydzieści', 'czterdzieści', 'pięćdziesiąt', 'sześćdziesiąt', 'siedemdziesiąt', 'osiemdziesiąt', 'dziewięćdziesiąt']
      hundreds: ['sto', 'dwieście', 'trzysta', 'czterysta', 'pięćset', 'sześćset', 'siedemset', 'osiemset', 'dziewięćset']
      thousands_ones: ['', 'jeden', 'dwa', 'trzy', 'cztery', 'pięć', 'sześć', 'siedem', 'osiem', 'dziewięć']
      megs: ['', 'thousand', 'milion', 'bilion', 'trilion']
      thousand:
        one: 'tysiąc'
        few: 'tysiące'
        many: 'tysięcy'
        other: 'tysięcy'
      milion:
        one: 'milion'
        few: 'miliony'
        many: 'milionów'
        other: 'milionów'
      bilion:
        one: 'bilion'
        few: 'biliony'
        many: 'bilionów'
        other: 'bilionów'
      trilion:
        one: 'trilion'
        few: 'triliony'
        many: 'trilionów'
        other: 'trilionów'
      currencies:
        integer:
          one: 'złoty'
          few: 'złote'
          many: 'złotych'
          other: 'złotych'
        decimal:
          one: 'grosz'
          few: 'grosze'
          many: 'groszy'
          other: 'groszy'

Missing locale?

If you need locale that is not supported yet, you can easily add one. All you need to do is translate 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 majak108@gmail.com.

Copyright © 2014 Jakub Malina. See LICENSE.txt for further details.