@carbon/ibm-products-web-components
Version:
Carbon for IBM Products Web Components
36 lines (33 loc) • 1.1 kB
JavaScript
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Copyright IBM Corp. 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Ensures the requested `locale` is valid, else returns the default locale.
*
* Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#locales
*/
const getSupportedLocale = (locale, defaultLocale = 'en-US') => {
let supportedLocale;
try {
// This line will throw an error if `locale` is not supported.
Intl.NumberFormat.supportedLocalesOf(locale);
// If no error is thrown, return `locale`,
supportedLocale = locale;
}
catch (error) {
// else return `defaultLocale`.
supportedLocale = defaultLocale;
}
return supportedLocale;
};
export { getSupportedLocale };
//# sourceMappingURL=getSupportedLocale.js.map