Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An incredibly simple i18n translation function using lodash `get` and `template` functions
import { get, template } from 'lodash';
const en = require('@/lang/en.json');
/**
* Translate a string from a preloaded JSON document
*/
const intl = function(key: string, values: Record<string, string | number> = {}): string {
const compiled = template(get(en, key), {
interpolate: /{([\s\S]+?)}/g, // {myVar}
});
return compiled(values);
};
export default intl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment