更新
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
/*!
|
||||
* @intlify/runtime v9.1.9
|
||||
* (c) 2021 kazuya kawaguchi
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var shared = require('@intlify/shared');
|
||||
|
||||
const DEFAULT_MODIFIER = (str) => str;
|
||||
const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
|
||||
const DEFAULT_MESSAGE_DATA_TYPE = 'text';
|
||||
const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : values.join('');
|
||||
const DEFAULT_INTERPOLATE = shared.toDisplayString;
|
||||
function pluralDefault(choice, choicesLength) {
|
||||
choice = Math.abs(choice);
|
||||
if (choicesLength === 2) {
|
||||
// prettier-ignore
|
||||
return choice
|
||||
? choice > 1
|
||||
? 1
|
||||
: 0
|
||||
: 1;
|
||||
}
|
||||
return choice ? Math.min(choice, 2) : 0;
|
||||
}
|
||||
function getPluralIndex(options) {
|
||||
// prettier-ignore
|
||||
const index = shared.isNumber(options.pluralIndex)
|
||||
? options.pluralIndex
|
||||
: -1;
|
||||
// prettier-ignore
|
||||
return options.named && (shared.isNumber(options.named.count) || shared.isNumber(options.named.n))
|
||||
? shared.isNumber(options.named.count)
|
||||
? options.named.count
|
||||
: shared.isNumber(options.named.n)
|
||||
? options.named.n
|
||||
: index
|
||||
: index;
|
||||
}
|
||||
function normalizeNamed(pluralIndex, props) {
|
||||
if (!props.count) {
|
||||
props.count = pluralIndex;
|
||||
}
|
||||
if (!props.n) {
|
||||
props.n = pluralIndex;
|
||||
}
|
||||
}
|
||||
function createMessageContext(options = {}) {
|
||||
const locale = options.locale;
|
||||
const pluralIndex = getPluralIndex(options);
|
||||
const pluralRule = shared.isObject(options.pluralRules) &&
|
||||
shared.isString(locale) &&
|
||||
shared.isFunction(options.pluralRules[locale])
|
||||
? options.pluralRules[locale]
|
||||
: pluralDefault;
|
||||
const orgPluralRule = shared.isObject(options.pluralRules) &&
|
||||
shared.isString(locale) &&
|
||||
shared.isFunction(options.pluralRules[locale])
|
||||
? pluralDefault
|
||||
: undefined;
|
||||
const plural = (messages) => messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
||||
const _list = options.list || [];
|
||||
const list = (index) => _list[index];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const _named = options.named || {};
|
||||
shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
|
||||
const named = (key) => _named[key];
|
||||
// TODO: need to design resolve message function?
|
||||
function message(key) {
|
||||
// prettier-ignore
|
||||
const msg = shared.isFunction(options.messages)
|
||||
? options.messages(key)
|
||||
: shared.isObject(options.messages)
|
||||
? options.messages[key]
|
||||
: false;
|
||||
return !msg
|
||||
? options.parent
|
||||
? options.parent.message(key) // resolve from parent messages
|
||||
: DEFAULT_MESSAGE
|
||||
: msg;
|
||||
}
|
||||
const _modifier = (name) => options.modifiers
|
||||
? options.modifiers[name]
|
||||
: DEFAULT_MODIFIER;
|
||||
const normalize = shared.isPlainObject(options.processor) && shared.isFunction(options.processor.normalize)
|
||||
? options.processor.normalize
|
||||
: DEFAULT_NORMALIZE;
|
||||
const interpolate = shared.isPlainObject(options.processor) &&
|
||||
shared.isFunction(options.processor.interpolate)
|
||||
? options.processor.interpolate
|
||||
: DEFAULT_INTERPOLATE;
|
||||
const type = shared.isPlainObject(options.processor) && shared.isString(options.processor.type)
|
||||
? options.processor.type
|
||||
: DEFAULT_MESSAGE_DATA_TYPE;
|
||||
const ctx = {
|
||||
["list" /* LIST */]: list,
|
||||
["named" /* NAMED */]: named,
|
||||
["plural" /* PLURAL */]: plural,
|
||||
["linked" /* LINKED */]: (key, modifier) => {
|
||||
// TODO: should check `key`
|
||||
const msg = message(key)(ctx);
|
||||
return shared.isString(modifier) ? _modifier(modifier)(msg) : msg;
|
||||
},
|
||||
["message" /* MESSAGE */]: message,
|
||||
["type" /* TYPE */]: type,
|
||||
["interpolate" /* INTERPOLATE */]: interpolate,
|
||||
["normalize" /* NORMALIZE */]: normalize
|
||||
};
|
||||
return ctx;
|
||||
}
|
||||
|
||||
exports.DEFAULT_MESSAGE_DATA_TYPE = DEFAULT_MESSAGE_DATA_TYPE;
|
||||
exports.createMessageContext = createMessageContext;
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
/*!
|
||||
* @intlify/runtime v9.1.9
|
||||
* (c) 2021 kazuya kawaguchi
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var shared = require('@intlify/shared');
|
||||
|
||||
const DEFAULT_MODIFIER = (str) => str;
|
||||
const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
|
||||
const DEFAULT_MESSAGE_DATA_TYPE = 'text';
|
||||
const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : values.join('');
|
||||
const DEFAULT_INTERPOLATE = shared.toDisplayString;
|
||||
function pluralDefault(choice, choicesLength) {
|
||||
choice = Math.abs(choice);
|
||||
if (choicesLength === 2) {
|
||||
// prettier-ignore
|
||||
return choice
|
||||
? choice > 1
|
||||
? 1
|
||||
: 0
|
||||
: 1;
|
||||
}
|
||||
return choice ? Math.min(choice, 2) : 0;
|
||||
}
|
||||
function getPluralIndex(options) {
|
||||
// prettier-ignore
|
||||
const index = shared.isNumber(options.pluralIndex)
|
||||
? options.pluralIndex
|
||||
: -1;
|
||||
// prettier-ignore
|
||||
return options.named && (shared.isNumber(options.named.count) || shared.isNumber(options.named.n))
|
||||
? shared.isNumber(options.named.count)
|
||||
? options.named.count
|
||||
: shared.isNumber(options.named.n)
|
||||
? options.named.n
|
||||
: index
|
||||
: index;
|
||||
}
|
||||
function normalizeNamed(pluralIndex, props) {
|
||||
if (!props.count) {
|
||||
props.count = pluralIndex;
|
||||
}
|
||||
if (!props.n) {
|
||||
props.n = pluralIndex;
|
||||
}
|
||||
}
|
||||
function createMessageContext(options = {}) {
|
||||
const locale = options.locale;
|
||||
const pluralIndex = getPluralIndex(options);
|
||||
const pluralRule = shared.isObject(options.pluralRules) &&
|
||||
shared.isString(locale) &&
|
||||
shared.isFunction(options.pluralRules[locale])
|
||||
? options.pluralRules[locale]
|
||||
: pluralDefault;
|
||||
const orgPluralRule = shared.isObject(options.pluralRules) &&
|
||||
shared.isString(locale) &&
|
||||
shared.isFunction(options.pluralRules[locale])
|
||||
? pluralDefault
|
||||
: undefined;
|
||||
const plural = (messages) => messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
||||
const _list = options.list || [];
|
||||
const list = (index) => _list[index];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const _named = options.named || {};
|
||||
shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
|
||||
const named = (key) => _named[key];
|
||||
// TODO: need to design resolve message function?
|
||||
function message(key) {
|
||||
// prettier-ignore
|
||||
const msg = shared.isFunction(options.messages)
|
||||
? options.messages(key)
|
||||
: shared.isObject(options.messages)
|
||||
? options.messages[key]
|
||||
: false;
|
||||
return !msg
|
||||
? options.parent
|
||||
? options.parent.message(key) // resolve from parent messages
|
||||
: DEFAULT_MESSAGE
|
||||
: msg;
|
||||
}
|
||||
const _modifier = (name) => options.modifiers
|
||||
? options.modifiers[name]
|
||||
: DEFAULT_MODIFIER;
|
||||
const normalize = shared.isPlainObject(options.processor) && shared.isFunction(options.processor.normalize)
|
||||
? options.processor.normalize
|
||||
: DEFAULT_NORMALIZE;
|
||||
const interpolate = shared.isPlainObject(options.processor) &&
|
||||
shared.isFunction(options.processor.interpolate)
|
||||
? options.processor.interpolate
|
||||
: DEFAULT_INTERPOLATE;
|
||||
const type = shared.isPlainObject(options.processor) && shared.isString(options.processor.type)
|
||||
? options.processor.type
|
||||
: DEFAULT_MESSAGE_DATA_TYPE;
|
||||
const ctx = {
|
||||
["list" /* LIST */]: list,
|
||||
["named" /* NAMED */]: named,
|
||||
["plural" /* PLURAL */]: plural,
|
||||
["linked" /* LINKED */]: (key, modifier) => {
|
||||
// TODO: should check `key`
|
||||
const msg = message(key)(ctx);
|
||||
return shared.isString(modifier) ? _modifier(modifier)(msg) : msg;
|
||||
},
|
||||
["message" /* MESSAGE */]: message,
|
||||
["type" /* TYPE */]: type,
|
||||
["interpolate" /* INTERPOLATE */]: interpolate,
|
||||
["normalize" /* NORMALIZE */]: normalize
|
||||
};
|
||||
return ctx;
|
||||
}
|
||||
|
||||
exports.DEFAULT_MESSAGE_DATA_TYPE = DEFAULT_MESSAGE_DATA_TYPE;
|
||||
exports.createMessageContext = createMessageContext;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import { Path } from '@intlify/message-resolver';
|
||||
|
||||
export declare type CoreMissingType = 'translate' | 'datetime format' | 'number format';
|
||||
|
||||
export declare function createMessageContext<T = string, N = {}>(options?: MessageContextOptions<T, N>): MessageContext<T>;
|
||||
|
||||
export declare const DEFAULT_MESSAGE_DATA_TYPE = "text";
|
||||
|
||||
declare type ExtractToStringFunction<T> = T[ExtractToStringKey<T>];
|
||||
|
||||
declare type ExtractToStringKey<T> = Extract<keyof T, 'toString'>;
|
||||
|
||||
/** @VueI18nGeneral */
|
||||
export declare type FallbackLocale = Locale | Locale[] | {
|
||||
[locale in string]: Locale[];
|
||||
} | false;
|
||||
|
||||
/** @VueI18nGeneral */
|
||||
export declare type LinkedModifiers<T = string> = {
|
||||
[key: string]: LinkedModify<T>;
|
||||
};
|
||||
|
||||
export declare type LinkedModify<T = string> = (value: T) => MessageType<T>;
|
||||
|
||||
/** @VueI18nGeneral */
|
||||
export declare type Locale = string;
|
||||
|
||||
export declare interface MessageContext<T = string> {
|
||||
list(index: number): unknown;
|
||||
named(key: string): unknown;
|
||||
plural(messages: T[]): T;
|
||||
linked(key: Path, modifier?: string): MessageType<T>;
|
||||
message(key: Path): MessageFunction<T>;
|
||||
type: string;
|
||||
interpolate: MessageInterpolate<T>;
|
||||
normalize: MessageNormalize<T>;
|
||||
}
|
||||
|
||||
export declare interface MessageContextOptions<T = string, N = {}> {
|
||||
parent?: MessageContext<T>;
|
||||
locale?: string;
|
||||
list?: unknown[];
|
||||
named?: NamedValue<N>;
|
||||
modifiers?: LinkedModifiers<T>;
|
||||
pluralIndex?: number;
|
||||
pluralRules?: PluralizationRules;
|
||||
messages?: MessageFunctions<T> | MessageResolveFunction<T>;
|
||||
processor?: MessageProcessor<T>;
|
||||
}
|
||||
|
||||
export declare type MessageFunction<T = string> = MessageFunctionCallable | MessageFunctionInternal<T>;
|
||||
|
||||
export declare type MessageFunctionCallable = <T = string>(ctx: MessageContext<T>) => MessageType<T>;
|
||||
|
||||
export declare type MessageFunctionInternal<T = string> = {
|
||||
(ctx: MessageContext<T>): MessageType<T>;
|
||||
key?: string;
|
||||
locale?: string;
|
||||
source?: string;
|
||||
};
|
||||
|
||||
export declare type MessageFunctions<T = string> = Record<string, MessageFunction<T>>;
|
||||
|
||||
export declare type MessageInterpolate<T = string> = (val: unknown) => MessageType<T>;
|
||||
|
||||
export declare type MessageNormalize<T = string> = (values: MessageType<string | T>[]) => MessageType<T | T[]>;
|
||||
|
||||
export declare interface MessageProcessor<T = string> {
|
||||
type?: string;
|
||||
interpolate?: MessageInterpolate<T>;
|
||||
normalize?: MessageNormalize<T>;
|
||||
}
|
||||
|
||||
export declare type MessageResolveFunction<T = string> = (key: string) => MessageFunction<T>;
|
||||
|
||||
export declare type MessageType<T = string> = T extends string ? string : StringConvertable<T>;
|
||||
|
||||
/** @VueI18nGeneral */
|
||||
export declare type NamedValue<T = {}> = T & Record<string, unknown>;
|
||||
|
||||
export declare type PluralizationProps = {
|
||||
n?: number;
|
||||
count?: number;
|
||||
};
|
||||
|
||||
export declare type PluralizationRule = (choice: number, choicesLength: number, orgRule?: PluralizationRule) => number;
|
||||
|
||||
/** @VueI18nGeneral */
|
||||
export declare type PluralizationRules = {
|
||||
[locale: string]: PluralizationRule;
|
||||
};
|
||||
|
||||
declare type StringConvertable<T> = ExtractToStringKey<T> extends never ? unknown : ExtractToStringFunction<T> extends (...args: any) => string ? T : unknown;
|
||||
|
||||
export { }
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
/*!
|
||||
* @intlify/runtime v9.1.9
|
||||
* (c) 2021 kazuya kawaguchi
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
import { isNumber, isObject, isString, isFunction, isPlainObject, toDisplayString } from '@intlify/shared';
|
||||
|
||||
const DEFAULT_MODIFIER = (str) => str;
|
||||
const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
|
||||
const DEFAULT_MESSAGE_DATA_TYPE = 'text';
|
||||
const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : values.join('');
|
||||
const DEFAULT_INTERPOLATE = toDisplayString;
|
||||
function pluralDefault(choice, choicesLength) {
|
||||
choice = Math.abs(choice);
|
||||
if (choicesLength === 2) {
|
||||
// prettier-ignore
|
||||
return choice
|
||||
? choice > 1
|
||||
? 1
|
||||
: 0
|
||||
: 1;
|
||||
}
|
||||
return choice ? Math.min(choice, 2) : 0;
|
||||
}
|
||||
function getPluralIndex(options) {
|
||||
// prettier-ignore
|
||||
const index = isNumber(options.pluralIndex)
|
||||
? options.pluralIndex
|
||||
: -1;
|
||||
// prettier-ignore
|
||||
return options.named && (isNumber(options.named.count) || isNumber(options.named.n))
|
||||
? isNumber(options.named.count)
|
||||
? options.named.count
|
||||
: isNumber(options.named.n)
|
||||
? options.named.n
|
||||
: index
|
||||
: index;
|
||||
}
|
||||
function normalizeNamed(pluralIndex, props) {
|
||||
if (!props.count) {
|
||||
props.count = pluralIndex;
|
||||
}
|
||||
if (!props.n) {
|
||||
props.n = pluralIndex;
|
||||
}
|
||||
}
|
||||
function createMessageContext(options = {}) {
|
||||
const locale = options.locale;
|
||||
const pluralIndex = getPluralIndex(options);
|
||||
const pluralRule = isObject(options.pluralRules) &&
|
||||
isString(locale) &&
|
||||
isFunction(options.pluralRules[locale])
|
||||
? options.pluralRules[locale]
|
||||
: pluralDefault;
|
||||
const orgPluralRule = isObject(options.pluralRules) &&
|
||||
isString(locale) &&
|
||||
isFunction(options.pluralRules[locale])
|
||||
? pluralDefault
|
||||
: undefined;
|
||||
const plural = (messages) => messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
||||
const _list = options.list || [];
|
||||
const list = (index) => _list[index];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const _named = options.named || {};
|
||||
isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
|
||||
const named = (key) => _named[key];
|
||||
// TODO: need to design resolve message function?
|
||||
function message(key) {
|
||||
// prettier-ignore
|
||||
const msg = isFunction(options.messages)
|
||||
? options.messages(key)
|
||||
: isObject(options.messages)
|
||||
? options.messages[key]
|
||||
: false;
|
||||
return !msg
|
||||
? options.parent
|
||||
? options.parent.message(key) // resolve from parent messages
|
||||
: DEFAULT_MESSAGE
|
||||
: msg;
|
||||
}
|
||||
const _modifier = (name) => options.modifiers
|
||||
? options.modifiers[name]
|
||||
: DEFAULT_MODIFIER;
|
||||
const normalize = isPlainObject(options.processor) && isFunction(options.processor.normalize)
|
||||
? options.processor.normalize
|
||||
: DEFAULT_NORMALIZE;
|
||||
const interpolate = isPlainObject(options.processor) &&
|
||||
isFunction(options.processor.interpolate)
|
||||
? options.processor.interpolate
|
||||
: DEFAULT_INTERPOLATE;
|
||||
const type = isPlainObject(options.processor) && isString(options.processor.type)
|
||||
? options.processor.type
|
||||
: DEFAULT_MESSAGE_DATA_TYPE;
|
||||
const ctx = {
|
||||
["list" /* LIST */]: list,
|
||||
["named" /* NAMED */]: named,
|
||||
["plural" /* PLURAL */]: plural,
|
||||
["linked" /* LINKED */]: (key, modifier) => {
|
||||
// TODO: should check `key`
|
||||
const msg = message(key)(ctx);
|
||||
return isString(modifier) ? _modifier(modifier)(msg) : msg;
|
||||
},
|
||||
["message" /* MESSAGE */]: message,
|
||||
["type" /* TYPE */]: type,
|
||||
["interpolate" /* INTERPOLATE */]: interpolate,
|
||||
["normalize" /* NORMALIZE */]: normalize
|
||||
};
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export { DEFAULT_MESSAGE_DATA_TYPE, createMessageContext };
|
||||
Reference in New Issue
Block a user