@prezly/theme-kit-intl 7.1.0 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/dsl.cjs +9 -0
- package/build/dsl.d.ts +12 -0
- package/build/dsl.mjs +3 -0
- package/build/formatting/datetime/constants.cjs +7 -0
- package/build/formatting/datetime/constants.d.ts +1 -0
- package/build/formatting/datetime/constants.mjs +1 -0
- package/build/formatting/datetime/index.cjs +25 -0
- package/build/formatting/datetime/index.d.ts +3 -0
- package/build/formatting/datetime/index.mjs +2 -0
- package/build/formatting/datetime/lib.cjs +58 -0
- package/build/formatting/datetime/lib.d.ts +10 -0
- package/build/formatting/datetime/lib.mjs +51 -0
- package/build/formatting/datetime/shared.cjs +15 -0
- package/build/formatting/datetime/shared.d.ts +2 -0
- package/build/formatting/datetime/shared.mjs +9 -0
- package/build/formatting/datetime/types.cjs +1 -0
- package/build/formatting/datetime/types.d.ts +3 -0
- package/build/formatting/datetime/types.mjs +1 -0
- package/build/formatting/index.cjs +27 -0
- package/build/formatting/index.d.ts +2 -0
- package/build/formatting/index.mjs +2 -0
- package/build/formatting/messages/index.cjs +18 -0
- package/build/formatting/messages/index.d.ts +2 -0
- package/build/formatting/messages/index.mjs +1 -0
- package/build/formatting/messages/lib.cjs +108 -0
- package/build/formatting/messages/lib.d.ts +3 -0
- package/build/formatting/messages/lib.mjs +101 -0
- package/build/formatting/messages/lib.test.cjs +87 -0
- package/build/formatting/messages/lib.test.d.ts +1 -0
- package/build/formatting/messages/lib.test.mjs +84 -0
- package/build/formatting/messages/types.cjs +1 -0
- package/build/formatting/messages/types.d.ts +10 -0
- package/build/formatting/messages/types.mjs +1 -0
- package/build/index.cjs +37 -0
- package/build/index.d.ts +3 -0
- package/build/index.mjs +4 -0
- package/build/locales/Locale.cjs +105 -0
- package/build/locales/Locale.d.ts +61 -0
- package/build/locales/Locale.mjs +99 -0
- package/build/locales/Locale.test.cjs +120 -0
- package/build/locales/Locale.test.d.ts +1 -0
- package/build/locales/Locale.test.mjs +118 -0
- package/build/locales/constants.cjs +7 -0
- package/build/locales/constants.d.ts +1 -0
- package/build/locales/constants.mjs +1 -0
- package/build/locales/index.cjs +40 -0
- package/build/locales/index.d.ts +5 -0
- package/build/locales/index.mjs +5 -0
- package/build/locales/isLocaleSupported.cjs +15 -0
- package/build/locales/isLocaleSupported.d.ts +2 -0
- package/build/locales/isLocaleSupported.mjs +9 -0
- package/build/locales/pickSuportedLocale.cjs +37 -0
- package/build/locales/pickSuportedLocale.d.ts +6 -0
- package/build/locales/pickSuportedLocale.mjs +31 -0
- package/build/locales/pickSuportedLocale.test.cjs +26 -0
- package/build/locales/pickSuportedLocale.test.d.ts +1 -0
- package/build/locales/pickSuportedLocale.test.mjs +24 -0
- package/build/locales/supportedLocales.cjs +12 -0
- package/build/locales/supportedLocales.d.ts +6 -0
- package/build/locales/supportedLocales.mjs +6 -0
- package/build/{cjs/messages/actions.js → messages/actions.cjs} +5 -2
- package/build/{esm/messages → messages}/actions.mjs +1 -1
- package/build/{cjs/messages/content.js → messages/content.cjs} +11 -8
- package/build/{esm/messages → messages}/content.mjs +1 -1
- package/build/{cjs/messages/errors.js → messages/errors.cjs} +7 -4
- package/build/{esm/messages → messages}/errors.mjs +1 -1
- package/build/{cjs/messages/index.js → messages/index.cjs} +9 -6
- package/build/{cjs/messages/misc.js → messages/misc.cjs} +5 -2
- package/build/{esm/messages → messages}/misc.mjs +1 -1
- package/build/{cjs/messages/search.js → messages/search.cjs} +6 -3
- package/build/{esm/messages → messages}/search.mjs +1 -1
- package/build/{cjs/messages/subscription.js → messages/subscription.cjs} +5 -2
- package/build/{esm/messages → messages}/subscription.mjs +1 -1
- package/package.json +27 -28
- package/build/cjs/index.d.ts +0 -4
- package/build/cjs/index.js +0 -19
- package/build/cjs/isLocaleSupported.d.ts +0 -1
- package/build/cjs/isLocaleSupported.js +0 -7
- package/build/cjs/locales.d.ts +0 -1
- package/build/cjs/locales.js +0 -5
- package/build/esm/index.d.ts +0 -4
- package/build/esm/index.mjs +0 -4
- package/build/esm/isLocaleSupported.d.ts +0 -1
- package/build/esm/isLocaleSupported.mjs +0 -4
- package/build/esm/locales.d.ts +0 -1
- package/build/esm/locales.mjs +0 -2
- package/build/esm/messages/actions.d.ts +0 -58
- package/build/esm/messages/content.d.ts +0 -66
- package/build/esm/messages/errors.d.ts +0 -34
- package/build/esm/messages/index.d.ts +0 -6
- package/build/esm/messages/misc.d.ts +0 -20
- package/build/esm/messages/search.d.ts +0 -66
- package/build/esm/messages/subscription.d.ts +0 -19
- /package/build/{messages → i18n}/af.json +0 -0
- /package/build/{messages → i18n}/ar.json +0 -0
- /package/build/{messages → i18n}/az.json +0 -0
- /package/build/{messages → i18n}/be.json +0 -0
- /package/build/{messages → i18n}/bg.json +0 -0
- /package/build/{messages → i18n}/ca.json +0 -0
- /package/build/{messages → i18n}/cs.json +0 -0
- /package/build/{messages → i18n}/da.json +0 -0
- /package/build/{messages → i18n}/de.json +0 -0
- /package/build/{messages → i18n}/el.json +0 -0
- /package/build/{messages → i18n}/en.json +0 -0
- /package/build/{messages → i18n}/es.json +0 -0
- /package/build/{messages → i18n}/et.json +0 -0
- /package/build/{messages → i18n}/fi.json +0 -0
- /package/build/{messages → i18n}/fil.json +0 -0
- /package/build/{messages → i18n}/fr.json +0 -0
- /package/build/{messages → i18n}/ga.json +0 -0
- /package/build/{messages → i18n}/he.json +0 -0
- /package/build/{messages → i18n}/hi.json +0 -0
- /package/build/{messages → i18n}/hr.json +0 -0
- /package/build/{messages → i18n}/hu.json +0 -0
- /package/build/{messages → i18n}/id.json +0 -0
- /package/build/{messages → i18n}/it.json +0 -0
- /package/build/{messages → i18n}/ja.json +0 -0
- /package/build/{messages → i18n}/kk.json +0 -0
- /package/build/{messages → i18n}/ko.json +0 -0
- /package/build/{messages → i18n}/lt.json +0 -0
- /package/build/{messages → i18n}/lv.json +0 -0
- /package/build/{messages → i18n}/mt.json +0 -0
- /package/build/{messages → i18n}/nl.json +0 -0
- /package/build/{messages → i18n}/no.json +0 -0
- /package/build/{messages → i18n}/pl.json +0 -0
- /package/build/{messages → i18n}/pt-BR.json +0 -0
- /package/build/{messages → i18n}/pt.json +0 -0
- /package/build/{messages → i18n}/ro.json +0 -0
- /package/build/{messages → i18n}/ru.json +0 -0
- /package/build/{messages → i18n}/sk.json +0 -0
- /package/build/{messages → i18n}/sl.json +0 -0
- /package/build/{messages → i18n}/sr.json +0 -0
- /package/build/{messages → i18n}/sv.json +0 -0
- /package/build/{messages → i18n}/sw.json +0 -0
- /package/build/{messages → i18n}/th.json +0 -0
- /package/build/{messages → i18n}/tr.json +0 -0
- /package/build/{messages → i18n}/uk.json +0 -0
- /package/build/{messages → i18n}/ur.json +0 -0
- /package/build/{messages → i18n}/uz.json +0 -0
- /package/build/{messages → i18n}/vi.json +0 -0
- /package/build/{messages → i18n}/zh-CN.json +0 -0
- /package/build/{messages → i18n}/zh-TW.json +0 -0
- /package/build/{cjs/messages → messages}/actions.d.ts +0 -0
- /package/build/{cjs/messages → messages}/content.d.ts +0 -0
- /package/build/{cjs/messages → messages}/errors.d.ts +0 -0
- /package/build/{cjs/messages → messages}/index.d.ts +0 -0
- /package/build/{esm/messages → messages}/index.mjs +0 -0
- /package/build/{cjs/messages → messages}/misc.d.ts +0 -0
- /package/build/{cjs/messages → messages}/search.d.ts +0 -0
- /package/build/{cjs/messages → messages}/subscription.d.ts +0 -0
package/build/dsl.cjs
ADDED
package/build/dsl.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Namespace = string;
|
|
2
|
+
type NamespacedId = `${string}` | `${Namespace}.${string}`;
|
|
3
|
+
interface IntlMessageDefinition {
|
|
4
|
+
id: NamespacedId;
|
|
5
|
+
defaultMessage?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
}
|
|
8
|
+
type IntlDictionaryDefinition = {
|
|
9
|
+
[name: string]: IntlMessageDefinition;
|
|
10
|
+
};
|
|
11
|
+
export declare function defineMessages<T extends IntlDictionaryDefinition>(dictionary: T): T;
|
|
12
|
+
export {};
|
package/build/dsl.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_TIMEZONE = "Europe/London";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export var DEFAULT_TIMEZONE = 'Europe/London';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "DEFAULT_TIMEZONE", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _constants.DEFAULT_TIMEZONE;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "formatDate", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _lib.formatDate;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "formatTime", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return _lib.formatTime;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
var _constants = require("./constants.cjs");
|
|
25
|
+
var _lib = require("./lib.cjs");
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.formatDate = formatDate;
|
|
7
|
+
exports.formatTime = formatTime;
|
|
8
|
+
var _index = require("../../locales/index.cjs");
|
|
9
|
+
var _shared = require("./shared.cjs");
|
|
10
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
11
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
12
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
|
|
14
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
15
|
+
function withCache(fn) {
|
|
16
|
+
var CACHE = new Map();
|
|
17
|
+
return function () {
|
|
18
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
19
|
+
args[_key] = arguments[_key];
|
|
20
|
+
}
|
|
21
|
+
var key = JSON.stringify(args);
|
|
22
|
+
var cached = CACHE.get(key);
|
|
23
|
+
if (cached) return cached;
|
|
24
|
+
var value = fn(...args);
|
|
25
|
+
CACHE.set(key, value);
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var getDateFormat = withCache(function (locale, timeZone) {
|
|
30
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
31
|
+
return new Intl.DateTimeFormat(_index.Locale.from(locale).isoCode, _objectSpread({
|
|
32
|
+
year: 'numeric',
|
|
33
|
+
month: 'long',
|
|
34
|
+
day: 'numeric',
|
|
35
|
+
timeZone
|
|
36
|
+
}, options));
|
|
37
|
+
});
|
|
38
|
+
var getTimeFormat = withCache(function (locale, timeZone) {
|
|
39
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
40
|
+
return new Intl.DateTimeFormat(_index.Locale.from(locale).isoCode, _objectSpread({
|
|
41
|
+
hour: 'numeric',
|
|
42
|
+
minute: 'numeric',
|
|
43
|
+
timeZone,
|
|
44
|
+
timeZoneName: 'shortGeneric'
|
|
45
|
+
}, options));
|
|
46
|
+
});
|
|
47
|
+
function formatDate(input, context) {
|
|
48
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
49
|
+
var dateTime = (0, _shared.toDate)(input);
|
|
50
|
+
var format = getDateFormat(context.locale, context.timezone, options);
|
|
51
|
+
return format.format(dateTime);
|
|
52
|
+
}
|
|
53
|
+
function formatTime(input, context) {
|
|
54
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
55
|
+
var dateTime = (0, _shared.toDate)(input);
|
|
56
|
+
var format = getTimeFormat(context.locale, context.timezone, options);
|
|
57
|
+
return format.format(dateTime);
|
|
58
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Locale } from '../../locales';
|
|
2
|
+
import type { Iso8601Date, Timezone, UnixTimestampInSeconds } from './types';
|
|
3
|
+
export declare function formatDate(input: Date | Iso8601Date | UnixTimestampInSeconds, context: {
|
|
4
|
+
locale: Locale.Code;
|
|
5
|
+
timezone: Timezone;
|
|
6
|
+
}, options?: Intl.DateTimeFormatOptions): string;
|
|
7
|
+
export declare function formatTime(input: Date | Iso8601Date | UnixTimestampInSeconds, context: {
|
|
8
|
+
locale: Locale.Code;
|
|
9
|
+
timezone: Timezone;
|
|
10
|
+
}, options?: Intl.DateTimeFormatOptions): string;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
2
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
3
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
|
|
5
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
6
|
+
import { Locale } from "../../locales/index.mjs";
|
|
7
|
+
import { toDate } from "./shared.mjs";
|
|
8
|
+
function withCache(fn) {
|
|
9
|
+
var CACHE = new Map();
|
|
10
|
+
return function () {
|
|
11
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
12
|
+
args[_key] = arguments[_key];
|
|
13
|
+
}
|
|
14
|
+
var key = JSON.stringify(args);
|
|
15
|
+
var cached = CACHE.get(key);
|
|
16
|
+
if (cached) return cached;
|
|
17
|
+
var value = fn(...args);
|
|
18
|
+
CACHE.set(key, value);
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
var getDateFormat = withCache(function (locale, timeZone) {
|
|
23
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
24
|
+
return new Intl.DateTimeFormat(Locale.from(locale).isoCode, _objectSpread({
|
|
25
|
+
year: 'numeric',
|
|
26
|
+
month: 'long',
|
|
27
|
+
day: 'numeric',
|
|
28
|
+
timeZone
|
|
29
|
+
}, options));
|
|
30
|
+
});
|
|
31
|
+
var getTimeFormat = withCache(function (locale, timeZone) {
|
|
32
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
33
|
+
return new Intl.DateTimeFormat(Locale.from(locale).isoCode, _objectSpread({
|
|
34
|
+
hour: 'numeric',
|
|
35
|
+
minute: 'numeric',
|
|
36
|
+
timeZone,
|
|
37
|
+
timeZoneName: 'shortGeneric'
|
|
38
|
+
}, options));
|
|
39
|
+
});
|
|
40
|
+
export function formatDate(input, context) {
|
|
41
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
42
|
+
var dateTime = toDate(input);
|
|
43
|
+
var format = getDateFormat(context.locale, context.timezone, options);
|
|
44
|
+
return format.format(dateTime);
|
|
45
|
+
}
|
|
46
|
+
export function formatTime(input, context) {
|
|
47
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
48
|
+
var dateTime = toDate(input);
|
|
49
|
+
var format = getTimeFormat(context.locale, context.timezone, options);
|
|
50
|
+
return format.format(dateTime);
|
|
51
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.toDate = toDate;
|
|
7
|
+
function toDate(value) {
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
return new Date(value);
|
|
10
|
+
}
|
|
11
|
+
if (typeof value === 'number') {
|
|
12
|
+
return new Date(value * 1000);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _index = require("./datetime/index.cjs");
|
|
7
|
+
Object.keys(_index).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _index[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function get() {
|
|
13
|
+
return _index[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _index2 = require("./messages/index.cjs");
|
|
18
|
+
Object.keys(_index2).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _index2[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function get() {
|
|
24
|
+
return _index2[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "formatMessageFragment", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _lib.formatMessageFragment;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "formatMessageString", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _lib.formatMessageString;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _lib = require("./lib.cjs");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { formatMessageFragment, formatMessageString } from "./lib.mjs";
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.formatMessageFragment = formatMessageFragment;
|
|
7
|
+
exports.formatMessageString = formatMessageString;
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
9
|
+
|
|
10
|
+
function formatMessageFragment(_ref, dictionary) {
|
|
11
|
+
var _ref2, _dictionary$id;
|
|
12
|
+
var {
|
|
13
|
+
id,
|
|
14
|
+
defaultMessage
|
|
15
|
+
} = _ref;
|
|
16
|
+
var values = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
17
|
+
var format = (_ref2 = (_dictionary$id = dictionary[id]) !== null && _dictionary$id !== void 0 ? _dictionary$id : prepareMessage(defaultMessage)) !== null && _ref2 !== void 0 ? _ref2 : [{
|
|
18
|
+
type: 0,
|
|
19
|
+
value: "[".concat(id, "]")
|
|
20
|
+
}];
|
|
21
|
+
return format.map(_ref3 => {
|
|
22
|
+
var _values$value;
|
|
23
|
+
var {
|
|
24
|
+
type,
|
|
25
|
+
value
|
|
26
|
+
} = _ref3;
|
|
27
|
+
if (type === 0) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
return (_values$value = values[value]) !== null && _values$value !== void 0 ? _values$value : "{".concat(value, "}");
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function formatMessageString(_ref4, dictionary) {
|
|
34
|
+
var _ref5, _dictionary$id2;
|
|
35
|
+
var {
|
|
36
|
+
id,
|
|
37
|
+
defaultMessage
|
|
38
|
+
} = _ref4;
|
|
39
|
+
var values = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
40
|
+
var format = (_ref5 = (_dictionary$id2 = dictionary[id]) !== null && _dictionary$id2 !== void 0 ? _dictionary$id2 : prepareMessage(defaultMessage)) !== null && _ref5 !== void 0 ? _ref5 : [{
|
|
41
|
+
type: 0,
|
|
42
|
+
value: "[".concat(id, "]")
|
|
43
|
+
}];
|
|
44
|
+
return format.map(_ref6 => {
|
|
45
|
+
var _values$value2;
|
|
46
|
+
var {
|
|
47
|
+
type,
|
|
48
|
+
value
|
|
49
|
+
} = _ref6;
|
|
50
|
+
if (type === 0) {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
return String((_values$value2 = values[value]) !== null && _values$value2 !== void 0 ? _values$value2 : "{".concat(value, "}"));
|
|
54
|
+
}).join('');
|
|
55
|
+
}
|
|
56
|
+
var CACHE = new Map();
|
|
57
|
+
function prepareMessage(message) {
|
|
58
|
+
if (typeof message === 'undefined') {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
var cached = CACHE.get(message);
|
|
62
|
+
if (cached) return cached;
|
|
63
|
+
var parsed = parseMessage(message);
|
|
64
|
+
CACHE.set(message, parsed);
|
|
65
|
+
return parsed;
|
|
66
|
+
}
|
|
67
|
+
function parseMessage(message) {
|
|
68
|
+
if (message.length === 0) {
|
|
69
|
+
return [{
|
|
70
|
+
type: 0,
|
|
71
|
+
value: message
|
|
72
|
+
}];
|
|
73
|
+
}
|
|
74
|
+
var remaining = message;
|
|
75
|
+
var format = [];
|
|
76
|
+
while (remaining.length > 0) {
|
|
77
|
+
var start = remaining.indexOf('{');
|
|
78
|
+
var end = remaining.indexOf('}');
|
|
79
|
+
if (start === -1 || end === -1) {
|
|
80
|
+
format.push({
|
|
81
|
+
type: 0,
|
|
82
|
+
value: remaining
|
|
83
|
+
});
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
var prefix = remaining.substring(0, start);
|
|
87
|
+
var placeholder = remaining.substring(start + 1, end);
|
|
88
|
+
if (prefix) {
|
|
89
|
+
format.push({
|
|
90
|
+
type: 0,
|
|
91
|
+
value: prefix
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (placeholder) {
|
|
95
|
+
format.push({
|
|
96
|
+
type: 1,
|
|
97
|
+
value: placeholder
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
format.push({
|
|
101
|
+
type: 0,
|
|
102
|
+
value: '{}'
|
|
103
|
+
}); // false-positive?
|
|
104
|
+
}
|
|
105
|
+
remaining = remaining.substring(end + 1);
|
|
106
|
+
}
|
|
107
|
+
return format;
|
|
108
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { IntlDictionary, IntlMessageDescriptor, IntlMessageValues } from './types';
|
|
2
|
+
export declare function formatMessageFragment<T>({ id, defaultMessage }: IntlMessageDescriptor, dictionary: IntlDictionary, values?: IntlMessageValues<T>): (T | string)[];
|
|
3
|
+
export declare function formatMessageString({ id, defaultMessage }: IntlMessageDescriptor, dictionary: IntlDictionary, values?: IntlMessageValues<string | number>): string;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
|
|
3
|
+
export function formatMessageFragment(_ref, dictionary) {
|
|
4
|
+
var _ref2, _dictionary$id;
|
|
5
|
+
var {
|
|
6
|
+
id,
|
|
7
|
+
defaultMessage
|
|
8
|
+
} = _ref;
|
|
9
|
+
var values = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
10
|
+
var format = (_ref2 = (_dictionary$id = dictionary[id]) !== null && _dictionary$id !== void 0 ? _dictionary$id : prepareMessage(defaultMessage)) !== null && _ref2 !== void 0 ? _ref2 : [{
|
|
11
|
+
type: 0,
|
|
12
|
+
value: "[".concat(id, "]")
|
|
13
|
+
}];
|
|
14
|
+
return format.map(_ref3 => {
|
|
15
|
+
var _values$value;
|
|
16
|
+
var {
|
|
17
|
+
type,
|
|
18
|
+
value
|
|
19
|
+
} = _ref3;
|
|
20
|
+
if (type === 0) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
return (_values$value = values[value]) !== null && _values$value !== void 0 ? _values$value : "{".concat(value, "}");
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export function formatMessageString(_ref4, dictionary) {
|
|
27
|
+
var _ref5, _dictionary$id2;
|
|
28
|
+
var {
|
|
29
|
+
id,
|
|
30
|
+
defaultMessage
|
|
31
|
+
} = _ref4;
|
|
32
|
+
var values = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
33
|
+
var format = (_ref5 = (_dictionary$id2 = dictionary[id]) !== null && _dictionary$id2 !== void 0 ? _dictionary$id2 : prepareMessage(defaultMessage)) !== null && _ref5 !== void 0 ? _ref5 : [{
|
|
34
|
+
type: 0,
|
|
35
|
+
value: "[".concat(id, "]")
|
|
36
|
+
}];
|
|
37
|
+
return format.map(_ref6 => {
|
|
38
|
+
var _values$value2;
|
|
39
|
+
var {
|
|
40
|
+
type,
|
|
41
|
+
value
|
|
42
|
+
} = _ref6;
|
|
43
|
+
if (type === 0) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
return String((_values$value2 = values[value]) !== null && _values$value2 !== void 0 ? _values$value2 : "{".concat(value, "}"));
|
|
47
|
+
}).join('');
|
|
48
|
+
}
|
|
49
|
+
var CACHE = new Map();
|
|
50
|
+
function prepareMessage(message) {
|
|
51
|
+
if (typeof message === 'undefined') {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
var cached = CACHE.get(message);
|
|
55
|
+
if (cached) return cached;
|
|
56
|
+
var parsed = parseMessage(message);
|
|
57
|
+
CACHE.set(message, parsed);
|
|
58
|
+
return parsed;
|
|
59
|
+
}
|
|
60
|
+
function parseMessage(message) {
|
|
61
|
+
if (message.length === 0) {
|
|
62
|
+
return [{
|
|
63
|
+
type: 0,
|
|
64
|
+
value: message
|
|
65
|
+
}];
|
|
66
|
+
}
|
|
67
|
+
var remaining = message;
|
|
68
|
+
var format = [];
|
|
69
|
+
while (remaining.length > 0) {
|
|
70
|
+
var start = remaining.indexOf('{');
|
|
71
|
+
var end = remaining.indexOf('}');
|
|
72
|
+
if (start === -1 || end === -1) {
|
|
73
|
+
format.push({
|
|
74
|
+
type: 0,
|
|
75
|
+
value: remaining
|
|
76
|
+
});
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
var prefix = remaining.substring(0, start);
|
|
80
|
+
var placeholder = remaining.substring(start + 1, end);
|
|
81
|
+
if (prefix) {
|
|
82
|
+
format.push({
|
|
83
|
+
type: 0,
|
|
84
|
+
value: prefix
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (placeholder) {
|
|
88
|
+
format.push({
|
|
89
|
+
type: 1,
|
|
90
|
+
value: placeholder
|
|
91
|
+
});
|
|
92
|
+
} else {
|
|
93
|
+
format.push({
|
|
94
|
+
type: 0,
|
|
95
|
+
value: '{}'
|
|
96
|
+
}); // false-positive?
|
|
97
|
+
}
|
|
98
|
+
remaining = remaining.substring(end + 1);
|
|
99
|
+
}
|
|
100
|
+
return format;
|
|
101
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _lib = require("./lib.cjs");
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
5
|
+
|
|
6
|
+
var I18N = {
|
|
7
|
+
["hello.world"]: [{
|
|
8
|
+
type: 0,
|
|
9
|
+
value: '¡Hola, '
|
|
10
|
+
}, {
|
|
11
|
+
type: 1,
|
|
12
|
+
value: 'name'
|
|
13
|
+
}, {
|
|
14
|
+
type: 0,
|
|
15
|
+
value: '!'
|
|
16
|
+
}]
|
|
17
|
+
};
|
|
18
|
+
describe('formatMessageString', () => {
|
|
19
|
+
it('should format intl-generated message intl object using the given values', () => {
|
|
20
|
+
var formatted = (0, _lib.formatMessageString)({
|
|
21
|
+
id: 'hello.world',
|
|
22
|
+
defaultMessage: 'Hello, {name}!'
|
|
23
|
+
}, I18N, {
|
|
24
|
+
name: 'Julio'
|
|
25
|
+
});
|
|
26
|
+
expect(formatted).toBe('¡Hola, Julio!');
|
|
27
|
+
});
|
|
28
|
+
it('should leave placeholders if the given values object does not have enough data', () => {
|
|
29
|
+
var formatted = (0, _lib.formatMessageString)({
|
|
30
|
+
id: 'hello.world',
|
|
31
|
+
defaultMessage: 'Hello, {name}!'
|
|
32
|
+
}, I18N, {});
|
|
33
|
+
expect(formatted).toBe('¡Hola, {name}!');
|
|
34
|
+
});
|
|
35
|
+
it('should fallback to the default message if the dictionary does not have it', () => {
|
|
36
|
+
var formatted = (0, _lib.formatMessageString)({
|
|
37
|
+
id: 'hello.world',
|
|
38
|
+
defaultMessage: 'Hello, {name}!'
|
|
39
|
+
}, {}, {
|
|
40
|
+
name: 'Julio'
|
|
41
|
+
});
|
|
42
|
+
expect(formatted).toBe('Hello, Julio!');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('formatMessageFragment', () => {
|
|
46
|
+
it('should format intl-generated message intl object using the given values', () => {
|
|
47
|
+
var formatted = (0, _lib.formatMessageFragment)({
|
|
48
|
+
id: 'hello.world',
|
|
49
|
+
defaultMessage: 'Hello, {name}!'
|
|
50
|
+
}, I18N, {
|
|
51
|
+
name: createElement('strong', ['Julio'])
|
|
52
|
+
});
|
|
53
|
+
expect(formatted).toEqual(['¡Hola, ', {
|
|
54
|
+
type: 'strong',
|
|
55
|
+
children: ['Julio']
|
|
56
|
+
}, '!']);
|
|
57
|
+
});
|
|
58
|
+
it('should leave placeholders if the given values object does not have enough data', () => {
|
|
59
|
+
var formatted = (0, _lib.formatMessageFragment)({
|
|
60
|
+
id: 'hello.world',
|
|
61
|
+
defaultMessage: 'Hello, {name}!'
|
|
62
|
+
}, I18N, {});
|
|
63
|
+
expect(formatted).toEqual(['¡Hola, ', '{name}', '!']);
|
|
64
|
+
});
|
|
65
|
+
it('should fallback to the default message if the dictionary does not have it', () => {
|
|
66
|
+
var formatted = (0, _lib.formatMessageFragment)({
|
|
67
|
+
id: 'hello.world',
|
|
68
|
+
defaultMessage: 'Hello, {name}!'
|
|
69
|
+
}, {}, {
|
|
70
|
+
name: createElement('strong', ['Julio'])
|
|
71
|
+
});
|
|
72
|
+
expect(formatted).toEqual(['Hello, ', {
|
|
73
|
+
type: 'strong',
|
|
74
|
+
children: ['Julio']
|
|
75
|
+
}, '!']);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Emulating React.createElement for test purposes.
|
|
81
|
+
*/
|
|
82
|
+
function createElement(type, children) {
|
|
83
|
+
return {
|
|
84
|
+
type,
|
|
85
|
+
children
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|