@react-aria/i18n 3.0.0-nightly-641446f65-240905
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/README.md +3 -0
- package/dist/context.main.js +54 -0
- package/dist/context.main.js.map +1 -0
- package/dist/context.mjs +44 -0
- package/dist/context.module.js +44 -0
- package/dist/context.module.js.map +1 -0
- package/dist/import.mjs +31 -0
- package/dist/main.js +45 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +31 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +91 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/useCollator.main.js +31 -0
- package/dist/useCollator.main.js.map +1 -0
- package/dist/useCollator.mjs +26 -0
- package/dist/useCollator.module.js +26 -0
- package/dist/useCollator.module.js.map +1 -0
- package/dist/useDateFormatter.main.js +47 -0
- package/dist/useDateFormatter.main.js.map +1 -0
- package/dist/useDateFormatter.mjs +42 -0
- package/dist/useDateFormatter.module.js +42 -0
- package/dist/useDateFormatter.module.js.map +1 -0
- package/dist/useDefaultLocale.main.js +68 -0
- package/dist/useDefaultLocale.main.js.map +1 -0
- package/dist/useDefaultLocale.mjs +63 -0
- package/dist/useDefaultLocale.module.js +63 -0
- package/dist/useDefaultLocale.module.js.map +1 -0
- package/dist/useFilter.main.js +72 -0
- package/dist/useFilter.main.js.map +1 -0
- package/dist/useFilter.mjs +67 -0
- package/dist/useFilter.module.js +67 -0
- package/dist/useFilter.module.js.map +1 -0
- package/dist/useListFormatter.main.js +31 -0
- package/dist/useListFormatter.main.js.map +1 -0
- package/dist/useListFormatter.mjs +26 -0
- package/dist/useListFormatter.module.js +26 -0
- package/dist/useListFormatter.module.js.map +1 -0
- package/dist/useLocalizedStringFormatter.main.js +47 -0
- package/dist/useLocalizedStringFormatter.main.js.map +1 -0
- package/dist/useLocalizedStringFormatter.mjs +41 -0
- package/dist/useLocalizedStringFormatter.module.js +41 -0
- package/dist/useLocalizedStringFormatter.module.js.map +1 -0
- package/dist/useMessageFormatter.main.js +48 -0
- package/dist/useMessageFormatter.main.js.map +1 -0
- package/dist/useMessageFormatter.mjs +43 -0
- package/dist/useMessageFormatter.module.js +43 -0
- package/dist/useMessageFormatter.module.js.map +1 -0
- package/dist/useNumberFormatter.main.js +33 -0
- package/dist/useNumberFormatter.main.js.map +1 -0
- package/dist/useNumberFormatter.mjs +28 -0
- package/dist/useNumberFormatter.module.js +28 -0
- package/dist/useNumberFormatter.module.js.map +1 -0
- package/dist/utils.main.js +71 -0
- package/dist/utils.main.js.map +1 -0
- package/dist/utils.mjs +66 -0
- package/dist/utils.module.js +66 -0
- package/dist/utils.module.js.map +1 -0
- package/package.json +66 -0
- package/server/index.js +81 -0
- package/server/index.js.map +1 -0
- package/server/index.mjs +71 -0
- package/server/index.mjs.map +1 -0
- package/src/context.tsx +58 -0
- package/src/index.ts +29 -0
- package/src/main.js +6 -0
- package/src/module.js +6 -0
- package/src/server.tsx +99 -0
- package/src/useCollator.ts +33 -0
- package/src/useDateFormatter.ts +52 -0
- package/src/useDefaultLocale.ts +91 -0
- package/src/useFilter.ts +83 -0
- package/src/useListFormatter.tsx +24 -0
- package/src/useLocalizedStringFormatter.ts +44 -0
- package/src/useMessageFormatter.ts +41 -0
- package/src/useNumberFormatter.ts +25 -0
- package/src/utils.ts +44 -0
package/server/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
var $kg1kr$react = require("react");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$interopDefault(a) {
|
|
5
|
+
return a && a.__esModule ? a.default : a;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function $parcel$export(e, n, v, s) {
|
|
9
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
$parcel$export(module.exports, "PackageLocalizationProvider", () => $417d44b6975b2ee7$export$9ec1fc026a5460ba);
|
|
13
|
+
$parcel$export(module.exports, "getPackageLocalizationScript", () => $417d44b6975b2ee7$export$fe9518edbbdc73da);
|
|
14
|
+
/*
|
|
15
|
+
* Copyright 2023 Adobe. All rights reserved.
|
|
16
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
17
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
18
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
19
|
+
*
|
|
20
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
21
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
22
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
23
|
+
* governing permissions and limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
function $417d44b6975b2ee7$export$9ec1fc026a5460ba(props) {
|
|
26
|
+
if (typeof document !== 'undefined') {
|
|
27
|
+
console.log('PackageLocalizationProvider should only be rendered on the server.');
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
let { nonce: nonce, locale: locale, strings: strings } = props;
|
|
31
|
+
// suppressHydrationWarning is necessary because the browser
|
|
32
|
+
// remove the nonce parameter from the DOM before hydration
|
|
33
|
+
return /*#__PURE__*/ (0, ($parcel$interopDefault($kg1kr$react))).createElement("script", {
|
|
34
|
+
nonce: typeof window === 'undefined' ? nonce : '',
|
|
35
|
+
suppressHydrationWarning: true,
|
|
36
|
+
dangerouslySetInnerHTML: {
|
|
37
|
+
__html: $417d44b6975b2ee7$export$fe9518edbbdc73da(locale, strings)
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function $417d44b6975b2ee7$export$fe9518edbbdc73da(locale, strings) {
|
|
42
|
+
return `window[Symbol.for('react-aria.i18n.locale')]=${JSON.stringify(locale)};{${$417d44b6975b2ee7$var$serialize(strings)}}`;
|
|
43
|
+
}
|
|
44
|
+
const $417d44b6975b2ee7$var$cache = new WeakMap();
|
|
45
|
+
function $417d44b6975b2ee7$var$serialize(strings) {
|
|
46
|
+
let cached = $417d44b6975b2ee7$var$cache.get(strings);
|
|
47
|
+
if (cached) return cached;
|
|
48
|
+
// Find common strings between packages and hoist them into variables.
|
|
49
|
+
let seen = new Set();
|
|
50
|
+
let common = new Map();
|
|
51
|
+
for(let pkg in strings)for(let key in strings[pkg]){
|
|
52
|
+
let v = strings[pkg][key];
|
|
53
|
+
let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();
|
|
54
|
+
if (seen.has(s) && !common.has(s)) {
|
|
55
|
+
let name = String.fromCharCode(common.size > 25 ? common.size + 97 : common.size + 65);
|
|
56
|
+
common.set(s, name);
|
|
57
|
+
}
|
|
58
|
+
seen.add(s);
|
|
59
|
+
}
|
|
60
|
+
let res = '';
|
|
61
|
+
if (common.size > 0) res += 'let ';
|
|
62
|
+
for (let [string, name] of common)res += `${name}=${string},`;
|
|
63
|
+
if (common.size > 0) res = res.slice(0, -1) + ';';
|
|
64
|
+
res += "window[Symbol.for('react-aria.i18n.strings')]={";
|
|
65
|
+
for(let pkg in strings){
|
|
66
|
+
res += `'${pkg}':{`;
|
|
67
|
+
for(let key in strings[pkg]){
|
|
68
|
+
let v = strings[pkg][key];
|
|
69
|
+
let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();
|
|
70
|
+
if (common.has(s)) s = common.get(s);
|
|
71
|
+
res += `${/[ ()]/.test(key) ? JSON.stringify(key) : key}:${s},`;
|
|
72
|
+
}
|
|
73
|
+
res = res.slice(0, -1) + '},';
|
|
74
|
+
}
|
|
75
|
+
res = res.slice(0, -1) + '};';
|
|
76
|
+
$417d44b6975b2ee7$var$cache.set(strings, res);
|
|
77
|
+
return res;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;AAmBM,SAAS,0CAA4B,KAAuC;IACjF,IAAI,OAAO,aAAa,aAAa;QACnC,QAAQ,GAAG,CAAC;QACZ,OAAO;IACT;IAEA,IAAI,SAAC,KAAK,UAAE,MAAM,WAAE,OAAO,EAAC,GAAG;IAC/B,4DAA4D;IAC5D,2DAA2D;IAC3D,qBAAO,0DAAC;QAAO,OAAO,OAAO,WAAW,cAAc,QAAQ;QAAI,0BAAA;QAAyB,yBAAyB;YAAC,QAAQ,0CAA6B,QAAQ;QAAQ;;AAC5K;AAKO,SAAS,0CAA6B,MAAc,EAAE,OAAgC;IAC3F,OAAO,CAAC,6CAA6C,EAAE,KAAK,SAAS,CAAC,QAAQ,EAAE,EAAE,gCAAU,SAAS,CAAC,CAAC;AACzG;AAEA,MAAM,8BAAQ,IAAI;AAElB,SAAS,gCAAU,OAAgC;IACjD,IAAI,SAAS,4BAAM,GAAG,CAAC;IACvB,IAAI,QACF,OAAO;IAGT,sEAAsE;IACtE,IAAI,OAAO,IAAI;IACf,IAAI,SAAS,IAAI;IACjB,IAAK,IAAI,OAAO,QACd,IAAK,IAAI,OAAO,OAAO,CAAC,IAAI,CAAE;QAC5B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI;QACzB,IAAI,IAAI,OAAO,MAAM,WAAW,KAAK,SAAS,CAAC,KAAK,EAAE,QAAQ;QAC9D,IAAI,KAAK,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,CAAC,IAAI;YACjC,IAAI,OAAO,OAAO,YAAY,CAAC,OAAO,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG;YACnF,OAAO,GAAG,CAAC,GAAG;QAChB;QACA,KAAK,GAAG,CAAC;IACX;IAGF,IAAI,MAAM;IACV,IAAI,OAAO,IAAI,GAAG,GAChB,OAAO;IAET,KAAK,IAAI,CAAC,QAAQ,KAAK,IAAI,OACzB,OAAO,CAAC,EAAE,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;IAE7B,IAAI,OAAO,IAAI,GAAG,GAChB,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM;IAG3B,OAAO;IACP,IAAK,IAAI,OAAO,QAAS;QACvB,OAAO,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC;QACnB,IAAK,IAAI,OAAO,OAAO,CAAC,IAAI,CAAE;YAC5B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI;YACzB,IAAI,IAAI,OAAO,MAAM,WAAW,KAAK,SAAS,CAAC,KAAK,EAAE,QAAQ;YAC9D,IAAI,OAAO,GAAG,CAAC,IACb,IAAI,OAAO,GAAG,CAAC;YAEjB,OAAO,CAAC,EAAE,QAAQ,IAAI,CAAC,OAAO,KAAK,SAAS,CAAC,OAAO,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;QACjE;QACA,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM;IAC3B;IACA,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM;IACzB,4BAAM,GAAG,CAAC,SAAS;IACnB,OAAO;AACT","sources":["packages/@react-aria/i18n/src/server.tsx"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport type {LocalizedString} from '@internationalized/string';\nimport React from 'react';\n\ntype PackageLocalizedStrings = {\n [packageName: string]: Record<string, LocalizedString>\n};\n\ninterface PackageLocalizationProviderProps {\n locale: string,\n strings: PackageLocalizedStrings,\n nonce?: string\n}\n\n/**\n * A PackageLocalizationProvider can be rendered on the server to inject the localized strings\n * needed by the client into the initial HTML.\n */\nexport function PackageLocalizationProvider(props: PackageLocalizationProviderProps) {\n if (typeof document !== 'undefined') {\n console.log('PackageLocalizationProvider should only be rendered on the server.');\n return null;\n }\n\n let {nonce, locale, strings} = props;\n // suppressHydrationWarning is necessary because the browser\n // remove the nonce parameter from the DOM before hydration\n return <script nonce={typeof window === 'undefined' ? nonce : ''} suppressHydrationWarning dangerouslySetInnerHTML={{__html: getPackageLocalizationScript(locale, strings)}} />;\n}\n\n/**\n * Returns the content for an inline `<script>` tag to inject localized strings into initial HTML.\n */\nexport function getPackageLocalizationScript(locale: string, strings: PackageLocalizedStrings) {\n return `window[Symbol.for('react-aria.i18n.locale')]=${JSON.stringify(locale)};{${serialize(strings)}}`;\n}\n\nconst cache = new WeakMap<PackageLocalizedStrings, string>();\n\nfunction serialize(strings: PackageLocalizedStrings): string {\n let cached = cache.get(strings);\n if (cached) {\n return cached;\n }\n\n // Find common strings between packages and hoist them into variables.\n let seen = new Set();\n let common = new Map();\n for (let pkg in strings) {\n for (let key in strings[pkg]) {\n let v = strings[pkg][key];\n let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();\n if (seen.has(s) && !common.has(s)) {\n let name = String.fromCharCode(common.size > 25 ? common.size + 97 : common.size + 65);\n common.set(s, name);\n }\n seen.add(s);\n }\n }\n\n let res = '';\n if (common.size > 0) {\n res += 'let ';\n }\n for (let [string, name] of common) {\n res += `${name}=${string},`;\n }\n if (common.size > 0) {\n res = res.slice(0, -1) + ';';\n }\n\n res += \"window[Symbol.for('react-aria.i18n.strings')]={\";\n for (let pkg in strings) {\n res += `'${pkg}':{`;\n for (let key in strings[pkg]) {\n let v = strings[pkg][key];\n let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();\n if (common.has(s)) {\n s = common.get(s);\n }\n res += `${/[ ()]/.test(key) ? JSON.stringify(key) : key}:${s},`;\n }\n res = res.slice(0, -1) + '},';\n }\n res = res.slice(0, -1) + '};';\n cache.set(strings, res);\n return res;\n}\n"],"names":[],"version":3,"file":"index.js.map"}
|
package/server/index.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import $7cMCw$react from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
function $f4fda66de26cefb3$export$9ec1fc026a5460ba(props) {
|
|
15
|
+
if (typeof document !== 'undefined') {
|
|
16
|
+
console.log('PackageLocalizationProvider should only be rendered on the server.');
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
let { nonce: nonce, locale: locale, strings: strings } = props;
|
|
20
|
+
// suppressHydrationWarning is necessary because the browser
|
|
21
|
+
// remove the nonce parameter from the DOM before hydration
|
|
22
|
+
return /*#__PURE__*/ (0, $7cMCw$react).createElement("script", {
|
|
23
|
+
nonce: typeof window === 'undefined' ? nonce : '',
|
|
24
|
+
suppressHydrationWarning: true,
|
|
25
|
+
dangerouslySetInnerHTML: {
|
|
26
|
+
__html: $f4fda66de26cefb3$export$fe9518edbbdc73da(locale, strings)
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function $f4fda66de26cefb3$export$fe9518edbbdc73da(locale, strings) {
|
|
31
|
+
return `window[Symbol.for('react-aria.i18n.locale')]=${JSON.stringify(locale)};{${$f4fda66de26cefb3$var$serialize(strings)}}`;
|
|
32
|
+
}
|
|
33
|
+
const $f4fda66de26cefb3$var$cache = new WeakMap();
|
|
34
|
+
function $f4fda66de26cefb3$var$serialize(strings) {
|
|
35
|
+
let cached = $f4fda66de26cefb3$var$cache.get(strings);
|
|
36
|
+
if (cached) return cached;
|
|
37
|
+
// Find common strings between packages and hoist them into variables.
|
|
38
|
+
let seen = new Set();
|
|
39
|
+
let common = new Map();
|
|
40
|
+
for(let pkg in strings)for(let key in strings[pkg]){
|
|
41
|
+
let v = strings[pkg][key];
|
|
42
|
+
let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();
|
|
43
|
+
if (seen.has(s) && !common.has(s)) {
|
|
44
|
+
let name = String.fromCharCode(common.size > 25 ? common.size + 97 : common.size + 65);
|
|
45
|
+
common.set(s, name);
|
|
46
|
+
}
|
|
47
|
+
seen.add(s);
|
|
48
|
+
}
|
|
49
|
+
let res = '';
|
|
50
|
+
if (common.size > 0) res += 'let ';
|
|
51
|
+
for (let [string, name] of common)res += `${name}=${string},`;
|
|
52
|
+
if (common.size > 0) res = res.slice(0, -1) + ';';
|
|
53
|
+
res += "window[Symbol.for('react-aria.i18n.strings')]={";
|
|
54
|
+
for(let pkg in strings){
|
|
55
|
+
res += `'${pkg}':{`;
|
|
56
|
+
for(let key in strings[pkg]){
|
|
57
|
+
let v = strings[pkg][key];
|
|
58
|
+
let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();
|
|
59
|
+
if (common.has(s)) s = common.get(s);
|
|
60
|
+
res += `${/[ ()]/.test(key) ? JSON.stringify(key) : key}:${s},`;
|
|
61
|
+
}
|
|
62
|
+
res = res.slice(0, -1) + '},';
|
|
63
|
+
}
|
|
64
|
+
res = res.slice(0, -1) + '};';
|
|
65
|
+
$f4fda66de26cefb3$var$cache.set(strings, res);
|
|
66
|
+
return res;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
export {$f4fda66de26cefb3$export$9ec1fc026a5460ba as PackageLocalizationProvider, $f4fda66de26cefb3$export$fe9518edbbdc73da as getPackageLocalizationScript};
|
|
71
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AAmBM,SAAS,0CAA4B,KAAuC;IACjF,IAAI,OAAO,aAAa,aAAa;QACnC,QAAQ,GAAG,CAAC;QACZ,OAAO;IACT;IAEA,IAAI,SAAC,KAAK,UAAE,MAAM,WAAE,OAAO,EAAC,GAAG;IAC/B,4DAA4D;IAC5D,2DAA2D;IAC3D,qBAAO,gCAAC;QAAO,OAAO,OAAO,WAAW,cAAc,QAAQ;QAAI,0BAAA;QAAyB,yBAAyB;YAAC,QAAQ,0CAA6B,QAAQ;QAAQ;;AAC5K;AAKO,SAAS,0CAA6B,MAAc,EAAE,OAAgC;IAC3F,OAAO,CAAC,6CAA6C,EAAE,KAAK,SAAS,CAAC,QAAQ,EAAE,EAAE,gCAAU,SAAS,CAAC,CAAC;AACzG;AAEA,MAAM,8BAAQ,IAAI;AAElB,SAAS,gCAAU,OAAgC;IACjD,IAAI,SAAS,4BAAM,GAAG,CAAC;IACvB,IAAI,QACF,OAAO;IAGT,sEAAsE;IACtE,IAAI,OAAO,IAAI;IACf,IAAI,SAAS,IAAI;IACjB,IAAK,IAAI,OAAO,QACd,IAAK,IAAI,OAAO,OAAO,CAAC,IAAI,CAAE;QAC5B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI;QACzB,IAAI,IAAI,OAAO,MAAM,WAAW,KAAK,SAAS,CAAC,KAAK,EAAE,QAAQ;QAC9D,IAAI,KAAK,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,CAAC,IAAI;YACjC,IAAI,OAAO,OAAO,YAAY,CAAC,OAAO,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG;YACnF,OAAO,GAAG,CAAC,GAAG;QAChB;QACA,KAAK,GAAG,CAAC;IACX;IAGF,IAAI,MAAM;IACV,IAAI,OAAO,IAAI,GAAG,GAChB,OAAO;IAET,KAAK,IAAI,CAAC,QAAQ,KAAK,IAAI,OACzB,OAAO,CAAC,EAAE,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;IAE7B,IAAI,OAAO,IAAI,GAAG,GAChB,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM;IAG3B,OAAO;IACP,IAAK,IAAI,OAAO,QAAS;QACvB,OAAO,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC;QACnB,IAAK,IAAI,OAAO,OAAO,CAAC,IAAI,CAAE;YAC5B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI;YACzB,IAAI,IAAI,OAAO,MAAM,WAAW,KAAK,SAAS,CAAC,KAAK,EAAE,QAAQ;YAC9D,IAAI,OAAO,GAAG,CAAC,IACb,IAAI,OAAO,GAAG,CAAC;YAEjB,OAAO,CAAC,EAAE,QAAQ,IAAI,CAAC,OAAO,KAAK,SAAS,CAAC,OAAO,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;QACjE;QACA,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM;IAC3B;IACA,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM;IACzB,4BAAM,GAAG,CAAC,SAAS;IACnB,OAAO;AACT","sources":["packages/@react-aria/i18n/src/server.tsx"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport type {LocalizedString} from '@internationalized/string';\nimport React from 'react';\n\ntype PackageLocalizedStrings = {\n [packageName: string]: Record<string, LocalizedString>\n};\n\ninterface PackageLocalizationProviderProps {\n locale: string,\n strings: PackageLocalizedStrings,\n nonce?: string\n}\n\n/**\n * A PackageLocalizationProvider can be rendered on the server to inject the localized strings\n * needed by the client into the initial HTML.\n */\nexport function PackageLocalizationProvider(props: PackageLocalizationProviderProps) {\n if (typeof document !== 'undefined') {\n console.log('PackageLocalizationProvider should only be rendered on the server.');\n return null;\n }\n\n let {nonce, locale, strings} = props;\n // suppressHydrationWarning is necessary because the browser\n // remove the nonce parameter from the DOM before hydration\n return <script nonce={typeof window === 'undefined' ? nonce : ''} suppressHydrationWarning dangerouslySetInnerHTML={{__html: getPackageLocalizationScript(locale, strings)}} />;\n}\n\n/**\n * Returns the content for an inline `<script>` tag to inject localized strings into initial HTML.\n */\nexport function getPackageLocalizationScript(locale: string, strings: PackageLocalizedStrings) {\n return `window[Symbol.for('react-aria.i18n.locale')]=${JSON.stringify(locale)};{${serialize(strings)}}`;\n}\n\nconst cache = new WeakMap<PackageLocalizedStrings, string>();\n\nfunction serialize(strings: PackageLocalizedStrings): string {\n let cached = cache.get(strings);\n if (cached) {\n return cached;\n }\n\n // Find common strings between packages and hoist them into variables.\n let seen = new Set();\n let common = new Map();\n for (let pkg in strings) {\n for (let key in strings[pkg]) {\n let v = strings[pkg][key];\n let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();\n if (seen.has(s) && !common.has(s)) {\n let name = String.fromCharCode(common.size > 25 ? common.size + 97 : common.size + 65);\n common.set(s, name);\n }\n seen.add(s);\n }\n }\n\n let res = '';\n if (common.size > 0) {\n res += 'let ';\n }\n for (let [string, name] of common) {\n res += `${name}=${string},`;\n }\n if (common.size > 0) {\n res = res.slice(0, -1) + ';';\n }\n\n res += \"window[Symbol.for('react-aria.i18n.strings')]={\";\n for (let pkg in strings) {\n res += `'${pkg}':{`;\n for (let key in strings[pkg]) {\n let v = strings[pkg][key];\n let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();\n if (common.has(s)) {\n s = common.get(s);\n }\n res += `${/[ ()]/.test(key) ? JSON.stringify(key) : key}:${s},`;\n }\n res = res.slice(0, -1) + '},';\n }\n res = res.slice(0, -1) + '};';\n cache.set(strings, res);\n return res;\n}\n"],"names":[],"version":3,"file":"index.mjs.map"}
|
package/src/context.tsx
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {isRTL} from './utils';
|
|
14
|
+
import {Locale, useDefaultLocale} from './useDefaultLocale';
|
|
15
|
+
import React, {ReactNode, useContext} from 'react';
|
|
16
|
+
|
|
17
|
+
export interface I18nProviderProps {
|
|
18
|
+
/** Contents that should have the locale applied. */
|
|
19
|
+
children: ReactNode,
|
|
20
|
+
/** The locale to apply to the children. */
|
|
21
|
+
locale?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const I18nContext = React.createContext<Locale | null>(null);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Provides the locale for the application to all child components.
|
|
28
|
+
*/
|
|
29
|
+
export function I18nProvider(props: I18nProviderProps) {
|
|
30
|
+
let {locale, children} = props;
|
|
31
|
+
let defaultLocale = useDefaultLocale();
|
|
32
|
+
|
|
33
|
+
let value: Locale = React.useMemo(() => {
|
|
34
|
+
if (!locale) {
|
|
35
|
+
return defaultLocale;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
locale,
|
|
40
|
+
direction: isRTL(locale) ? 'rtl' : 'ltr'
|
|
41
|
+
};
|
|
42
|
+
}, [defaultLocale, locale]);
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<I18nContext.Provider value={value}>
|
|
46
|
+
{children}
|
|
47
|
+
</I18nContext.Provider>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns the current locale and layout direction.
|
|
53
|
+
*/
|
|
54
|
+
export function useLocale(): Locale {
|
|
55
|
+
let defaultLocale = useDefaultLocale();
|
|
56
|
+
let context = useContext(I18nContext);
|
|
57
|
+
return context || defaultLocale;
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export {I18nProvider, useLocale} from './context';
|
|
14
|
+
export {useMessageFormatter} from './useMessageFormatter';
|
|
15
|
+
export {useLocalizedStringFormatter, useLocalizedStringDictionary} from './useLocalizedStringFormatter';
|
|
16
|
+
export {useListFormatter} from './useListFormatter';
|
|
17
|
+
export {useDateFormatter} from './useDateFormatter';
|
|
18
|
+
export {useNumberFormatter} from './useNumberFormatter';
|
|
19
|
+
export {useCollator} from './useCollator';
|
|
20
|
+
export {useFilter} from './useFilter';
|
|
21
|
+
|
|
22
|
+
export type {FormatMessage} from './useMessageFormatter';
|
|
23
|
+
export type {LocalizedStringFormatter} from '@internationalized/string';
|
|
24
|
+
export type {I18nProviderProps} from './context';
|
|
25
|
+
export type {Locale} from './useDefaultLocale';
|
|
26
|
+
export type {LocalizedStrings} from '@internationalized/message';
|
|
27
|
+
export type {DateFormatterOptions} from './useDateFormatter';
|
|
28
|
+
export type {DateFormatter} from '@internationalized/date';
|
|
29
|
+
export type {Filter} from './useFilter';
|
package/src/main.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// useMessageFormatter is deprecated, but has a large dependency on intl-messageformat
|
|
2
|
+
// that we want to avoid. If it is built into the same file as the rest of the package,
|
|
3
|
+
// it deopts tree shaking in Parcel even when unused. Instead, it is split into a separate
|
|
4
|
+
// file and re-exported here, which allows tree shaking to work properly.
|
|
5
|
+
module.exports = require('./real-main.js');
|
|
6
|
+
Object.defineProperties(module.exports, Object.getOwnPropertyDescriptors(require('./useMessageFormatter.js')));
|
package/src/module.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// useMessageFormatter is deprecated, but has a large dependency on intl-messageformat
|
|
2
|
+
// that we want to avoid. If it is built into the same file as the rest of the package,
|
|
3
|
+
// it deopts tree shaking in Parcel even when unused. Instead, it is split into a separate
|
|
4
|
+
// file and re-exported here, which allows tree shaking to work properly.
|
|
5
|
+
export * from './real-module.js';
|
|
6
|
+
export * from './useMessageFormatter.module.js';
|
package/src/server.tsx
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type {LocalizedString} from '@internationalized/string';
|
|
14
|
+
import React from 'react';
|
|
15
|
+
|
|
16
|
+
type PackageLocalizedStrings = {
|
|
17
|
+
[packageName: string]: Record<string, LocalizedString>
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
interface PackageLocalizationProviderProps {
|
|
21
|
+
locale: string,
|
|
22
|
+
strings: PackageLocalizedStrings,
|
|
23
|
+
nonce?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A PackageLocalizationProvider can be rendered on the server to inject the localized strings
|
|
28
|
+
* needed by the client into the initial HTML.
|
|
29
|
+
*/
|
|
30
|
+
export function PackageLocalizationProvider(props: PackageLocalizationProviderProps) {
|
|
31
|
+
if (typeof document !== 'undefined') {
|
|
32
|
+
console.log('PackageLocalizationProvider should only be rendered on the server.');
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let {nonce, locale, strings} = props;
|
|
37
|
+
// suppressHydrationWarning is necessary because the browser
|
|
38
|
+
// remove the nonce parameter from the DOM before hydration
|
|
39
|
+
return <script nonce={typeof window === 'undefined' ? nonce : ''} suppressHydrationWarning dangerouslySetInnerHTML={{__html: getPackageLocalizationScript(locale, strings)}} />;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns the content for an inline `<script>` tag to inject localized strings into initial HTML.
|
|
44
|
+
*/
|
|
45
|
+
export function getPackageLocalizationScript(locale: string, strings: PackageLocalizedStrings) {
|
|
46
|
+
return `window[Symbol.for('react-aria.i18n.locale')]=${JSON.stringify(locale)};{${serialize(strings)}}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const cache = new WeakMap<PackageLocalizedStrings, string>();
|
|
50
|
+
|
|
51
|
+
function serialize(strings: PackageLocalizedStrings): string {
|
|
52
|
+
let cached = cache.get(strings);
|
|
53
|
+
if (cached) {
|
|
54
|
+
return cached;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Find common strings between packages and hoist them into variables.
|
|
58
|
+
let seen = new Set();
|
|
59
|
+
let common = new Map();
|
|
60
|
+
for (let pkg in strings) {
|
|
61
|
+
for (let key in strings[pkg]) {
|
|
62
|
+
let v = strings[pkg][key];
|
|
63
|
+
let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();
|
|
64
|
+
if (seen.has(s) && !common.has(s)) {
|
|
65
|
+
let name = String.fromCharCode(common.size > 25 ? common.size + 97 : common.size + 65);
|
|
66
|
+
common.set(s, name);
|
|
67
|
+
}
|
|
68
|
+
seen.add(s);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let res = '';
|
|
73
|
+
if (common.size > 0) {
|
|
74
|
+
res += 'let ';
|
|
75
|
+
}
|
|
76
|
+
for (let [string, name] of common) {
|
|
77
|
+
res += `${name}=${string},`;
|
|
78
|
+
}
|
|
79
|
+
if (common.size > 0) {
|
|
80
|
+
res = res.slice(0, -1) + ';';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
res += "window[Symbol.for('react-aria.i18n.strings')]={";
|
|
84
|
+
for (let pkg in strings) {
|
|
85
|
+
res += `'${pkg}':{`;
|
|
86
|
+
for (let key in strings[pkg]) {
|
|
87
|
+
let v = strings[pkg][key];
|
|
88
|
+
let s = typeof v === 'string' ? JSON.stringify(v) : v.toString();
|
|
89
|
+
if (common.has(s)) {
|
|
90
|
+
s = common.get(s);
|
|
91
|
+
}
|
|
92
|
+
res += `${/[ ()]/.test(key) ? JSON.stringify(key) : key}:${s},`;
|
|
93
|
+
}
|
|
94
|
+
res = res.slice(0, -1) + '},';
|
|
95
|
+
}
|
|
96
|
+
res = res.slice(0, -1) + '};';
|
|
97
|
+
cache.set(strings, res);
|
|
98
|
+
return res;
|
|
99
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {useLocale} from './context';
|
|
14
|
+
|
|
15
|
+
let cache = new Map<string, Intl.Collator>();
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Provides localized string collation for the current locale. Automatically updates when the locale changes,
|
|
19
|
+
* and handles caching of the collator for performance.
|
|
20
|
+
* @param options - Collator options.
|
|
21
|
+
*/
|
|
22
|
+
export function useCollator(options?: Intl.CollatorOptions): Intl.Collator {
|
|
23
|
+
let {locale} = useLocale();
|
|
24
|
+
|
|
25
|
+
let cacheKey = locale + (options ? Object.entries(options).sort((a, b) => a[0] < b[0] ? -1 : 1).join() : '');
|
|
26
|
+
if (cache.has(cacheKey)) {
|
|
27
|
+
return cache.get(cacheKey)!;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let formatter = new Intl.Collator(locale, options);
|
|
31
|
+
cache.set(cacheKey, formatter);
|
|
32
|
+
return formatter;
|
|
33
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {DateFormatter} from '@internationalized/date';
|
|
14
|
+
import {useDeepMemo} from '@react-aria/utils';
|
|
15
|
+
import {useLocale} from './context';
|
|
16
|
+
import {useMemo} from 'react';
|
|
17
|
+
|
|
18
|
+
export interface DateFormatterOptions extends Intl.DateTimeFormatOptions {
|
|
19
|
+
calendar?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Provides localized date formatting for the current locale. Automatically updates when the locale changes,
|
|
24
|
+
* and handles caching of the date formatter for performance.
|
|
25
|
+
* @param options - Formatting options.
|
|
26
|
+
*/
|
|
27
|
+
export function useDateFormatter(options?: DateFormatterOptions): DateFormatter {
|
|
28
|
+
// Reuse last options object if it is shallowly equal, which allows the useMemo result to also be reused.
|
|
29
|
+
options = useDeepMemo(options ?? {}, isEqual);
|
|
30
|
+
let {locale} = useLocale();
|
|
31
|
+
return useMemo(() => new DateFormatter(locale, options), [locale, options]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isEqual(a: DateFormatterOptions, b: DateFormatterOptions) {
|
|
35
|
+
if (a === b) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let aKeys = Object.keys(a);
|
|
40
|
+
let bKeys = Object.keys(b);
|
|
41
|
+
if (aKeys.length !== bKeys.length) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (let key of aKeys) {
|
|
46
|
+
if (b[key] !== a[key]) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {Direction} from '@react-types/shared';
|
|
14
|
+
import {isRTL} from './utils';
|
|
15
|
+
import {useEffect, useState} from 'react';
|
|
16
|
+
import {useIsSSR} from '@react-aria/ssr';
|
|
17
|
+
|
|
18
|
+
export interface Locale {
|
|
19
|
+
/** The [BCP47](https://www.ietf.org/rfc/bcp/bcp47.txt) language code for the locale. */
|
|
20
|
+
locale: string,
|
|
21
|
+
/** The writing direction for the locale. */
|
|
22
|
+
direction: Direction
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Locale passed from server by PackageLocalizationProvider.
|
|
26
|
+
const localeSymbol = Symbol.for('react-aria.i18n.locale');
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Gets the locale setting of the browser.
|
|
30
|
+
*/
|
|
31
|
+
export function getDefaultLocale(): Locale {
|
|
32
|
+
let locale = typeof window !== 'undefined' && window[localeSymbol]
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
|| (typeof navigator !== 'undefined' && (navigator.language || navigator.userLanguage))
|
|
35
|
+
|| 'en-US';
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
Intl.DateTimeFormat.supportedLocalesOf([locale]);
|
|
40
|
+
} catch (_err) {
|
|
41
|
+
locale = 'en-US';
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
locale,
|
|
45
|
+
direction: isRTL(locale) ? 'rtl' : 'ltr'
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let currentLocale = getDefaultLocale();
|
|
50
|
+
let listeners = new Set<(locale: Locale) => void>();
|
|
51
|
+
|
|
52
|
+
function updateLocale() {
|
|
53
|
+
currentLocale = getDefaultLocale();
|
|
54
|
+
for (let listener of listeners) {
|
|
55
|
+
listener(currentLocale);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Returns the current browser/system language, and updates when it changes.
|
|
61
|
+
*/
|
|
62
|
+
export function useDefaultLocale(): Locale {
|
|
63
|
+
let isSSR = useIsSSR();
|
|
64
|
+
let [defaultLocale, setDefaultLocale] = useState(currentLocale);
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (listeners.size === 0) {
|
|
68
|
+
window.addEventListener('languagechange', updateLocale);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
listeners.add(setDefaultLocale);
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
listeners.delete(setDefaultLocale);
|
|
75
|
+
if (listeners.size === 0) {
|
|
76
|
+
window.removeEventListener('languagechange', updateLocale);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
|
|
81
|
+
// We cannot determine the browser's language on the server, so default to
|
|
82
|
+
// en-US. This will be updated after hydration on the client to the correct value.
|
|
83
|
+
if (isSSR) {
|
|
84
|
+
return {
|
|
85
|
+
locale: 'en-US',
|
|
86
|
+
direction: 'ltr'
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return defaultLocale;
|
|
91
|
+
}
|
package/src/useFilter.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {useCallback, useMemo} from 'react';
|
|
14
|
+
import {useCollator} from './useCollator';
|
|
15
|
+
|
|
16
|
+
export interface Filter {
|
|
17
|
+
/** Returns whether a string starts with a given substring. */
|
|
18
|
+
startsWith(string: string, substring: string): boolean,
|
|
19
|
+
/** Returns whether a string ends with a given substring. */
|
|
20
|
+
endsWith(string: string, substring: string): boolean,
|
|
21
|
+
/** Returns whether a string contains a given substring. */
|
|
22
|
+
contains(string: string, substring: string): boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Provides localized string search functionality that is useful for filtering or matching items
|
|
27
|
+
* in a list. Options can be provided to adjust the sensitivity to case, diacritics, and other parameters.
|
|
28
|
+
*/
|
|
29
|
+
export function useFilter(options?: Intl.CollatorOptions): Filter {
|
|
30
|
+
let collator = useCollator({
|
|
31
|
+
usage: 'search',
|
|
32
|
+
...options
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// TODO(later): these methods don't currently support the ignorePunctuation option.
|
|
36
|
+
let startsWith = useCallback((string, substring) => {
|
|
37
|
+
if (substring.length === 0) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Normalize both strings so we can slice safely
|
|
42
|
+
// TODO: take into account the ignorePunctuation option as well...
|
|
43
|
+
string = string.normalize('NFC');
|
|
44
|
+
substring = substring.normalize('NFC');
|
|
45
|
+
return collator.compare(string.slice(0, substring.length), substring) === 0;
|
|
46
|
+
}, [collator]);
|
|
47
|
+
|
|
48
|
+
let endsWith = useCallback((string, substring) => {
|
|
49
|
+
if (substring.length === 0) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
string = string.normalize('NFC');
|
|
54
|
+
substring = substring.normalize('NFC');
|
|
55
|
+
return collator.compare(string.slice(-substring.length), substring) === 0;
|
|
56
|
+
}, [collator]);
|
|
57
|
+
|
|
58
|
+
let contains = useCallback((string, substring) => {
|
|
59
|
+
if (substring.length === 0) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
string = string.normalize('NFC');
|
|
64
|
+
substring = substring.normalize('NFC');
|
|
65
|
+
|
|
66
|
+
let scan = 0;
|
|
67
|
+
let sliceLen = substring.length;
|
|
68
|
+
for (; scan + sliceLen <= string.length; scan++) {
|
|
69
|
+
let slice = string.slice(scan, scan + sliceLen);
|
|
70
|
+
if (collator.compare(substring, slice) === 0) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return false;
|
|
76
|
+
}, [collator]);
|
|
77
|
+
|
|
78
|
+
return useMemo(() => ({
|
|
79
|
+
startsWith,
|
|
80
|
+
endsWith,
|
|
81
|
+
contains
|
|
82
|
+
}), [startsWith, endsWith, contains]);
|
|
83
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {useLocale} from './context';
|
|
14
|
+
import {useMemo} from 'react';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provides localized list formatting for the current locale. Automatically updates when the locale changes,
|
|
18
|
+
* and handles caching of the list formatter for performance.
|
|
19
|
+
* @param options - Formatting options.
|
|
20
|
+
*/
|
|
21
|
+
export function useListFormatter(options: Intl.ListFormatOptions = {}): Intl.ListFormat {
|
|
22
|
+
let {locale} = useLocale();
|
|
23
|
+
return useMemo(() => new Intl.ListFormat(locale, options), [locale, options]);
|
|
24
|
+
}
|