@prezly/theme-kit-core
Version:
Data layer and utility library for developing Prezly themes with JavaScript
30 lines (28 loc) • 890 B
JavaScript
import { Locale } from '@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
*/
export function getLanguageDisplayName(subject, context) {
var locale = toLocale(subject);
var locales = context.map(toLocale);
var candidates = locales.filter(candidate => 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;
}