Skip to content

Instantly share code, notes, and snippets.

@arlanram
Last active April 14, 2021 19:21
Show Gist options
  • Save arlanram/4837823bd38e389b3b2a9c96e20e442f to your computer and use it in GitHub Desktop.
Save arlanram/4837823bd38e389b3b2a9c96e20e442f to your computer and use it in GitHub Desktop.
Pure localization for JS with unlimited JSON. simple, no need to require dependencies such i18, etc...
translate(key) {
let res = key.split('.').reduce((a, b) => a[b], t.data)
return res ? localStorage.getItem('translation') == 2 ? res[1] : res[0] : 'Translation not found'
}
//if you want to call tranlsate function from anywhere of your Vue application, you can use mixins
Vue.mixin({
methods: {
translate(key) {
let res = key.split('.').reduce((a, b) => a[b], t.data)
return res ? localStorage.getItem('translation') == 2 ? res[1] : res[0] : 'Translation not found'
}
}
})
//this function takes data from translations.json then displays in desired views
//json array translations can be extended as long as you need or queried from key-value storage (Mongo, Redis)
{
"data": {
"item": {
"name": ["Hello", "Konnichiwa"],
"desc": ["Naruto:", "Uzumaki"]
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment