Skip to content

Instantly share code, notes, and snippets.

@mderazon
Last active March 28, 2024 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mderazon/9729626 to your computer and use it in GitHub Desktop.
Save mderazon/9729626 to your computer and use it in GitHub Desktop.
JSON i18n
var traverse = require('traverse');
// filter json obj by language
var filter_language = function(language, obj) {
var result = traverse(obj).map(function(item) {
if (this.key === language) {
this.parent.update(item);
}
});
return result;
};
// example object in the multi-lingual format
var obj = {
"venue_id": "C001",
"venue_location": {
"en": "Baker St.",
"he": "רחוב האופה"
},
"venue_name": {
"en": "Aroma Coffee",
"he": "קפה ארומה"
}
};
console.log(filter_language('en', obj));
@stherrienaspnet
Copy link

This is really nice! Did you implemented a fallback language?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment