@prezly/theme-kit-core
Version:
Data layer and utility library for developing Prezly themes with JavaScript
33 lines • 1.5 kB
JavaScript
import { Locale } from '@prezly/theme-kit-intl';
import { isNumberCode } from "./isNumberCode.mjs";
/**
* Match the requested locale slug against the enabled locales.
* The enabled locales have to be provided in the order of importance:
* default first, then locales with public stories, then the rest.
*
* The logic is reversed from `getShortestLocaleSlug`, so that it "unwraps" the possible variants.
* @see {getShortestLocaleSlug()}
*
* The order of checks is:
* - exact locale slug match
* - match by language code
* - match by region code
*/
export function matchLocaleSlug(slug, locales) {
var _ref, _ref2, _matchByExactLocaleSl;
return (_ref = (_ref2 = (_matchByExactLocaleSl = matchByExactLocaleSlug(slug, locales)) !== null && _matchByExactLocaleSl !== void 0 ? _matchByExactLocaleSl : matchByLangSlug(slug, locales)) !== null && _ref2 !== void 0 ? _ref2 : matchByRegionSlug(slug, locales)) !== null && _ref !== void 0 ? _ref : undefined;
}
function matchByExactLocaleSlug(slug, locales) {
if (!Locale.isValid(slug)) return undefined;
var locale = Locale.from(slug);
return locales.find(localeCode => localeCode === locale.code);
}
function matchByLangSlug(slug, locales) {
return locales.find(localeCode => Locale.isLanguageCode(localeCode, slug.toLowerCase()));
}
function matchByRegionSlug(slug, locales) {
if (!slug || isNumberCode(slug)) {
return undefined;
}
return locales.find(localeCode => Locale.isRegionCode(localeCode, slug.toUpperCase()));
}