From 68ccc19c70981157234802cdbc136c82f5a956d2 Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 27 Jun 2014 11:45:34 +0200 Subject: [PATCH] allow to configure currency (closes foodcoops#258) --- app/controllers/application_controller.rb | 14 +++++++++++++- app/views/admin/configs/_tab_language.html.haml | 5 +++++ config/app_config.yml.SAMPLE | 3 +++ config/initializers/currency_display.rb | 7 +++++++ config/locales/en.yml | 4 ++++ lib/foodsoft_config.rb | 2 ++ 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 config/initializers/currency_display.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6c14f1c9..3a01550a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base protect_from_forgery before_filter :select_foodcoop, :authenticate, :store_controller, :items_per_page after_filter :remove_controller - around_filter :set_time_zone + around_filter :set_time_zone, :set_currency # Returns the controller handling the current request. @@ -180,4 +180,16 @@ class ApplicationController < ActionController::Base Time.zone = old_time_zone end + # Set currency according to foodcoop preference. + # @see #set_time_zone + def set_currency + old_currency = ::I18n.t('number.currency.format.unit') + new_currency = FoodsoftConfig[:currency_unit] || '' + new_currency += "\u202f" if FoodsoftConfig[:currency_space] + ::I18n.backend.store_translations(::I18n.locale, number: {currency: {format: {unit: new_currency}}}) + yield + ensure + ::I18n.backend.store_translations(::I18n.locale, number: {currency: {format: {unit: old_currency}}}) + end + end diff --git a/app/views/admin/configs/_tab_language.html.haml b/app/views/admin/configs/_tab_language.html.haml index c2c956a4..55cb3cfd 100644 --- a/app/views/admin/configs/_tab_language.html.haml +++ b/app/views/admin/configs/_tab_language.html.haml @@ -2,3 +2,8 @@ collection: I18n.available_locales.map {|l| [t("simple_form.options.settings.profile.language.#{l}"), l]} = config_input form, :ignore_browser_locale, as: :boolean = config_input form, :time_zone, as: :time_zone, include_blank: true, input_html: {class: 'input-xlarge'} += config_input form, :currency_unit do + = config_input_field form, :currency_unit, class: 'input-mini' + %label.inline + = config_input_label form, :currency_space + = config_input_field form, :currency_space, as: :boolean diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index a485c6e2..a455f113 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -39,6 +39,9 @@ default: &defaults #ignore_browser_locale: false # Default timezone, e.g. UTC, Amsterdam, Berlin, etc. #time_zone: Berlin + # Currency symbol, and whether to add a whitespace after the unit. + #currency_unit: € + #currency_space: true # price markup in percent price_markup: 2.0 diff --git a/config/initializers/currency_display.rb b/config/initializers/currency_display.rb new file mode 100644 index 00000000..191f255c --- /dev/null +++ b/config/initializers/currency_display.rb @@ -0,0 +1,7 @@ +# remove all currency translations, so that we can set the default language and +# have it shown in all other languages too +::I18n.available_locales.each do |locale| + unless locale == ::I18n.default_locale + ::I18n.backend.store_translations(locale, number: {currency: {format: {unit: nil}}}) + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 88374665..43ba4e7d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -450,6 +450,8 @@ en: contact: email: General contact email address, shown on website as well as some forms. street: Address, typically this will be your delivery and pick-up location. + currency_unit: Currency symbol for displaying prices. + currency_space: Whether to add whitespace after the currency symbol. homepage: Website of your foodcoop. help_url: Documentation website. applepear_url: Website where the apple and pear system for tasks is explained. @@ -481,6 +483,8 @@ en: country: Country email: Email phone: Phone + currency_unit: Currency + currency_space: add space homepage: Homepage help_url: Documentation URL applepear_url: Apple system URL diff --git a/lib/foodsoft_config.rb b/lib/foodsoft_config.rb index d6d8257c..eb238a7a 100644 --- a/lib/foodsoft_config.rb +++ b/lib/foodsoft_config.rb @@ -235,6 +235,8 @@ class FoodsoftConfig use_apple_points: true, # English is the default language, and this makes it show up as default. default_locale: 'en', + currency_unit: '€', + currency_space: true, foodsoft_url: 'https://github.com/foodcoops/foodsoft', # The following keys cannot, by default, be set by foodcoops themselves. protected: {