From 32e7bcb37d47ad7ed03f12472438b8c8a74f2361 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 20 Feb 2017 15:42:11 -0800 Subject: [PATCH 1/4] Extract Spree.t method from handlebars extensions Previously Martin Tomov updated the Handlebars "t" helper to behave more like the standard rails helper. Awesome! This commit extracts that behaviour so it is available to all javascripts via Spree.t --- .../app/assets/javascripts/spree/backend.js | 1 + .../backend/handlebars_extensions.coffee | 15 ++----------- .../javascripts/spree/backend/translation.js | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/translation.js diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js index d138cda46d8..5f29d353258 100644 --- a/backend/app/assets/javascripts/spree/backend.js +++ b/backend/app/assets/javascripts/spree/backend.js @@ -12,6 +12,7 @@ //= require solidus_admin/bootstrap //= require prism //= require spree +//= require spree/backend/translation //= require spree/backend/backbone-overrides //= require spree/backend/spree-select2 // diff --git a/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee b/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee index 14e6b9d7279..be2929043bf 100644 --- a/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee +++ b/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee @@ -1,19 +1,8 @@ #= require handlebars - -# Resolves string keys with dots in a deeply nested object -# http://stackoverflow.com/a/22129960/4405214 -resolveObject = (path, obj) -> - path.split('.').reduce ((prev, curr) -> - if prev then prev[curr] else undefined - ), obj || self - +#= require spree/backend/translation Handlebars.registerHelper "t", (key) -> - translation = resolveObject key, Spree.translations - return translation if translation - - console.error "No translation found for #{key}." - key + Spree.t(key) Handlebars.registerHelper "admin_url", -> Spree.pathFor("admin") diff --git a/backend/app/assets/javascripts/spree/backend/translation.js b/backend/app/assets/javascripts/spree/backend/translation.js new file mode 100644 index 00000000000..470ec95b7f5 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/translation.js @@ -0,0 +1,21 @@ +(function() { + // Resolves string keys with dots in a deeply nested object + // http://stackoverflow.com/a/22129960/4405214 + var resolveObject = function(path, obj) { + return path + .split('.') + .reduce(function(prev, curr) { + return prev && prev[curr]; + }, obj || self); + } + + Spree.t = function(key) { + var translation = resolveObject(key, Spree.translations); + if (translation) { + return translation; + } else { + console.error("No translation found for " + key + "."); + return key; + } + } +})(); From 2f0b6df7db79d0ada53004749d779df95db21748 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 21 Feb 2017 17:07:06 -0800 Subject: [PATCH 2/4] Add Spree.human_attribute_name This allows us to conveniently use the activerecord attribute name translations, which is our preferred convention for storing translations, from JS. --- .../javascripts/spree/backend/handlebars_extensions.coffee | 3 +++ backend/app/assets/javascripts/spree/backend/translation.js | 4 ++++ backend/app/views/spree/admin/shared/_translations.html.erb | 1 + 3 files changed, 8 insertions(+) diff --git a/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee b/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee index be2929043bf..016f543a37a 100644 --- a/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee +++ b/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee @@ -4,5 +4,8 @@ Handlebars.registerHelper "t", (key) -> Spree.t(key) +Handlebars.registerHelper "human_attribute_name", (model, attr) -> + Spree.human_attribute_name(model, attr) + Handlebars.registerHelper "admin_url", -> Spree.pathFor("admin") diff --git a/backend/app/assets/javascripts/spree/backend/translation.js b/backend/app/assets/javascripts/spree/backend/translation.js index 470ec95b7f5..6e2c21a317d 100644 --- a/backend/app/assets/javascripts/spree/backend/translation.js +++ b/backend/app/assets/javascripts/spree/backend/translation.js @@ -18,4 +18,8 @@ return key; } } + + Spree.human_attribute_name = function(model, attr) { + return Spree.t("activerecord.attributes." + model + '.' + attr); + } })(); diff --git a/backend/app/views/spree/admin/shared/_translations.html.erb b/backend/app/views/spree/admin/shared/_translations.html.erb index c0b2a1c88ad..7f6493d2b30 100644 --- a/backend/app/views/spree/admin/shared/_translations.html.erb +++ b/backend/app/views/spree/admin/shared/_translations.html.erb @@ -18,6 +18,7 @@ :item_stock_placeholder => Spree.t(:choose_location), :month_names => I18n.t(:month_names, :scope => :date).reject(&:blank?), :currency_separator => I18n.t('number.currency.format.separator'), + :activerecord => I18n.t('activerecord') }).to_json %> From d4aa3652b1ea665f6e640636da89e53cf257a771 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 21 Feb 2017 13:43:28 -0800 Subject: [PATCH 3/4] Add scope option to JS Spree.t This adds a scope option to our JS Spree.t similar to the one ruby's `I18n.t` accepts. This is especially useful from handlebars templates, which can't themselves concatenate the string keys. --- .../javascripts/spree/backend/handlebars_extensions.coffee | 4 ++-- backend/app/assets/javascripts/spree/backend/translation.js | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee b/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee index 016f543a37a..abb4f1a0648 100644 --- a/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee +++ b/backend/app/assets/javascripts/spree/backend/handlebars_extensions.coffee @@ -1,8 +1,8 @@ #= require handlebars #= require spree/backend/translation -Handlebars.registerHelper "t", (key) -> - Spree.t(key) +Handlebars.registerHelper "t", (key, options) -> + Spree.t(key, options.hash) Handlebars.registerHelper "human_attribute_name", (model, attr) -> Spree.human_attribute_name(model, attr) diff --git a/backend/app/assets/javascripts/spree/backend/translation.js b/backend/app/assets/javascripts/spree/backend/translation.js index 6e2c21a317d..0b9febeace5 100644 --- a/backend/app/assets/javascripts/spree/backend/translation.js +++ b/backend/app/assets/javascripts/spree/backend/translation.js @@ -9,7 +9,11 @@ }, obj || self); } - Spree.t = function(key) { + Spree.t = function(key, options) { + options = (options || {}); + if(options.scope) { + key = options.scope + "." + key; + } var translation = resolveObject(key, Spree.translations); if (translation) { return translation; From 396a996b4c49e3230fea28590bcebeeb461b4622 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 22 Feb 2017 09:22:11 -0800 Subject: [PATCH 4/4] Use console.warn instead of console.error --- backend/app/assets/javascripts/spree/backend/translation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/assets/javascripts/spree/backend/translation.js b/backend/app/assets/javascripts/spree/backend/translation.js index 0b9febeace5..e3b75dd0840 100644 --- a/backend/app/assets/javascripts/spree/backend/translation.js +++ b/backend/app/assets/javascripts/spree/backend/translation.js @@ -18,7 +18,7 @@ if (translation) { return translation; } else { - console.error("No translation found for " + key + "."); + console.warn("No translation found for " + key + "."); return key; } }