Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JS i18n #1730

Merged
merged 4 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#= require handlebars
#= require spree/backend/translation

# 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
Handlebars.registerHelper "t", (key, options) ->
Spree.t(key, options.hash)


Handlebars.registerHelper "t", (key) ->
translation = resolveObject key, Spree.translations
return translation if translation

console.error "No translation found for #{key}."
key
Handlebars.registerHelper "human_attribute_name", (model, attr) ->
Spree.human_attribute_name(model, attr)

Handlebars.registerHelper "admin_url", ->
Spree.pathFor("admin")
29 changes: 29 additions & 0 deletions backend/app/assets/javascripts/spree/backend/translation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(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, options) {
options = (options || {});
if(options.scope) {
key = options.scope + "." + key;
}
var translation = resolveObject(key, Spree.translations);
if (translation) {
return translation;
} else {
console.warn("No translation found for " + key + ".");
return key;
}
}

Spree.human_attribute_name = function(model, attr) {
return Spree.t("activerecord.attributes." + model + '.' + attr);
}
})();
Original file line number Diff line number Diff line change
Expand Up @@ -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
%>
</script>
Expand Down