@internationalized/string 3.0.2-nightly.3698 → 3.0.2-nightly.3705
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 +118 -0
- package/package.json +7 -2
package/dist/import.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
* Copyright 2022 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/ class $5b160d28a433310d$export$c17fa47878dc55b6 {
|
|
22
|
+
/** Returns a localized string for the given key and locale. */ getStringForLocale(key, locale) {
|
|
23
|
+
let strings = this.strings[locale];
|
|
24
|
+
if (!strings) {
|
|
25
|
+
strings = $5b160d28a433310d$var$getStringsForLocale(locale, this.strings, this.defaultLocale);
|
|
26
|
+
this.strings[locale] = strings;
|
|
27
|
+
}
|
|
28
|
+
let string = strings[key];
|
|
29
|
+
if (!string) throw new Error(`Could not find intl message ${key} in ${locale} locale`);
|
|
30
|
+
return string;
|
|
31
|
+
}
|
|
32
|
+
constructor(messages, defaultLocale = "en-US"){
|
|
33
|
+
// Clone messages so we don't modify the original object.
|
|
34
|
+
this.strings = {
|
|
35
|
+
...messages
|
|
36
|
+
};
|
|
37
|
+
this.defaultLocale = defaultLocale;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function $5b160d28a433310d$var$getStringsForLocale(locale, strings, defaultLocale = "en-US") {
|
|
41
|
+
// If there is an exact match, use it.
|
|
42
|
+
if (strings[locale]) return strings[locale];
|
|
43
|
+
// Attempt to find the closest match by language.
|
|
44
|
+
// For example, if the locale is fr-CA (French Canadian), but there is only
|
|
45
|
+
// an fr-FR (France) set of strings, use that.
|
|
46
|
+
// This could be replaced with Intl.LocaleMatcher once it is supported.
|
|
47
|
+
// https://github.com/tc39/proposal-intl-localematcher
|
|
48
|
+
let language = $5b160d28a433310d$var$getLanguage(locale);
|
|
49
|
+
if (strings[language]) return strings[language];
|
|
50
|
+
for(let key in strings){
|
|
51
|
+
if (key.startsWith(language + "-")) return strings[key];
|
|
52
|
+
}
|
|
53
|
+
// Nothing close, use english.
|
|
54
|
+
return strings[defaultLocale];
|
|
55
|
+
}
|
|
56
|
+
function $5b160d28a433310d$var$getLanguage(locale) {
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
if (Intl.Locale) // @ts-ignore
|
|
59
|
+
return new Intl.Locale(locale).language;
|
|
60
|
+
return locale.split("-")[0];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
/*
|
|
65
|
+
* Copyright 2022 Adobe. All rights reserved.
|
|
66
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
67
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
68
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
69
|
+
*
|
|
70
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
71
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
72
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
73
|
+
* governing permissions and limitations under the License.
|
|
74
|
+
*/ const $6db58dc88e78b024$var$pluralRulesCache = new Map();
|
|
75
|
+
const $6db58dc88e78b024$var$numberFormatCache = new Map();
|
|
76
|
+
class $6db58dc88e78b024$export$2f817fcdc4b89ae0 {
|
|
77
|
+
/** Formats a localized string for the given key with the provided variables. */ format(key, variables) {
|
|
78
|
+
let message = this.strings.getStringForLocale(key, this.locale);
|
|
79
|
+
return typeof message === "function" ? message(variables, this) : message;
|
|
80
|
+
}
|
|
81
|
+
plural(count, options, type = "cardinal") {
|
|
82
|
+
let opt = options["=" + count];
|
|
83
|
+
if (opt) return typeof opt === "function" ? opt() : opt;
|
|
84
|
+
let key = this.locale + ":" + type;
|
|
85
|
+
let pluralRules = $6db58dc88e78b024$var$pluralRulesCache.get(key);
|
|
86
|
+
if (!pluralRules) {
|
|
87
|
+
pluralRules = new Intl.PluralRules(this.locale, {
|
|
88
|
+
type: type
|
|
89
|
+
});
|
|
90
|
+
$6db58dc88e78b024$var$pluralRulesCache.set(key, pluralRules);
|
|
91
|
+
}
|
|
92
|
+
let selected = pluralRules.select(count);
|
|
93
|
+
opt = options[selected] || options.other;
|
|
94
|
+
return typeof opt === "function" ? opt() : opt;
|
|
95
|
+
}
|
|
96
|
+
number(value) {
|
|
97
|
+
let numberFormat = $6db58dc88e78b024$var$numberFormatCache.get(this.locale);
|
|
98
|
+
if (!numberFormat) {
|
|
99
|
+
numberFormat = new Intl.NumberFormat(this.locale);
|
|
100
|
+
$6db58dc88e78b024$var$numberFormatCache.set(this.locale, numberFormat);
|
|
101
|
+
}
|
|
102
|
+
return numberFormat.format(value);
|
|
103
|
+
}
|
|
104
|
+
select(options, value) {
|
|
105
|
+
let opt = options[value] || options.other;
|
|
106
|
+
return typeof opt === "function" ? opt() : opt;
|
|
107
|
+
}
|
|
108
|
+
constructor(locale, strings){
|
|
109
|
+
this.locale = locale;
|
|
110
|
+
this.strings = strings;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
export {$5b160d28a433310d$export$c17fa47878dc55b6 as LocalizedStringDictionary, $6db58dc88e78b024$export$2f817fcdc4b89ae0 as LocalizedStringFormatter};
|
|
118
|
+
//# sourceMappingURL=module.js.map
|
package/package.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@internationalized/string",
|
|
3
|
-
"version": "3.0.2-nightly.
|
|
3
|
+
"version": "3.0.2-nightly.3705+93b3c951e",
|
|
4
4
|
"description": "Internationalized string formatting and locale negotiation",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
7
7
|
"module": "dist/module.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"types": "./dist/types.d.ts",
|
|
10
|
+
"import": "./dist/import.mjs",
|
|
11
|
+
"require": "./dist/main.js"
|
|
12
|
+
},
|
|
8
13
|
"types": "dist/types.d.ts",
|
|
9
14
|
"source": "src/index.ts",
|
|
10
15
|
"files": [
|
|
@@ -22,5 +27,5 @@
|
|
|
22
27
|
"publishConfig": {
|
|
23
28
|
"access": "public"
|
|
24
29
|
},
|
|
25
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "93b3c951eb784b14183f9988f2d188b34de8f42d"
|
|
26
31
|
}
|