@react-aria/i18n 3.12.16 → 3.13.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/dist/import.mjs +9 -11
- package/dist/main.js +19 -21
- package/dist/main.js.map +1 -1
- package/dist/module.js +9 -11
- package/dist/module.js.map +1 -1
- package/dist/{useMessageFormatter.main.js → private/useMessageFormatter.cjs} +15 -15
- package/dist/{useMessageFormatter.main.js.map → private/useMessageFormatter.cjs.map} +1 -1
- package/dist/private/useMessageFormatter.js +43 -0
- package/dist/{useMessageFormatter.module.js.map → private/useMessageFormatter.js.map} +1 -1
- package/dist/types/src/index.d.ts +15 -0
- package/dist/types/src/server.d.ts +1 -0
- package/dist/types/src/useMessageFormatter.d.ts +11 -0
- package/package.json +15 -21
- package/server/index.js +4 -72
- package/server/index.js.map +1 -1
- package/server/index.mjs +3 -67
- package/server/index.mjs.map +1 -1
- package/src/index.ts +11 -13
- package/src/server.ts +1 -0
- package/src/useMessageFormatter.ts +1 -1
- package/dist/context.main.js +0 -72
- package/dist/context.main.js.map +0 -1
- package/dist/context.mjs +0 -62
- package/dist/context.module.js +0 -62
- package/dist/context.module.js.map +0 -1
- package/dist/types.d.ts +0 -95
- package/dist/types.d.ts.map +0 -1
- package/dist/useCollator.main.js +0 -31
- package/dist/useCollator.main.js.map +0 -1
- package/dist/useCollator.mjs +0 -26
- package/dist/useCollator.module.js +0 -26
- package/dist/useCollator.module.js.map +0 -1
- package/dist/useDateFormatter.main.js +0 -47
- package/dist/useDateFormatter.main.js.map +0 -1
- package/dist/useDateFormatter.mjs +0 -42
- package/dist/useDateFormatter.module.js +0 -42
- package/dist/useDateFormatter.module.js.map +0 -1
- package/dist/useDefaultLocale.main.js +0 -70
- package/dist/useDefaultLocale.main.js.map +0 -1
- package/dist/useDefaultLocale.mjs +0 -65
- package/dist/useDefaultLocale.module.js +0 -65
- package/dist/useDefaultLocale.module.js.map +0 -1
- package/dist/useFilter.main.js +0 -72
- package/dist/useFilter.main.js.map +0 -1
- package/dist/useFilter.mjs +0 -67
- package/dist/useFilter.module.js +0 -67
- package/dist/useFilter.module.js.map +0 -1
- package/dist/useListFormatter.main.js +0 -31
- package/dist/useListFormatter.main.js.map +0 -1
- package/dist/useListFormatter.mjs +0 -26
- package/dist/useListFormatter.module.js +0 -26
- package/dist/useListFormatter.module.js.map +0 -1
- package/dist/useLocalizedStringFormatter.main.js +0 -47
- package/dist/useLocalizedStringFormatter.main.js.map +0 -1
- package/dist/useLocalizedStringFormatter.mjs +0 -41
- package/dist/useLocalizedStringFormatter.module.js +0 -41
- package/dist/useLocalizedStringFormatter.module.js.map +0 -1
- package/dist/useMessageFormatter.mjs +0 -43
- package/dist/useMessageFormatter.module.js +0 -43
- package/dist/useNumberFormatter.main.js +0 -33
- package/dist/useNumberFormatter.main.js.map +0 -1
- package/dist/useNumberFormatter.mjs +0 -28
- package/dist/useNumberFormatter.module.js +0 -28
- package/dist/useNumberFormatter.module.js.map +0 -1
- package/dist/utils.main.js +0 -70
- package/dist/utils.main.js.map +0 -1
- package/dist/utils.mjs +0 -65
- package/dist/utils.module.js +0 -65
- package/dist/utils.module.js.map +0 -1
- package/src/context.tsx +0 -87
- package/src/main.js +0 -6
- package/src/module.js +0 -6
- package/src/server.tsx +0 -99
- package/src/useCollator.ts +0 -33
- package/src/useDateFormatter.ts +0 -52
- package/src/useDefaultLocale.ts +0 -91
- package/src/useFilter.ts +0 -83
- package/src/useListFormatter.tsx +0 -24
- package/src/useLocalizedStringFormatter.ts +0 -44
- package/src/useNumberFormatter.ts +0 -25
- package/src/utils.ts +0 -43
package/src/server.tsx
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
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, {JSX} 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): JSX.Element | null {
|
|
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): string {
|
|
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
|
-
}
|
package/src/useCollator.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
}
|
package/src/useDateFormatter.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
}
|
package/src/useDefaultLocale.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
Intl.DateTimeFormat.supportedLocalesOf([locale]);
|
|
39
|
-
} catch {
|
|
40
|
-
locale = 'en-US';
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
locale,
|
|
44
|
-
direction: isRTL(locale) ? 'rtl' : 'ltr'
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let currentLocale = getDefaultLocale();
|
|
49
|
-
let listeners = new Set<(locale: Locale) => void>();
|
|
50
|
-
|
|
51
|
-
function updateLocale() {
|
|
52
|
-
currentLocale = getDefaultLocale();
|
|
53
|
-
for (let listener of listeners) {
|
|
54
|
-
listener(currentLocale);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Returns the current browser/system language, and updates when it changes.
|
|
60
|
-
*/
|
|
61
|
-
export function useDefaultLocale(): Locale {
|
|
62
|
-
let isSSR = useIsSSR();
|
|
63
|
-
let [defaultLocale, setDefaultLocale] = useState(currentLocale);
|
|
64
|
-
|
|
65
|
-
useEffect(() => {
|
|
66
|
-
if (listeners.size === 0) {
|
|
67
|
-
window.addEventListener('languagechange', updateLocale);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
listeners.add(setDefaultLocale);
|
|
71
|
-
|
|
72
|
-
return () => {
|
|
73
|
-
listeners.delete(setDefaultLocale);
|
|
74
|
-
if (listeners.size === 0) {
|
|
75
|
-
window.removeEventListener('languagechange', updateLocale);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
}, []);
|
|
79
|
-
|
|
80
|
-
// We cannot determine the browser's language on the server, so default to
|
|
81
|
-
// en-US. This will be updated after hydration on the client to the correct value.
|
|
82
|
-
if (isSSR) {
|
|
83
|
-
let locale = typeof window !== 'undefined' && window[localeSymbol];
|
|
84
|
-
return {
|
|
85
|
-
locale: locale || 'en-US',
|
|
86
|
-
direction: 'ltr'
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return defaultLocale;
|
|
91
|
-
}
|
package/src/useFilter.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
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
|
-
}
|
package/src/useListFormatter.tsx
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2022 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 {LocalizedString, LocalizedStringDictionary, LocalizedStringFormatter, LocalizedStrings} from '@internationalized/string';
|
|
14
|
-
import {useLocale} from './context';
|
|
15
|
-
import {useMemo} from 'react';
|
|
16
|
-
|
|
17
|
-
const cache = new WeakMap();
|
|
18
|
-
function getCachedDictionary<K extends string, T extends LocalizedString>(strings: LocalizedStrings<K, T>): LocalizedStringDictionary<K, T> {
|
|
19
|
-
let dictionary = cache.get(strings);
|
|
20
|
-
if (!dictionary) {
|
|
21
|
-
dictionary = new LocalizedStringDictionary(strings);
|
|
22
|
-
cache.set(strings, dictionary);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return dictionary;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Returns a cached LocalizedStringDictionary for the given strings.
|
|
30
|
-
*/
|
|
31
|
-
export function useLocalizedStringDictionary<K extends string = string, T extends LocalizedString = string>(strings: LocalizedStrings<K, T>, packageName?: string): LocalizedStringDictionary<K, T> {
|
|
32
|
-
return (packageName && LocalizedStringDictionary.getGlobalDictionaryForPackage(packageName)) || getCachedDictionary(strings);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Provides localized string formatting for the current locale. Supports interpolating variables,
|
|
37
|
-
* selecting the correct pluralization, and formatting numbers. Automatically updates when the locale changes.
|
|
38
|
-
* @param strings - A mapping of languages to localized strings by key.
|
|
39
|
-
*/
|
|
40
|
-
export function useLocalizedStringFormatter<K extends string = string, T extends LocalizedString = string>(strings: LocalizedStrings<K, T>, packageName?: string): LocalizedStringFormatter<K, T> {
|
|
41
|
-
let {locale} = useLocale();
|
|
42
|
-
let dictionary = useLocalizedStringDictionary(strings, packageName);
|
|
43
|
-
return useMemo(() => new LocalizedStringFormatter(locale, dictionary), [locale, dictionary]);
|
|
44
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
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 {NumberFormatOptions, NumberFormatter} from '@internationalized/number';
|
|
14
|
-
import {useLocale} from './context';
|
|
15
|
-
import {useMemo} from 'react';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Provides localized number formatting for the current locale. Automatically updates when the locale changes,
|
|
19
|
-
* and handles caching of the number formatter for performance.
|
|
20
|
-
* @param options - Formatting options.
|
|
21
|
-
*/
|
|
22
|
-
export function useNumberFormatter(options: NumberFormatOptions = {}): Intl.NumberFormat {
|
|
23
|
-
let {locale} = useLocale();
|
|
24
|
-
return useMemo(() => new NumberFormatter(locale, options), [locale, options]);
|
|
25
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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
|
-
// https://en.wikipedia.org/wiki/Right-to-left
|
|
14
|
-
const RTL_SCRIPTS = new Set(['Arab', 'Syrc', 'Samr', 'Mand', 'Thaa', 'Mend', 'Nkoo', 'Adlm', 'Rohg', 'Hebr']);
|
|
15
|
-
const RTL_LANGS = new Set(['ae', 'ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', 'he', 'ku', 'mzn', 'nqo', 'pnb', 'ps', 'sd', 'ug', 'ur', 'yi']);
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Determines if a locale is read right to left using [Intl.Locale]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale}.
|
|
19
|
-
*/
|
|
20
|
-
export function isRTL(localeString: string): boolean {
|
|
21
|
-
// If the Intl.Locale API is available, use it to get the locale's text direction.
|
|
22
|
-
if (Intl.Locale) {
|
|
23
|
-
let locale = new Intl.Locale(localeString).maximize();
|
|
24
|
-
|
|
25
|
-
// Use the text info object to get the direction if possible.
|
|
26
|
-
// @ts-ignore - this was implemented as a property by some browsers before it was standardized as a function.
|
|
27
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo
|
|
28
|
-
let textInfo = typeof locale.getTextInfo === 'function' ? locale.getTextInfo() : locale.textInfo;
|
|
29
|
-
if (textInfo) {
|
|
30
|
-
return textInfo.direction === 'rtl';
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Fallback: guess using the script.
|
|
34
|
-
// This is more accurate than guessing by language, since languages can be written in multiple scripts.
|
|
35
|
-
if (locale.script) {
|
|
36
|
-
return RTL_SCRIPTS.has(locale.script);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// If not, just guess by the language (first part of the locale)
|
|
41
|
-
let lang = localeString.split('-')[0];
|
|
42
|
-
return RTL_LANGS.has(lang);
|
|
43
|
-
}
|