UNPKG

@react-aria/i18n

Version:
368 lines (332 loc) • 15.6 kB
import $iFADg$react, {useContext as $iFADg$useContext, useState as $iFADg$useState, useEffect as $iFADg$useEffect, useMemo as $iFADg$useMemo, useCallback as $iFADg$useCallback} from "react"; import {useIsSSR as $iFADg$useIsSSR} from "@react-aria/ssr"; import {LocalizedStringDictionary as $iFADg$LocalizedStringDictionary, LocalizedStringFormatter as $iFADg$LocalizedStringFormatter} from "@internationalized/string"; import {DateFormatter as $iFADg$DateFormatter} from "@internationalized/date"; import {useDeepMemo as $iFADg$useDeepMemo} from "@react-aria/utils"; import {NumberFormatter as $iFADg$NumberFormatter} from "@internationalized/number"; /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ // https://en.wikipedia.org/wiki/Right-to-left const $148a7a147e38ea7f$var$RTL_SCRIPTS = new Set([ "Arab", "Syrc", "Samr", "Mand", "Thaa", "Mend", "Nkoo", "Adlm", "Rohg", "Hebr" ]); const $148a7a147e38ea7f$var$RTL_LANGS = new Set([ "ae", "ar", "arc", "bcc", "bqi", "ckb", "dv", "fa", "glk", "he", "ku", "mzn", "nqo", "pnb", "ps", "sd", "ug", "ur", "yi" ]); function $148a7a147e38ea7f$export$702d680b21cbd764(locale) { // If the Intl.Locale API is available, use it to get the script for the locale. // This is more accurate than guessing by language, since languages can be written in multiple scripts. // @ts-ignore if (Intl.Locale) { // @ts-ignore let script = new Intl.Locale(locale).maximize().script; return $148a7a147e38ea7f$var$RTL_SCRIPTS.has(script); } // If not, just guess by the language (first part of the locale) let lang = locale.split("-")[0]; return $148a7a147e38ea7f$var$RTL_LANGS.has(lang); } /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ function $1e5a04cdaf7d1af8$export$f09106e7c6677ec5() { // @ts-ignore let locale = typeof navigator !== "undefined" && (navigator.language || navigator.userLanguage) || "en-US"; try { // @ts-ignore Intl.DateTimeFormat.supportedLocalesOf([ locale ]); } catch (_err) { locale = "en-US"; } return { locale: locale, direction: (0, $148a7a147e38ea7f$export$702d680b21cbd764)(locale) ? "rtl" : "ltr" }; } let $1e5a04cdaf7d1af8$var$currentLocale = $1e5a04cdaf7d1af8$export$f09106e7c6677ec5(); let $1e5a04cdaf7d1af8$var$listeners = new Set(); function $1e5a04cdaf7d1af8$var$updateLocale() { $1e5a04cdaf7d1af8$var$currentLocale = $1e5a04cdaf7d1af8$export$f09106e7c6677ec5(); for (let listener of $1e5a04cdaf7d1af8$var$listeners)listener($1e5a04cdaf7d1af8$var$currentLocale); } function $1e5a04cdaf7d1af8$export$188ec29ebc2bdc3a() { let isSSR = (0, $iFADg$useIsSSR)(); let [defaultLocale, setDefaultLocale] = (0, $iFADg$useState)($1e5a04cdaf7d1af8$var$currentLocale); (0, $iFADg$useEffect)(()=>{ if ($1e5a04cdaf7d1af8$var$listeners.size === 0) window.addEventListener("languagechange", $1e5a04cdaf7d1af8$var$updateLocale); $1e5a04cdaf7d1af8$var$listeners.add(setDefaultLocale); return ()=>{ $1e5a04cdaf7d1af8$var$listeners.delete(setDefaultLocale); if ($1e5a04cdaf7d1af8$var$listeners.size === 0) window.removeEventListener("languagechange", $1e5a04cdaf7d1af8$var$updateLocale); }; }, []); // We cannot determine the browser's language on the server, so default to // en-US. This will be updated after hydration on the client to the correct value. if (isSSR) return { locale: "en-US", direction: "ltr" }; return defaultLocale; } const $18f2051aff69b9bf$var$I18nContext = /*#__PURE__*/ (0, $iFADg$react).createContext(null); function $18f2051aff69b9bf$export$a54013f0d02a8f82(props) { let { locale: locale , children: children } = props; let defaultLocale = (0, $1e5a04cdaf7d1af8$export$188ec29ebc2bdc3a)(); let value = locale ? { locale: locale, direction: (0, $148a7a147e38ea7f$export$702d680b21cbd764)(locale) ? "rtl" : "ltr" } : defaultLocale; return /*#__PURE__*/ (0, $iFADg$react).createElement($18f2051aff69b9bf$var$I18nContext.Provider, { value: value }, children); } function $18f2051aff69b9bf$export$43bb16f9c6d9e3f7() { let defaultLocale = (0, $1e5a04cdaf7d1af8$export$188ec29ebc2bdc3a)(); let context = (0, $iFADg$useContext)($18f2051aff69b9bf$var$I18nContext); return context || defaultLocale; } var $2aa2084a6c2b6b4f$exports = {}; "use strict"; /* * Copyright 2022 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ const $fca6afa0e843324b$var$cache = new WeakMap(); function $fca6afa0e843324b$var$getCachedDictionary(strings) { let dictionary = $fca6afa0e843324b$var$cache.get(strings); if (!dictionary) { dictionary = new (0, $iFADg$LocalizedStringDictionary)(strings); $fca6afa0e843324b$var$cache.set(strings, dictionary); } return dictionary; } function $fca6afa0e843324b$export$f12b703ca79dfbb1(strings) { let { locale: locale } = (0, $18f2051aff69b9bf$export$43bb16f9c6d9e3f7)(); let dictionary = (0, $iFADg$useMemo)(()=>$fca6afa0e843324b$var$getCachedDictionary(strings), [ strings ]); return (0, $iFADg$useMemo)(()=>new (0, $iFADg$LocalizedStringFormatter)(locale, dictionary), [ locale, dictionary ]); } /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ function $33bf17300c498528$export$a2f47a3d2973640(options = {}) { let { locale: locale } = (0, $18f2051aff69b9bf$export$43bb16f9c6d9e3f7)(); // @ts-ignore return (0, $iFADg$useMemo)(()=>new Intl.ListFormat(locale, options), [ locale, options ]); } /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ function $896ba0a80a8f4d36$export$85fd5fdf27bacc79(options) { // Reuse last options object if it is shallowly equal, which allows the useMemo result to also be reused. options = (0, $iFADg$useDeepMemo)(options, $896ba0a80a8f4d36$var$isEqual); let { locale: locale } = (0, $18f2051aff69b9bf$export$43bb16f9c6d9e3f7)(); return (0, $iFADg$useMemo)(()=>new (0, $iFADg$DateFormatter)(locale, options), [ locale, options ]); } function $896ba0a80a8f4d36$var$isEqual(a, b) { if (a === b) return true; let aKeys = Object.keys(a); let bKeys = Object.keys(b); if (aKeys.length !== bKeys.length) return false; for (let key of aKeys){ if (b[key] !== a[key]) return false; } return true; } /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ function $a916eb452884faea$export$b7a616150fdb9f44(options = {}) { let { locale: locale } = (0, $18f2051aff69b9bf$export$43bb16f9c6d9e3f7)(); return (0, $iFADg$useMemo)(()=>new (0, $iFADg$NumberFormatter)(locale, options), [ locale, options ]); } /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ let $325a3faab7a68acd$var$cache = new Map(); function $325a3faab7a68acd$export$a16aca283550c30d(options) { let { locale: locale } = (0, $18f2051aff69b9bf$export$43bb16f9c6d9e3f7)(); let cacheKey = locale + (options ? Object.entries(options).sort((a, b)=>a[0] < b[0] ? -1 : 1).join() : ""); if ($325a3faab7a68acd$var$cache.has(cacheKey)) return $325a3faab7a68acd$var$cache.get(cacheKey); let formatter = new Intl.Collator(locale, options); $325a3faab7a68acd$var$cache.set(cacheKey, formatter); return formatter; } /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ function $bb77f239b46e8c72$export$3274cf84b703fff(options) { let collator = (0, $325a3faab7a68acd$export$a16aca283550c30d)({ usage: "search", ...options }); // TODO(later): these methods don't currently support the ignorePunctuation option. let startsWith = (0, $iFADg$useCallback)((string, substring)=>{ if (substring.length === 0) return true; // Normalize both strings so we can slice safely // TODO: take into account the ignorePunctuation option as well... string = string.normalize("NFC"); substring = substring.normalize("NFC"); return collator.compare(string.slice(0, substring.length), substring) === 0; }, [ collator ]); let endsWith = (0, $iFADg$useCallback)((string, substring)=>{ if (substring.length === 0) return true; string = string.normalize("NFC"); substring = substring.normalize("NFC"); return collator.compare(string.slice(-substring.length), substring) === 0; }, [ collator ]); let contains = (0, $iFADg$useCallback)((string, substring)=>{ if (substring.length === 0) return true; string = string.normalize("NFC"); substring = substring.normalize("NFC"); let scan = 0; let sliceLen = substring.length; for(; scan + sliceLen <= string.length; scan++){ let slice = string.slice(scan, scan + sliceLen); if (collator.compare(substring, slice) === 0) return true; } return false; }, [ collator ]); return (0, $iFADg$useMemo)(()=>({ startsWith: startsWith, endsWith: endsWith, contains: contains }), [ startsWith, endsWith, contains ]); } export {$18f2051aff69b9bf$export$a54013f0d02a8f82 as I18nProvider, $18f2051aff69b9bf$export$43bb16f9c6d9e3f7 as useLocale, $fca6afa0e843324b$export$f12b703ca79dfbb1 as useLocalizedStringFormatter, $33bf17300c498528$export$a2f47a3d2973640 as useListFormatter, $896ba0a80a8f4d36$export$85fd5fdf27bacc79 as useDateFormatter, $a916eb452884faea$export$b7a616150fdb9f44 as useNumberFormatter, $325a3faab7a68acd$export$a16aca283550c30d as useCollator, $bb77f239b46e8c72$export$3274cf84b703fff as useFilter}; //# sourceMappingURL=real-module.js.map