UNPKG

@prezly/theme-kit-core

Version:

Data layer and utility library for developing Prezly themes with JavaScript

35 lines (33 loc) 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLanguageDisplayName = getLanguageDisplayName; var _themeKitIntl = require("@prezly/theme-kit-intl"); /** * @returns the display name of the locale in its native language * * If there's only one culture used in a specific language, * we strip the culture name completely. * * Examples: * - English (Global), Spanish (Spain) * - -> English, Spanish * - English (Global), English (UK), Spanish (Spain) * - -> English (Global), English (UK), Spanish */ function getLanguageDisplayName(subject, context) { var locale = toLocale(subject); var locales = context.map(toLocale); var candidates = locales.filter(candidate => _themeKitIntl.Locale.isSameLang(candidate.code, locale.code)); if (candidates.length === 1) { return locale.native_name.replace(/\s*\(.*\)/, ''); } return locale.native_name; } function toLocale(value) { if ('native_name' in value && 'code' in value) { return value; } return value.locale; }