-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Improve JS i18n #1730
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This little code for a huge improvement 🍾
I have one little nit regarding logging
if (translation) { | ||
return translation; | ||
} else { | ||
console.error("No translation found for " + key + "."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider to use a console.warn
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. Done.
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
This allows us to conveniently use the activerecord attribute name translations, which is our preferred convention for storing translations, from JS.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow. 👍
A few improvements to our (very basic) JS i18n
In c871eea @mtomov made the Handlebars
t
helper understand the dot-separated paths. 👍This extracts that functionality to a
Spree.t
, helper, so that JS doesn't need to dig through theSpree.translations
object manually.This also adds a
scope
option similar to the one ruby's18n.t
accepts. This is especially useful from handlebars templates, which can't themselves concatenate the string keys.Finally, this adds
Spree.human_attribute_name
, as well as ships the activerecord attribute translations to our JS.