Created
April 20, 2022 10:07
-
-
Save edouard/978c5c2f11ba46a9c2e75654add4270c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
js: | |
products: | |
price: "Some text" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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