Last active
April 14, 2021 19:21
-
-
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...
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
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) |
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
{ | |
"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