igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
121 lines • 4.68 kB
JavaScript
import { getCurrentI18n, getDateFormatter, getDisplayNamesFormatter, getI18nManager, } from 'igniteui-i18n-core';
import { calendarResourcesMap, convertToCoreResource, dateRangePickerResourcesMap, } from './utils.js';
class I18nController {
set locale(value) {
if (this._locale !== value) {
this._locale = value;
this._defaultResourceStrings = this._getCurrentResourceStrings();
this._host.requestUpdate();
}
}
get locale() {
return this._locale ?? getCurrentI18n();
}
set resourceStrings(value) {
if (this._resourceStrings !== value) {
this._resourceStrings = value;
this._host.requestUpdate();
}
}
get resourceStrings() {
return this._resourceStrings ?? this._defaultResourceStrings;
}
constructor(host, config) {
this._host = host;
this._defaultEN = config.defaultEN;
this._resourceChangeCallback = config.onResourceChange;
this._defaultResourceStrings = this._getCurrentResourceStrings();
this._registerResources(this._defaultEN);
this._host.addController(this);
}
hostConnected() {
getI18nManager().addEventListener('onResourceChange', this);
}
hostDisconnected() {
getI18nManager().removeEventListener('onResourceChange', this);
}
handleEvent(event) {
this._defaultResourceStrings = this._getCurrentResourceStrings();
this._resourceChangeCallback?.call(this._host, event);
this._host.requestUpdate();
}
_registerResources(resource) {
const convertedResource = convertToCoreResource(resource);
const manager = getI18nManager();
manager.registerI18n(convertedResource, manager.defaultLocale);
}
_getResourceMapForComponent() {
const keys = Object.keys(this._defaultEN);
if (keys.includes('last7Days')) {
return dateRangePickerResourcesMap;
}
if (keys.includes('selectMonth')) {
return calendarResourcesMap;
}
return undefined;
}
_getCurrentResourceStrings() {
const coreResourceStrings = getI18nManager().getCurrentResourceStrings(this.locale);
const resourceMap = this._getResourceMapForComponent();
const normalizedResourceStrings = {};
const defaultComponentKeys = Object.keys(this._defaultEN);
for (const igcKey of defaultComponentKeys) {
const coreKey = resourceMap?.get(igcKey);
let resolvedValue = this._defaultEN[igcKey];
if (coreKey) {
if (coreKey.includes('getWeekLabel')) {
resolvedValue = getDisplayNamesFormatter().getWeekLabel(this.locale, {
style: 'short',
});
}
else if (coreKey in coreResourceStrings) {
resolvedValue = coreResourceStrings[coreKey];
}
}
else if (igcKey in coreResourceStrings) {
resolvedValue = coreResourceStrings[igcKey];
}
normalizedResourceStrings[igcKey] = resolvedValue;
}
return normalizedResourceStrings;
}
}
const DATE_TIME_STYLES = new Set(['short', 'long', 'medium', 'full']);
function extractStyle(format, suffix) {
return format.toLowerCase().split(suffix)[0];
}
export function getDefaultDateTimeFormat(locale) {
return getDateFormatter().getLocaleDateTimeFormat(locale, true);
}
export function getDateTimeFormat(format, suffix = 'Date') {
return format && DATE_TIME_STYLES.has(format) ? `${format}${suffix}` : format;
}
export function formatDisplayDate(value, locale, displayFormat) {
if (!displayFormat) {
return getDateFormatter().formatDateTime(value, locale, {});
}
if (DATE_TIME_STYLES.has(displayFormat)) {
const style = displayFormat;
return getDateFormatter().formatDateTime(value, locale, {
dateStyle: style,
timeStyle: style,
});
}
if (displayFormat.endsWith('Date')) {
return getDateFormatter().formatDateTime(value, locale, {
dateStyle: extractStyle(displayFormat, 'date'),
});
}
if (displayFormat.endsWith('Time')) {
return getDateFormatter().formatDateTime(value, locale, {
timeStyle: extractStyle(displayFormat, 'time'),
});
}
return getDateFormatter().formatDateCustomFormat(value, displayFormat, {
locale,
});
}
export function addI18nController(host, config) {
return new I18nController(host, config);
}
//# sourceMappingURL=i18n-controller.js.map