You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
231 lines
7.5 KiB
JavaScript
231 lines
7.5 KiB
JavaScript
(function (root) {
|
|
'use strict';
|
|
|
|
const source = root.document?.getElementById('user-copy-bootstrap');
|
|
let payload = {};
|
|
try {
|
|
payload = JSON.parse(source?.textContent || '{}');
|
|
} catch {
|
|
payload = {};
|
|
}
|
|
const locale = typeof payload.locale === 'string' && payload.locale
|
|
? payload.locale : 'zh-CN';
|
|
const messages = payload.messages && typeof payload.messages === 'object'
|
|
? payload.messages : {};
|
|
const metadata = payload.metadata && typeof payload.metadata === 'object'
|
|
? payload.metadata : {};
|
|
|
|
function matchingBrace(pattern, start) {
|
|
let depth = 0;
|
|
let quoted = false;
|
|
for (let index = start; index < pattern.length; index += 1) {
|
|
const character = pattern[index];
|
|
if (character === "'") {
|
|
if (pattern[index + 1] === "'") {
|
|
index += 1;
|
|
} else {
|
|
quoted = !quoted;
|
|
}
|
|
continue;
|
|
}
|
|
if (quoted) continue;
|
|
if (character === '{') depth += 1;
|
|
if (character === '}') {
|
|
depth -= 1;
|
|
if (depth === 0) return index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
function splitArgument(argument) {
|
|
const parts = [];
|
|
let depth = 0;
|
|
let quoted = false;
|
|
let start = 0;
|
|
for (let index = 0; index < argument.length; index += 1) {
|
|
const character = argument[index];
|
|
if (character === "'") {
|
|
if (argument[index + 1] === "'") index += 1;
|
|
else quoted = !quoted;
|
|
continue;
|
|
}
|
|
if (quoted) continue;
|
|
if (character === '{') depth += 1;
|
|
else if (character === '}') depth -= 1;
|
|
else if (character === ',' && depth === 0 && parts.length < 2) {
|
|
parts.push(argument.slice(start, index).trim());
|
|
start = index + 1;
|
|
}
|
|
}
|
|
parts.push(argument.slice(start).trim());
|
|
return parts;
|
|
}
|
|
|
|
function choices(style) {
|
|
const result = {};
|
|
let offset = 0;
|
|
let cursor = 0;
|
|
const offsetMatch = style.match(/^\s*offset\s*:\s*(\d+(?:\.\d+)?)\s*/);
|
|
if (offsetMatch) {
|
|
offset = Number(offsetMatch[1]);
|
|
cursor = offsetMatch[0].length;
|
|
}
|
|
while (cursor < style.length) {
|
|
while (/\s/.test(style[cursor] || '')) cursor += 1;
|
|
const selectorStart = cursor;
|
|
while (cursor < style.length && !/[\s{]/.test(style[cursor])) cursor += 1;
|
|
const selector = style.slice(selectorStart, cursor);
|
|
while (/\s/.test(style[cursor] || '')) cursor += 1;
|
|
if (!selector || style[cursor] !== '{') return null;
|
|
const end = matchingBrace(style, cursor);
|
|
if (end < 0) return null;
|
|
result[selector] = style.slice(cursor + 1, end);
|
|
cursor = end + 1;
|
|
}
|
|
return { offset, values: result };
|
|
}
|
|
|
|
function formatArgument(argument, parameters, pluralValue) {
|
|
const parts = splitArgument(argument);
|
|
const name = parts[0];
|
|
if (!Object.prototype.hasOwnProperty.call(parameters, name)) {
|
|
return `{${argument}}`;
|
|
}
|
|
const value = parameters[name];
|
|
if (parts.length === 1) return String(value);
|
|
const type = String(parts[1] || '').toLowerCase();
|
|
const style = parts[2] || '';
|
|
if (type === 'plural' || type === 'selectordinal') {
|
|
const parsed = choices(style);
|
|
const numeric = Number(value);
|
|
if (!parsed || !Number.isFinite(numeric)) return `{${argument}}`;
|
|
const exact = `=${numeric}`;
|
|
const adjusted = numeric - parsed.offset;
|
|
const category = new Intl.PluralRules(locale, {
|
|
type: type === 'selectordinal' ? 'ordinal' : 'cardinal',
|
|
}).select(adjusted);
|
|
const selected = parsed.values[exact]
|
|
?? parsed.values[category] ?? parsed.values.other;
|
|
return typeof selected === 'string'
|
|
? formatPattern(selected, parameters, adjusted) : `{${argument}}`;
|
|
}
|
|
if (type === 'select') {
|
|
const parsed = choices(style);
|
|
if (!parsed) return `{${argument}}`;
|
|
const selected = parsed.values[String(value)] ?? parsed.values.other;
|
|
return typeof selected === 'string'
|
|
? formatPattern(selected, parameters, pluralValue) : `{${argument}}`;
|
|
}
|
|
if (type === 'number') {
|
|
const numeric = Number(value);
|
|
if (!Number.isFinite(numeric)) return `{${argument}}`;
|
|
if (style === 'integer') {
|
|
return number(numeric, { maximumFractionDigits: 0 });
|
|
}
|
|
if (style === 'percent') {
|
|
return number(numeric, { style: 'percent' });
|
|
}
|
|
return number(numeric);
|
|
}
|
|
if (type === 'date' || type === 'time') {
|
|
const parsed = new Date(Number(value) < 1e12
|
|
? Number(value) * 1000 : value);
|
|
if (!Number.isFinite(parsed.getTime())) return `{${argument}}`;
|
|
const options = type === 'date'
|
|
? { dateStyle: style || 'medium' }
|
|
: { timeStyle: style || 'medium', hour12: false };
|
|
return new Intl.DateTimeFormat(locale, options).format(parsed);
|
|
}
|
|
return `{${argument}}`;
|
|
}
|
|
|
|
function formatPattern(pattern, parameters, pluralValue = null) {
|
|
let output = '';
|
|
for (let index = 0; index < pattern.length; index += 1) {
|
|
const character = pattern[index];
|
|
if (character === "'") {
|
|
if (pattern[index + 1] === "'") {
|
|
output += "'";
|
|
index += 1;
|
|
continue;
|
|
}
|
|
const end = pattern.indexOf("'", index + 1);
|
|
if (end >= 0) {
|
|
output += pattern.slice(index + 1, end);
|
|
index = end;
|
|
continue;
|
|
}
|
|
}
|
|
if (character === '{') {
|
|
const end = matchingBrace(pattern, index);
|
|
if (end >= 0) {
|
|
output += formatArgument(
|
|
pattern.slice(index + 1, end), parameters, pluralValue
|
|
);
|
|
index = end;
|
|
continue;
|
|
}
|
|
}
|
|
if (character === '#' && pluralValue !== null) {
|
|
output += number(pluralValue);
|
|
} else {
|
|
output += character;
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
function text(key, parameters = {}) {
|
|
const message = typeof messages[key] === 'string' ? messages[key] : key;
|
|
const descriptor = metadata[key] && typeof metadata[key] === 'object'
|
|
? metadata[key] : {};
|
|
if (descriptor.hidden === true) return '';
|
|
if (descriptor.format !== 'icu') return message;
|
|
return formatPattern(message, parameters);
|
|
}
|
|
|
|
function number(value, options = {}) {
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed)
|
|
? new Intl.NumberFormat(locale, options).format(parsed) : '—';
|
|
}
|
|
|
|
function currency(value, currencyCode = 'USD', options = {}) {
|
|
return number(value, {
|
|
style: 'currency', currency: currencyCode, ...options,
|
|
});
|
|
}
|
|
|
|
function date(value, options = {}) {
|
|
const numeric = Number(value);
|
|
const timestamp = Number.isFinite(numeric) && numeric > 0
|
|
? (numeric < 1e12 ? numeric * 1000 : numeric)
|
|
: Date.parse(String(value || ''));
|
|
const parsed = new Date(timestamp);
|
|
return Number.isFinite(parsed.getTime())
|
|
? new Intl.DateTimeFormat(locale, {
|
|
dateStyle: 'medium', timeStyle: 'medium', hour12: false, ...options,
|
|
}).format(parsed)
|
|
: '—';
|
|
}
|
|
|
|
function bytes(value, options = {}) {
|
|
let amount = Number(value);
|
|
if (!Number.isFinite(amount) || amount < 0) return '—';
|
|
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
|
|
let unit = 0;
|
|
while (amount >= 1024 && unit < units.length - 1) {
|
|
amount /= 1024;
|
|
unit += 1;
|
|
}
|
|
const maximumFractionDigits = unit === 0 ? 0 : amount >= 100 ? 0
|
|
: amount >= 10 ? 1 : 2;
|
|
return `${number(amount, { maximumFractionDigits, ...options })} ${units[unit]}`;
|
|
}
|
|
|
|
root.VenixUserCopy = Object.freeze({
|
|
locale, text, number, currency, date, bytes,
|
|
});
|
|
}(globalThis));
|