Skip to content

Instantly share code, notes, and snippets.

@drewdaemon
Created July 26, 2019 22:39
Show Gist options
  • Save drewdaemon/f15e14cd87519c070ad157e0c269b7eb to your computer and use it in GitHub Desktop.
Save drewdaemon/f15e14cd87519c070ad157e0c269b7eb to your computer and use it in GitHub Desktop.
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