@carbon/ibm-products
Version:
Carbon for IBM Products
32 lines (27 loc) • 964 B
JavaScript
/**
* Copyright IBM Corp. 2020, 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 = function (locale) {
let defaultLocale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '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;
};
exports.getSupportedLocale = getSupportedLocale;