@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
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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 {LocalizedStrings, MessageDictionary, MessageFormatter} from '@internationalized/message';
|
|
14
|
+
import {useCallback, useMemo} from 'react';
|
|
15
|
+
import {useLocale} from './context';
|
|
16
|
+
|
|
17
|
+
export type FormatMessage = (key: string, variables?: {[key: string]: any}) => string;
|
|
18
|
+
|
|
19
|
+
const cache = new WeakMap();
|
|
20
|
+
function getCachedDictionary(strings: LocalizedStrings) {
|
|
21
|
+
let dictionary = cache.get(strings);
|
|
22
|
+
if (!dictionary) {
|
|
23
|
+
dictionary = new MessageDictionary(strings);
|
|
24
|
+
cache.set(strings, dictionary);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return dictionary;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Handles formatting ICU Message strings to create localized strings for the current locale.
|
|
32
|
+
* Automatically updates when the locale changes, and handles caching of messages for performance.
|
|
33
|
+
* @param strings - A mapping of languages to strings by key.
|
|
34
|
+
* @deprecated - use useLocalizedStringFormatter instead.
|
|
35
|
+
*/
|
|
36
|
+
export function useMessageFormatter(strings: LocalizedStrings): FormatMessage {
|
|
37
|
+
let {locale} = useLocale();
|
|
38
|
+
let dictionary = useMemo(() => getCachedDictionary(strings), [strings]);
|
|
39
|
+
let formatter = useMemo(() => new MessageFormatter(locale, dictionary), [locale, dictionary]);
|
|
40
|
+
return useCallback((key, variables) => formatter.format(key, variables), [formatter]);
|
|
41
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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) {
|
|
21
|
+
// If the Intl.Locale API is available, use it to get the locale's text direction.
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
if (Intl.Locale) {
|
|
24
|
+
let locale = new Intl.Locale(localeString).maximize();
|
|
25
|
+
|
|
26
|
+
// Use the text info object to get the direction if possible.
|
|
27
|
+
// @ts-ignore - this was implemented as a property by some browsers before it was standardized as a function.
|
|
28
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo
|
|
29
|
+
let textInfo = typeof locale.getTextInfo === 'function' ? locale.getTextInfo() : locale.textInfo;
|
|
30
|
+
if (textInfo) {
|
|
31
|
+
return textInfo.direction === 'rtl';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Fallback: guess using the script.
|
|
35
|
+
// This is more accurate than guessing by language, since languages can be written in multiple scripts.
|
|
36
|
+
if (locale.script) {
|
|
37
|
+
return RTL_SCRIPTS.has(locale.script);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// If not, just guess by the language (first part of the locale)
|
|
42
|
+
let lang = localeString.split('-')[0];
|
|
43
|
+
return RTL_LANGS.has(lang);
|
|
44
|
+
}
|