From 64a597c68907a17b7e8d36366fa729bb9e656fbb Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 4 Oct 2013 14:53:22 +0200 Subject: [PATCH] use i18n-js --- .gitignore | 5 +++-- Gemfile | 2 +- Gemfile.lock | 8 ++++++++ app/assets/javascripts/application.js | 2 ++ app/assets/javascripts/ordering.js | 21 ++++----------------- app/views/group_orders/_form.html.haml | 1 - app/views/layouts/_header.html.haml | 6 ++++-- config/environments/development.rb.SAMPLE | 5 ++++- config/environments/test.rb | 3 +++ config/i18n-js.yml | 4 ++++ 10 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 config/i18n-js.yml diff --git a/.gitignore b/.gitignore index d26b8ce7..a3389c40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ log/*.log tmp/**/* -config/*.yml +config/app_config.yml +config/database.yml config/initializers/secret_token.rb db/*.sqlite3 nbproject/ @@ -16,4 +17,4 @@ doc/app/ Capfile config/deploy.rb config/deploy/* -.localeapp \ No newline at end of file +.localeapp diff --git a/Gemfile b/Gemfile index e80314ed..e98aa4ea 100644 --- a/Gemfile +++ b/Gemfile @@ -19,7 +19,7 @@ end gem 'jquery-rails' gem 'select2-rails' gem 'bootstrap-datepicker-rails' - +gem 'i18n-js', git: 'git://github.com/fnando/i18n-js.git' # to avoid US-ASCII js.erb error gem 'mysql2' gem 'prawn' diff --git a/Gemfile.lock b/Gemfile.lock index c816b8c4..824f2cae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,13 @@ GIT specs: localize_input (0.1.0) +GIT + remote: git://github.com/fnando/i18n-js.git + revision: eab4137f83777963f0ebe6960704a7f64fd8911d + specs: + i18n-js (2.1.2) + i18n + GIT remote: git://github.com/technoweenie/acts_as_versioned.git revision: 63b1fc8529d028fae632fe80ec0cb25df56cd76b @@ -321,6 +328,7 @@ DEPENDENCIES factory_girl_rails (~> 4.0) faker haml-rails + i18n-js! i18n-spec inherited_resources jquery-rails diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index c6d6e36a..af02efcc 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -9,6 +9,8 @@ //= require bootstrap-datepicker/locales/bootstrap-datepicker.nl //= require jquery.observe_field //= require rails.validations +//= require i18n +//= require i18n/translations //= require_self //= require ordering //= require stupidtable diff --git a/app/assets/javascripts/ordering.js b/app/assets/javascripts/ordering.js index 159370f2..3f80d40f 100644 --- a/app/assets/javascripts/ordering.js +++ b/app/assets/javascripts/ordering.js @@ -7,9 +7,6 @@ var modified = false // indicates if anything has been clicked on this page var groupBalance = 0; // available group money -var currencySeparator = "."; // default decimal separator -var currencyPrecision = 2; // default digits behind comma -var currencyUnit = "€"; // default currency var minimumBalance = 0; // minimum group balance for the order to be succesful var toleranceIsCostly = true; // default tolerance behaviour var isStockit = false; // Wheter the order is from stock oder normal supplier @@ -23,12 +20,6 @@ var toleranceOthers = new Array(); var itemsAllocated = new Array(); // how many items the group has been allocated and should definitely get var quantityAvailable = new Array(); // stock_order. how many items are currently in stock -function setCurrencyFormat(separator, precision, unit) { - currencySeparator = separator; - currencyPrecision = precision; - currencyUnit = unit; -} - function setToleranceBehaviour(value) { toleranceIsCostly = value; } @@ -124,7 +115,7 @@ function update(item, quantity, tolerance) { } else { itemTotal[item] = price[item] * (Number(quantity)); } - $('#price_' + item + '_display').html(asMoney(itemTotal[item])); + $('#price_' + item + '_display').html(I18n.l("currency", itemTotal[item])); // update missing units var missing_units = unit[item] - (((quantityOthers[item] + Number(quantity)) % unit[item]) + Number(tolerance) + toleranceOthers[item]) @@ -137,10 +128,6 @@ function update(item, quantity, tolerance) { updateBalance(); } -function asMoney(amount) { - return String(amount.toFixed(currencyPrecision)).replace(/\./, currencySeparator) + ' ' + currencyUnit; -} - function calcUnits(unitSize, quantity, tolerance) { var units = Math.floor(quantity / unitSize) var remainder = quantity % unitSize @@ -158,10 +145,10 @@ function updateBalance() { for (i in itemTotal) { total += itemTotal[i]; } - $('#total_price').html(asMoney(total)); + $('#total_price').html(I18n.l("currency", total)); var balance = groupBalance - total; - $('#new_balance').html(asMoney(balance)); - $('#total_balance').val(asMoney(balance)); + $('#new_balance').html(I18n.l("currency", balance)); + $('#total_balance').val(I18n.l("currency", balance)); // determine bgcolor and submit button state according to balance var bgcolor = ''; if (balance < minimumBalance) { diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index 2bd2f30b..9e4007f3 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -3,7 +3,6 @@ $(function() { #{data_to_js(@ordering_data)} setGroupBalance(#{@ordering_data[:available_funds]}); - setCurrencyFormat("#{t('number.currency.format.separator')}", #{t('number.currency.format.precision')}, "#{t('number.currency.format.unit')}"); setMinimumBalance(#{FoodsoftConfig[:minimum_balance] or 0}); setToleranceBehaviour(#{FoodsoftConfig[:tolerance_is_costly]}); setStockit(#{@order.stockit?}); diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index dfd5f2f5..b3a4e30c 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -22,7 +22,9 @@ Javascripts \================================================== / Placed at the end of the document so the pages load faster - :javascript - I18n = {locale: '#{j(I18n.locale.to_s)}'} = javascript_include_tag "application" + :javascript + I18n.defaultLocale = "#{I18n.default_locale}"; + I18n.locale = "#{I18n.locale}"; + I18n.fallbacks = true; = yield(:javascript) diff --git a/config/environments/development.rb.SAMPLE b/config/environments/development.rb.SAMPLE index 826028ba..ac1781e6 100644 --- a/config/environments/development.rb.SAMPLE +++ b/config/environments/development.rb.SAMPLE @@ -35,6 +35,9 @@ Foodsoft::Application.configure do # Expands the lines which load the assets config.assets.debug = true + # Required for i18n-js + config.assets.initialize_on_precompile = true + # Configure hostname for action mailer config.action_mailer.default_url_options = { host: 'localhost:3000' } @@ -42,4 +45,4 @@ Foodsoft::Application.configure do # Mailcatcher can be installed by gem install mailcatcher config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: "localhost", port: 1025 } -end \ No newline at end of file +end diff --git a/config/environments/test.rb b/config/environments/test.rb index 30c06137..b67c0285 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -11,6 +11,9 @@ Foodsoft::Application.configure do config.serve_static_assets = true config.static_cache_control = "public, max-age=3600" + # Required for i18n-js + config.assets.initialize_on_precompile = true + # Log error messages when you accidentally call methods on nil config.whiny_nils = true diff --git a/config/i18n-js.yml b/config/i18n-js.yml new file mode 100644 index 00000000..76af6fda --- /dev/null +++ b/config/i18n-js.yml @@ -0,0 +1,4 @@ +# only serve selected strings for i18n-js to keep filesize down +translations: +- file: 'app/assets/javascripts/i18n/translations.js' + only: ['*.js.*', '*.number.*', '*.date.formats.*']