@discord/intl 0.5.2 → 0.6.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.
@@ -1,63 +1,20 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
2
  Object.defineProperty(exports, "__esModule", { value: true });
43
3
  exports.reactFormatter = exports.DEFAULT_REACT_RICH_TEXT_ELEMENTS = void 0;
44
4
  exports.formatReact = formatReact;
45
5
  exports.makeReactFormatter = makeReactFormatter;
46
- var React = require("react");
47
- var format_1 = require("../format");
48
- var h = React.createElement;
6
+ const React = require("react");
7
+ const format_1 = require("../format");
8
+ const h = React.createElement;
49
9
  exports.DEFAULT_REACT_RICH_TEXT_ELEMENTS = {
50
- $_: function () { return ''; },
51
- $b: function (content, key) { return h('strong', { key: key }, content); },
52
- $i: function (content, key) { return h('em', { key: key }, content); },
53
- $del: function (content, key) { return h('del', { key: key }, content); },
54
- $code: function (content, key) { return h('code', { key: key }, content); },
10
+ $_: () => '',
11
+ $b: (content, key) => h('strong', { key }, content),
12
+ $i: (content, key) => h('em', { key }, content),
13
+ $del: (content, key) => h('del', { key }, content),
14
+ $code: (content, key) => h('code', { key }, content),
55
15
  // $link will always be [href, <empty>, ...content]
56
- $link: function (_a, key) {
57
- var _b = __read(_a), href = _b[0], content = _b.slice(1);
58
- return h('a', { href: href, key: key }, content);
59
- },
60
- $p: function (content, key) { return h('p', { key: key }, content); },
16
+ $link: ([href, ...content], key) => h('a', { href, key }, content),
17
+ $p: (content, key) => h('p', { key }, content),
61
18
  };
62
19
  /**
63
20
  * Creates a new `FormatBuilder` class that constructs a React element tree
@@ -67,36 +24,33 @@ exports.DEFAULT_REACT_RICH_TEXT_ELEMENTS = {
67
24
  * @param richTextElements
68
25
  */
69
26
  function createReactBuilder(richTextElements) {
70
- return /** @class */ (function (_super) {
71
- __extends(class_1, _super);
72
- function class_1() {
73
- var _this = _super.apply(this, __spreadArray([], __read(arguments), false)) || this;
74
- _this._nodeKey = 0;
75
- _this.result = [];
76
- return _this;
27
+ return class extends format_1.FormatBuilder {
28
+ constructor() {
29
+ super(...arguments);
30
+ this._nodeKey = 0;
31
+ this.result = [];
77
32
  }
78
- class_1.prototype.pushRichTextTag = function (tag, children) {
79
- this.result.push(richTextElements[tag](children, "".concat(this._nodeKey++)));
80
- };
81
- class_1.prototype.pushLiteralText = function (text) {
33
+ pushRichTextTag(tag, children) {
34
+ this.result.push(richTextElements[tag](children, `${this._nodeKey++}`));
35
+ }
36
+ pushLiteralText(text) {
82
37
  this.result.push(text);
83
- };
84
- class_1.prototype.pushObject = function (value) {
38
+ }
39
+ pushObject(value) {
85
40
  // @ts-expect-error this is technically invalid, but we'll just assume that if a format returns
86
41
  // an object, it'll be acting as some form of ReactNode.
87
42
  this.result.push(value);
88
- };
89
- class_1.prototype.finish = function () {
43
+ }
44
+ finish() {
90
45
  return this.result;
91
- };
92
- return class_1;
93
- }(format_1.FormatBuilder));
46
+ }
47
+ };
94
48
  }
95
49
  function formatReact(message, values, Builder) {
96
50
  if (typeof message === 'string') {
97
51
  return [message];
98
52
  }
99
- var parts = this.bindFormatValues(Builder, message, values);
53
+ const parts = this.bindFormatValues(Builder, message, values);
100
54
  return parts;
101
55
  }
102
56
  /**
@@ -1,56 +1,14 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
2
  Object.defineProperty(exports, "__esModule", { value: true });
43
3
  exports.stringFormatter = exports.StringBuilder = void 0;
44
4
  exports.formatToPlainString = formatToPlainString;
45
- var format_1 = require("../format");
46
- var StringBuilder = /** @class */ (function (_super) {
47
- __extends(StringBuilder, _super);
48
- function StringBuilder() {
49
- var _this = _super.apply(this, __spreadArray([], __read(arguments), false)) || this;
50
- _this.result = '';
51
- return _this;
5
+ const format_1 = require("../format");
6
+ class StringBuilder extends format_1.FormatBuilder {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.result = '';
52
10
  }
53
- StringBuilder.prototype.pushRichTextTag = function (tag, children) {
11
+ pushRichTextTag(tag, children) {
54
12
  // Plain string formatting ignores rich text tags and just takes the
55
13
  // visible content from the children. This means the `target` element
56
14
  // of a link is ignored, otherwise all the children are just joined
@@ -60,27 +18,26 @@ var StringBuilder = /** @class */ (function (_super) {
60
18
  return;
61
19
  }
62
20
  this.result += children.join('');
63
- };
64
- StringBuilder.prototype.pushLiteralText = function (text) {
21
+ }
22
+ pushLiteralText(text) {
65
23
  this.result += text;
66
- };
67
- StringBuilder.prototype.pushObject = function (value) {
24
+ }
25
+ pushObject(value) {
68
26
  // Objects are only included in the result if they specify a toString value directly.
69
27
  // Otherwise, they would be rendered as `[object Object]`, which is never helpful.
70
28
  if (value != null && 'toString' in value) {
71
29
  this.result += value.toString();
72
30
  }
73
- };
74
- StringBuilder.prototype.finish = function () {
31
+ }
32
+ finish() {
75
33
  return [this.result];
76
- };
77
- return StringBuilder;
78
- }(format_1.FormatBuilder));
34
+ }
35
+ }
79
36
  exports.StringBuilder = StringBuilder;
80
37
  function formatToPlainString(message, values) {
81
38
  if (typeof message === 'string')
82
39
  return message;
83
- var result = this.bindFormatValues(StringBuilder, message, values);
40
+ const result = this.bindFormatValues(StringBuilder, message, values);
84
41
  // StringBuilder always creates a single element array with the string value.
85
42
  return result[0];
86
43
  }
package/dist/hash.js CHANGED
@@ -1,21 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runtimeHashMessageKey = runtimeHashMessageKey;
4
- var xxhash64_1 = require("@intrnl/xxhash64");
5
- var BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
6
- var IS_BIG_ENDIAN = (function () {
7
- var array = new Uint8Array(4);
8
- var view = new Uint32Array(array.buffer);
4
+ const xxhash64_1 = require("@intrnl/xxhash64");
5
+ const BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
6
+ const IS_BIG_ENDIAN = (() => {
7
+ const array = new Uint8Array(4);
8
+ const view = new Uint32Array(array.buffer);
9
9
  return !((view[0] = 1) & array[0]);
10
10
  })();
11
11
  function numberToBytes(number) {
12
12
  number = BigInt(number);
13
- var array = [];
14
- var byteCount = Math.ceil(Math.floor(Math.log2(Number(number)) + 1) / 8);
15
- for (var i = 0; i < byteCount; i++) {
13
+ const array = [];
14
+ const byteCount = Math.ceil(Math.floor(Math.log2(Number(number)) + 1) / 8);
15
+ for (let i = 0; i < byteCount; i++) {
16
16
  array.unshift(Number((number >> BigInt(8 * i)) & BigInt(255)));
17
17
  }
18
- var bytes = new Uint8Array(array);
18
+ const bytes = new Uint8Array(array);
19
19
  // The native `hashToMessageKey` always works in Big/Network Endian bytes, so this array
20
20
  // needs to be converted to the same endianness to get the same base64 result.
21
21
  return IS_BIG_ENDIAN ? bytes : bytes.reverse();
@@ -28,8 +28,8 @@ function numberToBytes(number) {
28
28
  * `@discord/intl-loader-core`, to be able to hash names at runtime.
29
29
  */
30
30
  function runtimeHashMessageKey(key) {
31
- var hash = (0, xxhash64_1.hash)(key, 0);
32
- var bytes = numberToBytes(hash);
31
+ const hash = (0, xxhash64_1.hash)(key, 0);
32
+ const bytes = numberToBytes(hash);
33
33
  return [
34
34
  BASE64_TABLE[bytes[0] >> 2],
35
35
  BASE64_TABLE[((bytes[0] & 0x03) << 4) | (bytes[1] >> 4)],
@@ -1,58 +1,30 @@
1
1
  "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
9
- }
10
- };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
2
  Object.defineProperty(exports, "__esModule", { value: true });
30
3
  exports.IntlManager = exports.DEFAULT_LOCALE = void 0;
31
- var intl_1 = require("@formatjs/intl");
32
- var format_1 = require("./format");
33
- var format_config_1 = require("./format-config");
4
+ const intl_ast_1 = require("@discord/intl-ast");
5
+ const intl_1 = require("@formatjs/intl");
6
+ const format_1 = require("./format");
7
+ const format_config_1 = require("./format-config");
34
8
  /**
35
9
  * Fallback locale used for all internationalization when an operation in the
36
10
  * requested locale is not possible.
37
11
  */
38
12
  exports.DEFAULT_LOCALE = 'en-US';
39
- var IntlManager = /** @class */ (function () {
40
- function IntlManager(defaultLocale) {
41
- if (defaultLocale === void 0) { defaultLocale = exports.DEFAULT_LOCALE; }
42
- var _this = this;
13
+ class IntlManager {
14
+ constructor(defaultLocale = exports.DEFAULT_LOCALE) {
43
15
  /**
44
16
  * Subscribe to changes in the current locale for this manager.
45
17
  */
46
- this.onLocaleChange = function (callback) {
47
- _this._localeSubscriptions.add(callback);
48
- return function () { return _this._localeSubscriptions.delete(callback); };
18
+ this.onLocaleChange = (callback) => {
19
+ this._localeSubscriptions.add(callback);
20
+ return () => this._localeSubscriptions.delete(callback);
49
21
  };
50
22
  this.defaultLocale = defaultLocale;
51
23
  this.currentLocale = defaultLocale;
52
24
  this.formatConfig = format_config_1.DEFAULT_FORMAT_CONFIG;
53
25
  this.intl = (0, intl_1.createIntl)({
54
26
  formats: format_config_1.DEFAULT_FORMAT_CONFIG,
55
- defaultLocale: defaultLocale,
27
+ defaultLocale,
56
28
  locale: defaultLocale,
57
29
  });
58
30
  this._localeSubscriptions = new Set();
@@ -61,74 +33,52 @@ var IntlManager = /** @class */ (function () {
61
33
  * Add a set of formatter implementations to this manager, making each available as a direct
62
34
  * property
63
35
  */
64
- IntlManager.prototype.withFormatters = function (formatters) {
65
- var e_1, _a;
66
- try {
67
- for (var _b = __values(Object.entries(formatters)), _c = _b.next(); !_c.done; _c = _b.next()) {
68
- var _d = __read(_c.value, 2), name = _d[0], formatter = _d[1];
69
- this[name] = this.makeFormatFunction(formatter);
70
- }
71
- }
72
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
73
- finally {
74
- try {
75
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
76
- }
77
- finally { if (e_1) throw e_1.error; }
36
+ withFormatters(formatters) {
37
+ for (const [name, formatter] of Object.entries(formatters)) {
38
+ this[name] = this.makeFormatFunction(formatter);
78
39
  }
79
40
  return this;
80
- };
41
+ }
81
42
  /**
82
43
  * Return a new function bound to this manager that uses the given `FormatterImplementation` to
83
44
  * format a message after it has been resolved for the current locale and potentially processed in
84
45
  * other ways by the manager.
85
46
  */
86
- IntlManager.prototype.makeFormatFunction = function (_a) {
87
- var _this = this;
88
- var format = _a.format, builder = _a.builder;
89
- var formatter = format.bind(this);
90
- return function (message, values) {
91
- return formatter(message(_this.currentLocale), values, builder);
47
+ makeFormatFunction({ format, builder, }) {
48
+ const formatter = format.bind(this);
49
+ return (message, values) => {
50
+ return formatter(message(this.currentLocale), values, builder);
92
51
  };
93
- };
52
+ }
94
53
  /**
95
54
  * Set the locale for this package to use. This is a global change, and all
96
55
  * methods on this object will use this locale for formatting.
97
56
  */
98
- IntlManager.prototype.setLocale = function (locale) {
57
+ setLocale(locale) {
99
58
  this.currentLocale = locale;
100
- this.intl = (0, intl_1.createIntl)({ defaultLocale: this.defaultLocale, locale: locale });
59
+ this.intl = (0, intl_1.createIntl)({ defaultLocale: this.defaultLocale, locale });
101
60
  this.emitLocaleChange(locale);
102
- };
103
- IntlManager.prototype.emitLocaleChange = function (locale) {
104
- var e_2, _a;
105
- try {
106
- for (var _b = __values(this._localeSubscriptions), _c = _b.next(); !_c.done; _c = _b.next()) {
107
- var callback = _c.value;
108
- callback(locale);
109
- }
110
- }
111
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
112
- finally {
113
- try {
114
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
115
- }
116
- finally { if (e_2) throw e_2.error; }
61
+ }
62
+ emitLocaleChange(locale) {
63
+ for (const callback of this._localeSubscriptions) {
64
+ callback(locale);
117
65
  }
118
- };
66
+ }
119
67
  /**
120
68
  * For static messages with no rich text and no dynamic placeholders, use this method to
121
69
  * immediately return the plain string value of the message in the current locale.
122
70
  */
123
- IntlManager.prototype.string = function (message) {
124
- var resolved = message(this.currentLocale);
71
+ string(message) {
72
+ const resolved = message(this.currentLocale);
125
73
  if (resolved == null || resolved.ast.length === 0)
126
74
  return '';
127
75
  // TODO: Figure out how to make this typing exact. This currently relies on
128
76
  // the generic typing being sound enough to know that the message can only
129
77
  // contain a single static text node and no placeholders or rich text.
130
- return resolved.ast[0].value;
131
- };
78
+ // As it stands, this is _technically_ typed as string | undefined, even though
79
+ // TypeScript doesn't actually complain about it.
80
+ return resolved.ast[0][intl_ast_1.AstNodeIndices.Value];
81
+ }
132
82
  /**
133
83
  * Return a raw string representing the syntax of the original message, as
134
84
  * authored, with no values replaced. The result of this function could be
@@ -138,15 +88,14 @@ var IntlManager = /** @class */ (function () {
138
88
  * This should rarely be necessary outside of sending raw messages to other
139
89
  * applications that do their own message parsing.
140
90
  */
141
- IntlManager.prototype.reserialize = function (message) {
142
- var resolved = message(this.currentLocale);
91
+ reserialize(message) {
92
+ const resolved = message(this.currentLocale);
143
93
  if (typeof resolved === 'string')
144
94
  return resolved;
145
95
  return resolved.reserialize();
146
- };
147
- IntlManager.prototype.bindFormatValues = function (Builder, message, values) {
96
+ }
97
+ bindFormatValues(Builder, message, values) {
148
98
  return (0, format_1.bindFormatValues)(Builder, message.ast, [this.currentLocale, this.defaultLocale], this.intl.formatters, this.formatConfig, values);
149
- };
150
- return IntlManager;
151
- }());
99
+ }
100
+ }
152
101
  exports.IntlManager = IntlManager;
@@ -1,6 +1,12 @@
1
- import { FormatJsNode } from './keyless-json';
1
+ import { AstNode, FullFormatJsNode } from '@discord/intl-ast';
2
2
  import { InternalIntlMessage } from './message';
3
- type MessagesData = Record<string, FormatJsNode[]>;
3
+ /**
4
+ * Type representing the serialized content of a translations file, which is a record of hashed
5
+ * message keys to their AST structure. This type represents both compressed, keyless AstNodes as
6
+ * well as fully-typed, object FullFormatJsNodes as the message content, since either can be given
7
+ * depending on the configuration of the bundler/compiler.
8
+ */
9
+ type MessagesData = Record<string, AstNode[] | FullFormatJsNode[]>;
4
10
  export interface IntlMessageGetterAdditions {
5
11
  onChange(callback: () => void): () => void;
6
12
  }