Created
July 26, 2019 22:39
-
-
Save drewdaemon/f15e14cd87519c070ad157e0c269b7eb to your computer and use it in GitHub Desktop.
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
const i18n = { | |
create: function () { | |
this.interpolate = (template, fillers) => { | |
let ret = template; | |
for (const key of Object.keys(fillers)) { | |
ret = ret.replace(`{${key}}`, fillers[key]) | |
} | |
return ret; | |
}; | |
this.t = (lookupStr, fillers) => { | |
const lookupKeys = lookupStr.split('.'); | |
let translation = this.translations; | |
for (let i = 0; i < lookupKeys.length; i++) { | |
translation = translation[lookupKeys[i]]; | |
} | |
if (!fillers) { | |
return translation; | |
} else { | |
return this.interpolate(translation, fillers); | |
} | |
}; | |
this.translations = { | |
homePage: { | |
footer: { | |
copyright: 'Copyright {companyName} 2019' | |
} | |
} | |
}; | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment