vant/src/utils/create/translate.ts
2020-12-13 12:29:30 +08:00

17 lines
511 B
TypeScript

import { get, isFunction } from '../base';
import { camelize } from '../format/string';
import locale from '../../locale';
export function createTranslate(name: string) {
const prefix = camelize(name) + '.';
return function (path: string, ...args: any[]): any {
const messages = locale.messages();
const message = get(messages, prefix + path) || get(messages, path);
return isFunction(message) ? message(...args) : message;
};
}
export type Translate = ReturnType<typeof createTranslate>;