Skip to content

Instantly share code, notes, and snippets.

@xiCO2k
Last active September 9, 2021 15:55
Show Gist options
  • Save xiCO2k/219e34681cdad4a498867715de47bf34 to your computer and use it in GitHub Desktop.
Save xiCO2k/219e34681cdad4a498867715de47bf34 to your computer and use it in GitHub Desktop.
vue-i18n CustomFormatter using the laravel replacement logic
// const i18n = new VueI18n({
// formatter: new CustomFormatter(),
// });
export default class CustomFormatter {
interpolate (message, values) {
const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1);
Object.entries(values || []).forEach(([key, value]) => {
value = '' + value;
message = message
.replace(`:${key}`, value)
.replace(`:${key.toUpperCase()}`, value.toUpperCase())
.replace(`:${capitalize(key)}`, capitalize(value));
});
return [message];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment