Last active
March 28, 2024 09:59
-
-
Save mderazon/9729626 to your computer and use it in GitHub Desktop.
JSON i18n
This file contains 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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really nice! Did you implemented a fallback language?