@brightspace-ui/intl
Advanced tools
Comparing version 3.20.1 to 3.21.0
@@ -8,2 +8,20 @@ import './PluralRules.js'; | ||
const characterMap = new Map([ | ||
['\'', 'apostrophe'], | ||
['&', 'ampersand'], | ||
['*', 'asterisk'], | ||
['\\', 'backslash'], | ||
[':', 'colon'], | ||
[',', 'comma'], | ||
['>', 'greaterThan'], | ||
['<', 'lessThan'], | ||
['#', 'numberSign'], | ||
['%', 'percentSign'], | ||
['|', 'pipe'], | ||
['?', 'questionMark'], | ||
['"', 'quotationMark'] | ||
]); | ||
const commonResources = new Map(); | ||
export let commonResourcesImportCount = 0; | ||
const getDisallowedTagsRegex = allowedTags => { | ||
@@ -72,2 +90,13 @@ const validTerminators = '([>\\s/]|$)'; | ||
localizeCharacter(char) { | ||
if (!characterMap.has(char)) { | ||
throw new Error(`localizeCharacter() does not support character: "${char}"`); | ||
} | ||
const value = this.localize(`intl-common:characters:${characterMap.get(char)}`); | ||
if (value.length === 0) { | ||
throw new Error('localizeCharacter() cannot be used unless loadCommon in localizeConfig is enabled'); | ||
} | ||
return value; | ||
} | ||
localizeHTML(name, replacements = {}) { | ||
@@ -134,2 +163,13 @@ | ||
resourcesLoadedPromises.push(resourcesPromise); | ||
if (config?.loadCommon) { | ||
resourcesLoadedPromises.push(this.getLocalizeResources(possibleLanguages, { | ||
importFunc: async(lang) => { | ||
if (commonResources.has(lang)) return commonResources.get(lang); | ||
const resources = (await import(`../lang/${lang}.js`)).default; | ||
commonResourcesImportCount++; | ||
commonResources.set(lang, resources); | ||
return resources; | ||
} | ||
})); | ||
} | ||
} | ||
@@ -141,2 +181,4 @@ return Promise.all(resourcesLoadedPromises); | ||
if (importFunc === undefined) return; | ||
// in dev, don't request unsupported langpacks | ||
@@ -178,3 +220,5 @@ if (!importFunc.toString().includes('switch') && !useBrowserLangs) { | ||
this.#resourcesPromise = resourcesPromise; | ||
const localizeResources = (await resourcesPromise).flat(Infinity); | ||
const localizeResources = (await resourcesPromise) | ||
.flat(Infinity) | ||
.filter(e => e); | ||
// If the locale changed while resources were being fetched, abort | ||
@@ -185,2 +229,3 @@ if (this.#resourcesPromise !== resourcesPromise) return; | ||
const resolvedLocales = new Set(); | ||
for (const { language, resources } of localizeResources) { | ||
@@ -187,0 +232,0 @@ for (const [name, value] of Object.entries(resources)) { |
{ | ||
"name": "@brightspace-ui/intl", | ||
"version": "3.20.1", | ||
"version": "3.21.0", | ||
"description": "Internationalization APIs for number, date, time and file size formatting and parsing in D2L Brightspace.", | ||
@@ -14,2 +14,3 @@ "main": "lib/number.js", | ||
"files": [ | ||
"/lang", | ||
"/lib", | ||
@@ -16,0 +17,0 @@ "/helpers" |
@@ -8,3 +8,3 @@ # intl | ||
This library consists of APIs to format and parse numbers, dates, times and file sizes for use in D2L Brightspace. | ||
This library consists of APIs to format and parse numbers, dates, times and file sizes for use in D2L Brightspace. It also provides localization for common terms. | ||
@@ -32,4 +32,6 @@ > Looking for the older `d2l-intl` library? It's still here [in the `v2.x` branch](https://github.com/BrightspaceUI/intl/tree/v2.x). | ||
## Number Formatting | ||
## Numbers | ||
### Number Formatting | ||
Integer and decimal numbers can be formatted in the user's locale using `formatNumber`. Percentages can be formatted using `formatPercent`. Use the optional `options` parameter for rounding. | ||
@@ -70,3 +72,3 @@ | ||
## Number Parsing | ||
### Number Parsing | ||
@@ -81,4 +83,6 @@ The `parseNumber` method can be used to parse an integer or decimal number written in the user's locale. | ||
## Date/Time Formatting | ||
## Dates & Times | ||
### Date & Time Formatting | ||
Dates and times can be formatted in the user's locale using `formatDate`, `formatTime`, `formatDateTime`, and `formatRelativeDateTime`. | ||
@@ -190,3 +194,3 @@ | ||
## Date Parsing | ||
### Date Parsing | ||
@@ -204,3 +208,3 @@ To parse a date written in the user's locale, use `parseDate`: | ||
## Time Parsing | ||
### Time Parsing | ||
@@ -217,3 +221,3 @@ To parse a time written in the user's locale, use `parseTime`: | ||
## Date/Time Conversion based on user timezone | ||
### Date/Time Conversion based on user timezone | ||
@@ -382,3 +386,37 @@ To convert an object containing a UTC date to an object containing a local date corresponding to the `data-timezone` attribute: | ||
### Common Resources | ||
Some localization resources are common and shared across D2L applications. To use these resources, set the `loadCommon` option: | ||
```javascript | ||
import { Localize } from '@brightspace-ui/intl/lib/localize.js'; | ||
const localizer = new Localize({ | ||
loadCommon: true | ||
}); | ||
``` | ||
#### localizeCharacter | ||
The localized value of the following characters can be accessed using `localizeCharacter(char)`: | ||
* `'` (apostrophe) | ||
* `&` (ampersand) | ||
* `*` (asterisk) | ||
* `\` (backslash) | ||
* `:` (colon) | ||
* `,` (comma) | ||
* `>` (greater-than sign) | ||
* `<` (less-than sign) | ||
* `#` (number sign) | ||
* `%` (percent sign) | ||
* `|` (pipe) | ||
* `?` (question mark) | ||
* `"` (quotation mark) | ||
```javascript | ||
const value = localizer.localizeCharacter('&'); // -> 'ampersand' in en-US | ||
``` | ||
### `onResourcesChange` | ||
Provide an `onResourcesChange` callback function to perform tasks when the document language is changed and updated resources are available: | ||
@@ -385,0 +423,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114285
30
2579
450