Skip to content

Commit 32b604f

Browse files
committed
feat(localization): Add ability to disable Angulars locale and localization.
1 parent 1034253 commit 32b604f

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

projects/igniteui-angular/src/lib/core/i18n/resources.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,22 @@ export function getCurrentResourceStrings<T>(defaultEN: T, init = true) {
8181
export function changei18n(resourceStrings: IResourceStrings) {
8282
igxRegisterI18n(resourceStrings, getI18nManager().defaultLocale);
8383
}
84+
85+
const angularLocalizationProp = Symbol.for('igx.i18n.angularLocalization');
86+
87+
/** Toggle Angular's localization and formatting in favor of our Intl implementation.
88+
* @enable If should be enabled(true) or disabled(false). True by default.
89+
* @returns If is now enabled or disabled.
90+
*/
91+
export function toggleIgxAngularLocalization(enable?: boolean): boolean {
92+
globalThis[angularLocalizationProp] = enable != null ? enable : !globalThis[angularLocalizationProp];
93+
return globalThis[angularLocalizationProp];
94+
}
95+
96+
/** Get if the Angular's localization and formatting is enabled. It is true by default. */
97+
export function isIgxAngularLocalizationEnabled(): boolean {
98+
if (globalThis[angularLocalizationProp] == null) {
99+
globalThis[angularLocalizationProp] = true;
100+
}
101+
return globalThis[angularLocalizationProp];
102+
}

projects/igniteui-angular/src/lib/core/utils.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { setImmediate } from './setImmediate';
1414
import { isDevMode } from '@angular/core';
1515
import type { IgxTheme } from '../services/theme/theme.token';
1616
import { getDateFormatter, getI18nManager, getNumberFormatter, IResourceChangeEventArgs } from 'igniteui-i18n-core';
17+
import { isIgxAngularLocalizationEnabled } from './i18n/resources';
1718

1819
/** @hidden @internal */
1920
export const ELEMENTS_TOKEN = /*@__PURE__*/new InjectionToken<boolean>('elements environment');
@@ -631,6 +632,8 @@ export function onResourceChangeHandle(destroyObj: Subject<any> | DestroyRef, ca
631632
}
632633
}
633634

635+
//#region Localization
636+
const angularDisabledError = new Error('Angular localization disabled!');
634637
const IntlDateTimeStyleValues = {
635638
full: 'Full',
636639
long: 'Long',
@@ -652,7 +655,11 @@ export function getLocaleDateFormat(locale: string, displayFormat?: string): str
652655
}
653656
let format: string;
654657
try {
655-
format = ngGetLocaleDateFormat(locale, FormatWidth[IntlDateTimeStyleValues[targetKey]]);
658+
if (isIgxAngularLocalizationEnabled()) {
659+
format = ngGetLocaleDateFormat(locale, FormatWidth[IntlDateTimeStyleValues[targetKey]]);
660+
} else {
661+
throw angularDisabledError;
662+
}
656663
} catch {
657664
// No longer throw warnings. Back up is to use Intl now, which should return the format without registering locales.
658665
format = getDateFormatter().getLocaleDateTimeFormat(locale, false, { dateStyle: targetKey });
@@ -675,7 +682,11 @@ export function getLocaleDateTimeFormat(locale: string, displayFormat?: string)
675682
}
676683
let format: string;
677684
try {
678-
format = ngGetLocaleDateTimeFormat(locale, FormatWidth[IntlDateTimeStyleValues[targetKey]]);
685+
if (isIgxAngularLocalizationEnabled()) {
686+
format = ngGetLocaleDateTimeFormat(locale, FormatWidth[IntlDateTimeStyleValues[targetKey]]);
687+
} else {
688+
throw angularDisabledError;
689+
}
679690
} catch {
680691
// No longer throw warnings. Back up is to use Intl now, which should return the format without registering locales.
681692
format = getDateFormatter().getLocaleDateTimeFormat(locale, false, { dateStyle: targetKey, timeStyle: targetKey });
@@ -691,7 +702,11 @@ export function getLocaleDateTimeFormat(locale: string, displayFormat?: string)
691702
export function formatDate(value: Date | string | number | null | undefined, format: string, locale: string, timezone?: string): string {
692703
let formattedDate: string;
693704
try {
694-
formattedDate = ngFormatDate(value, format, locale, timezone);
705+
if (isIgxAngularLocalizationEnabled()) {
706+
formattedDate = ngFormatDate(value, format, locale, timezone);
707+
} else {
708+
throw angularDisabledError;
709+
}
695710
} catch {
696711
if (value === null || value === undefined || value === '') {
697712
return '';
@@ -784,7 +799,11 @@ export function getCurrencyCode(locale: string, overrideCode?: string) {
784799
return overrideCode;
785800
} else {
786801
try {
787-
currencyCode = getLocaleCurrencyCode(locale)
802+
if (isIgxAngularLocalizationEnabled()) {
803+
currencyCode = getLocaleCurrencyCode(locale);
804+
} else {
805+
throw angularDisabledError;
806+
}
788807
} catch {
789808
// If not available the user needs to define it.
790809
}
@@ -798,11 +817,16 @@ export function getCurrencySymbol(currencyCode: string, locale?: string, currenc
798817

799818
export function getLocaleFirstDayOfWeek(locale?: string) {
800819
try {
801-
// Angular returns 0 for Sunday...
802-
return ngGetLocaleFirstDayOfWeek(locale);
820+
if (isIgxAngularLocalizationEnabled()) {
821+
// Angular returns 0 for Sunday...
822+
return ngGetLocaleFirstDayOfWeek(locale);
823+
} else {
824+
throw angularDisabledError;
825+
}
803826
} catch {}
804827
return getDateFormatter().getFirstDayOfWeek(locale);
805828
}
829+
//#endregion
806830

807831
/** Converts pixel values to their rem counterparts for a base value */
808832
export const rem = (value: number | string) => {

0 commit comments

Comments
 (0)