Skip to content

Instantly share code, notes, and snippets.

@edouard
Created April 20, 2022 10:07
Show Gist options
  • Save edouard/978c5c2f11ba46a9c2e75654add4270c to your computer and use it in GitHub Desktop.
Save edouard/978c5c2f11ba46a9c2e75654add4270c to your computer and use it in GitHub Desktop.
// At the end of your layouts/application.html.haml file, convert the YAML Hash to a JS Hash:
:javascript
window.I18n = #{sanitize(js_translations.to_json)}
en:
js:
products:
price: "Some text"
/*
Use it by running t("products.price");
*/
window.t = function(key) {
if(!key){
return "N/A";
}
var keys = key.split(".");
var comp = window.I18n;
$(keys).each(function(_, value) {
if(comp){
comp = comp[value];
}
});
if(!comp && console){
console.debug("No translation found for key: " + key);
return "N/A";
}
return comp;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment