@carbon/ibm-products
Version:
Carbon for IBM Products
32 lines (30 loc) • 934 B
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/global/js/utils/getSupportedLocale.ts
/**
* 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 {
Intl.NumberFormat.supportedLocalesOf(locale);
supportedLocale = locale;
} catch (error) {
supportedLocale = defaultLocale;
}
return supportedLocale;
};
//#endregion
exports.getSupportedLocale = getSupportedLocale;