Skip to content

Commit a12394d

Browse files
committed
refactor(localization): Override FormAssociated localization controller when extended by localized component.
1 parent 461680e commit a12394d

File tree

6 files changed

+74
-43
lines changed

6 files changed

+74
-43
lines changed

src/components/combo/combo.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
ComboResourceStringsEN,
33
type IComboResourceStrings,
4+
type IValidationResourceStrings,
5+
ValidationResourceStringsEN,
46
} from 'igniteui-i18n-core';
57
import { html, LitElement, nothing, type TemplateResult } from 'lit';
68
import {
@@ -149,12 +151,11 @@ export default class IgcComboComponent<
149151
},
150152
});
151153

152-
private readonly _i18nController = addI18nController<IComboResourceStrings>(
153-
this,
154-
{
155-
defaultEN: ComboResourceStringsEN,
156-
}
157-
);
154+
protected override readonly __i18nController = addI18nController<
155+
IComboResourceStrings & IValidationResourceStrings
156+
>(this, {
157+
defaultEN: { ...ComboResourceStringsEN, ...ValidationResourceStringsEN },
158+
});
158159

159160
protected override readonly _formValue = createFormValueState<
160161
ComboValue<T>[]
@@ -275,11 +276,11 @@ export default class IgcComboComponent<
275276
*/
276277
@property()
277278
public set locale(value: string) {
278-
this._i18nController.locale = value;
279+
this.__i18nController.locale = value;
279280
}
280281

281282
public get locale() {
282-
return this._i18nController.locale;
283+
return this.__i18nController.locale;
283284
}
284285

285286
/**
@@ -326,12 +327,15 @@ export default class IgcComboComponent<
326327
* The resource strings for localization.
327328
*/
328329
@property({ attribute: false })
329-
public set resourceStrings(value: IComboResourceStrings) {
330-
this._i18nController.resourceStrings = value;
330+
public set resourceStrings(
331+
value: IComboResourceStrings & IValidationResourceStrings
332+
) {
333+
this.__i18nController.resourceStrings = value;
331334
}
332335

333-
public get resourceStrings(): IComboResourceStrings {
334-
return this._i18nController.resourceStrings;
336+
public get resourceStrings(): IComboResourceStrings &
337+
IValidationResourceStrings {
338+
return this.__i18nController.resourceStrings;
335339
}
336340

337341
/**

src/components/common/i18n/i18n-controller.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,32 +166,31 @@ class I18nController<T extends object> implements ReactiveController {
166166
const coreResourceStrings = getI18nManager().getCurrentResourceStrings(
167167
this.locale
168168
);
169-
const resourceMap = this._getResourceMapForComponent();
170-
171-
if (!resourceMap) {
172-
return coreResourceStrings as T;
173-
}
174169

170+
const resourceMap = this._getResourceMapForComponent();
175171
const normalizedResourceStrings: T = {} as T;
176172
const defaultComponentKeys = Object.keys(this._defaultEN) as (keyof T)[];
177173

178174
for (const igcKey of defaultComponentKeys) {
179-
const coreKey = resourceMap.get(igcKey as string);
175+
const coreKey = resourceMap?.get(igcKey as string);
180176
let resolvedValue: T[keyof T] = this._defaultEN[igcKey];
181177

182178
if (coreKey) {
183179
if (coreKey.includes('getWeekLabel')) {
184180
resolvedValue = getDisplayNamesFormatter().getWeekLabel(this.locale, {
185181
style: 'short',
186182
}) as T[keyof T];
187-
} else {
188-
resolvedValue =
189-
coreKey in coreResourceStrings
190-
? (coreResourceStrings[
191-
coreKey as keyof IResourceStrings
192-
] as T[keyof T])
193-
: this._defaultEN[igcKey];
183+
} else if (coreKey in coreResourceStrings) {
184+
resolvedValue = coreResourceStrings[
185+
coreKey as keyof IResourceStrings
186+
] as T[keyof T];
194187
}
188+
} else if (igcKey in coreResourceStrings) {
189+
// For a mix of old and core resources.
190+
// Only for internal default resources. Users shouldn't mix them.
191+
resolvedValue = coreResourceStrings[
192+
igcKey as keyof IResourceStrings
193+
] as T[keyof T];
195194
}
196195

197196
normalizedResourceStrings[igcKey] = resolvedValue;

src/components/common/mixins/forms/associated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function BaseFormAssociated<T extends Constructor<LitElement>>(base: T) {
4141
//#region Internal state and properties
4242

4343
private readonly __internals = addInternalsController(this);
44-
private readonly __i18nController =
44+
protected readonly __i18nController =
4545
addI18nController<IValidationResourceStrings>(this, {
4646
defaultEN: ValidationResourceStringsEN,
4747
});

src/components/common/mixins/forms/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { IValidationResourceStrings } from 'igniteui-i18n-core';
12
import type { LitElement } from 'lit';
23
import type { ElementInternalsController } from '../../controllers/internals.js';
4+
import type { I18nController } from '../../i18n/i18n-controller.js';
35
import type { Validator } from '../../validators.js';
46

57
export type FormRestoreMode = 'autocomplete' | 'restore';
@@ -13,6 +15,7 @@ declare class BaseFormAssociatedElement {
1315
//#region Properties
1416

1517
private readonly __internals: ElementInternalsController;
18+
protected readonly __i18nController: I18nController<IValidationResourceStrings>;
1619
protected readonly _formValue: unknown;
1720

1821
protected _pristine: boolean;

src/components/date-picker/date-picker.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
type IValidationResourceStrings,
3+
ValidationResourceStringsEN,
4+
} from 'igniteui-i18n-core';
15
import { html, nothing, type TemplateResult } from 'lit';
26
import { property, query } from 'lit/decorators.js';
37
import { ifDefined } from 'lit/directives/if-defined.js';
@@ -199,10 +203,19 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
199203
private readonly _inputId = `date-picker-${nextId++}`;
200204
private readonly _themes = addThemingController(this, all);
201205
private readonly _slots = addSlotController(this, { slots: Slots });
202-
private readonly _i18nController =
203-
addI18nController<IgcCalendarResourceStrings>(this, {
204-
defaultEN: IgcCalendarResourceStringEN,
205-
});
206+
207+
/**
208+
* For now we use the core validation strings internally only, to avoid mixing with old resources by users.
209+
* To Do: Update resourceStrings type when the IgcCalendarResourceStrings is changed to ICalendarResourceStrings
210+
*/
211+
protected override readonly __i18nController = addI18nController<
212+
IgcCalendarResourceStrings & IValidationResourceStrings
213+
>(this, {
214+
defaultEN: {
215+
...IgcCalendarResourceStringEN,
216+
...ValidationResourceStringsEN,
217+
},
218+
});
206219

207220
private _oldValue: Date | null = null;
208221
private _activeDate: Date | null = null;
@@ -455,23 +468,23 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
455468
*/
456469
@property()
457470
public set locale(value: string) {
458-
this._i18nController.locale = value;
471+
this.__i18nController.locale = value;
459472
}
460473

461474
public get locale() {
462-
return this._i18nController.locale;
475+
return this.__i18nController.locale;
463476
}
464477

465478
/**
466479
* The resource strings for localization.
467480
*/
468481
@property({ attribute: false })
469482
public set resourceStrings(value: IgcCalendarResourceStrings) {
470-
this._i18nController.resourceStrings = value;
483+
this.__i18nController.resourceStrings = value;
471484
}
472485

473486
public get resourceStrings(): IgcCalendarResourceStrings {
474-
return this._i18nController.resourceStrings;
487+
return this.__i18nController.resourceStrings;
475488
}
476489

477490
/** Sets the start day of the week for the calendar. */

src/components/date-range-picker/date-range-picker.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { getDateFormatter } from 'igniteui-i18n-core';
1+
import {
2+
getDateFormatter,
3+
type IValidationResourceStrings,
4+
ValidationResourceStringsEN,
5+
} from 'igniteui-i18n-core';
26
import { html, nothing, type TemplateResult } from 'lit';
37
import {
48
property,
@@ -229,10 +233,18 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
229233
}
230234
);
231235

232-
private readonly _i18nController =
233-
addI18nController<IgcDateRangePickerResourceStrings>(this, {
234-
defaultEN: IgcDateRangePickerResourceStringsEN,
235-
});
236+
/**
237+
* For now we use the core validation strings internally only, to avoid mixing with old resources by users.
238+
* To Do: Update resourceStrings type when the IgcDateRangePickerResourceStrings is changed to IDateRangePickerResourceStrings
239+
*/
240+
protected override readonly __i18nController = addI18nController<
241+
IgcDateRangePickerResourceStrings & IValidationResourceStrings
242+
>(this, {
243+
defaultEN: {
244+
...IgcDateRangePickerResourceStringsEN,
245+
...ValidationResourceStringsEN,
246+
},
247+
});
236248

237249
private _activeDate: Date | null = null;
238250
private _min: Date | null = null;
@@ -346,21 +358,21 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
346358
*/
347359
@property()
348360
public set locale(value: string) {
349-
this._i18nController.locale = value;
361+
this.__i18nController.locale = value;
350362
}
351363

352364
public get locale() {
353-
return this._i18nController.locale;
365+
return this.__i18nController.locale;
354366
}
355367

356368
/** The resource strings of the date range picker. */
357369
@property({ attribute: false })
358370
public set resourceStrings(value: IgcDateRangePickerResourceStrings) {
359-
this._i18nController.resourceStrings = value;
371+
this.__i18nController.resourceStrings = value;
360372
}
361373

362374
public get resourceStrings(): IgcDateRangePickerResourceStrings {
363-
return this._i18nController.resourceStrings;
375+
return this.__i18nController.resourceStrings;
364376
}
365377

366378
// #endregion

0 commit comments

Comments
 (0)