How to store user's preferred currency format in Rails application? -
i want users of international, multilingual rails app able set own currency , currency format.
right now, using translation files achieve this:
de: currency: format: format: "%n %u" unit: "€" separator: "," delimiter: "."
however, not flexible enough. e.g. if user wants use euro wants €
symbol appear before amount?
so best way store user's currency formatting preferences , use throughout application?
i figured store user's preferences in database this:
user.preferences.format = "%n %u" user.preferences.unit = "€" user.preferences.separator = "," user.preferences.delimiter = "." user.preferences.save
and then, in view:
number_to_currency(123.50, unit: user.preferences.unit, separator: user.preferences.separator, delimiter: user.preferences.delimiter, format: user.preferences.format)
is common rails practice? might better approach?
thanks help.
i think you've made better use individual’s preferences/settings instead of app settings.
but there still concern in new arrangement.
what if user want change currency, euro canada dollar? not easy in app because current amount in euro. need convert amount can dollar , take currency rate consideration adds complexity. , amount not consistent. example, if want change euro after few days, it's meet new euro amount.
my suggestions:
use consistent amount , currency everything. price us$ 19. user can have own currency, of course, converted base amount dollar.
use separate settings custom currency , save them db. you've done that. , there nice gem efficiently: https://github.com/ledermann/rails-settings
Comments
Post a Comment