Skip to content

Commit 4d5e65b

Browse files
authored
fix(locales): fix some Locales not showing up when not using Translate (#392)
* fix(locales): fix some Locales not showing up when not using Translate
1 parent 184e47a commit 4d5e65b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+144
-96
lines changed

src/app/modules/angular-slickgrid/components/__tests__/angular-slickgrid-constructor.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1273,9 +1273,9 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
12731273
leftContainerClass: 'col-xs-12 col-sm-5',
12741274
metricSeparator: '|',
12751275
metricTexts: {
1276-
items: 'ITEMS',
1276+
items: 'items',
12771277
itemsKey: 'ITEMS',
1278-
of: 'OF',
1278+
of: 'of',
12791279
ofKey: 'OF',
12801280
},
12811281
rightContainerClass: 'col-xs-6 col-sm-7',

src/app/modules/angular-slickgrid/components/__tests__/slick-pagination-without-translate.component.spec.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { By } from '@angular/platform-browser';
33
import { Subject } from 'rxjs';
44

55
import { SlickPaginationComponent } from '../slick-pagination.component';
6-
import { Locale, ServicePagination } from '../../models';
6+
import { GridOption, ServicePagination } from '../../models';
77
import { PaginationService } from '../../services';
88

99
const paginationServiceStub = {
@@ -56,13 +56,7 @@ describe('without ngx-translate', () => {
5656
pageSizes: [5, 10, 15, 20],
5757
totalItems: 100,
5858
};
59-
component.enableTranslate = false;
60-
component.locales = {
61-
TEXT_ITEMS_PER_PAGE: 'items per page',
62-
TEXT_ITEMS: 'items',
63-
TEXT_OF: 'of',
64-
TEXT_PAGE: 'page'
65-
} as Locale;
59+
component.gridOptions = { enableTranslate: false } as GridOption;
6660

6761
paginationServiceStub.onPaginationChanged.next(mockServicePagination);
6862
fixture.detectChanges();
@@ -81,16 +75,17 @@ describe('without ngx-translate', () => {
8175

8276
it('should throw an error when "enableTranslate" is set and Translate Service is not provided', (done) => {
8377
try {
84-
component.enableTranslate = true;
78+
component.gridOptions.enableTranslate = true;
8579
component.constructor();
80+
component.ngOnInit();
8681
} catch (e) {
8782
expect(e.toString()).toContain('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
8883
done();
8984
}
9085
});
9186

9287
it('should have defined locale and expect new text in the UI', (done) => {
93-
component.enableTranslate = false;
88+
component.gridOptions.enableTranslate = false;
9489
fixture.detectChanges();
9590

9691
setTimeout(() => {

src/app/modules/angular-slickgrid/components/__tests__/slick-pagination.component.spec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TranslateModule, TranslateService } from '@ngx-translate/core';
44
import { Subject } from 'rxjs';
55

66
import { SlickPaginationComponent } from '../slick-pagination.component';
7-
import { ServicePagination } from '../../models';
7+
import { GridOption, ServicePagination } from '../../models';
88
import { PaginationService } from '../../services';
99

1010
const paginationServiceStub = {
@@ -87,7 +87,8 @@ describe('App Component', () => {
8787
pageSizes: [5, 10, 15, 20],
8888
totalItems: 100,
8989
};
90-
component.enableTranslate = true;
90+
component.gridOptions = { enableTranslate: true } as GridOption;
91+
component.ngOnInit();
9192
paginationServiceStub.onPaginationChanged.next(mockServicePagination);
9293
fixture.detectChanges();
9394
});
@@ -110,12 +111,14 @@ describe('App Component', () => {
110111
});
111112

112113
it('should create a the Slick-Pagination component in the DOM', () => {
114+
component.gridOptions = { enableTranslate: true } as GridOption;
113115
fixture.detectChanges();
114116

115117
const elm = document.querySelector('.slick-pagination');
116118
const pageInfoFromTo = fixture.debugElement.query(By.css('.page-info-from-to')).nativeElement;
117119
const pageInfoTotalItems = fixture.debugElement.query(By.css('.page-info-total-items')).nativeElement;
118120

121+
expect(translate.currentLang).toBe('fr');
119122
expect(elm.innerHTML).toContain('slick-pagination-nav');
120123
expect(pageInfoFromTo.innerHTML).toBe('<span data-test="item-from">5</span>-<span data-test="item-to">10</span> de ');
121124
expect(pageInfoTotalItems.innerHTML).toBe('<span data-test="total-items">100</span> éléments ');

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
</div>
44

55
<!-- Pagination section under the grid -->
6-
<slick-pagination id="slickPagingContainer-{{gridId}}" *ngIf="showPagination"
7-
[enableTranslate]="gridOptions.enableTranslate" [locales]="locales">
6+
<slick-pagination id="slickPagingContainer-{{gridId}}" *ngIf="showPagination" [gridOptions]="gridOptions">
87
</slick-pagination>
98

109
<!-- Custom Footer section under the grid -->

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
139139
showPagination = false;
140140
totalItems = 0;
141141
paginationData: {
142-
enableTranslate: boolean;
143-
locales: Locale;
142+
gridOptions: GridOption;
144143
};
145144
subscriptions: Subscription[] = [];
146145

@@ -664,8 +663,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
664663
private initializePaginationService(paginationOptions: Pagination) {
665664
if (this.gridOptions) {
666665
this.paginationData = {
667-
enableTranslate: this.gridOptions.enableTranslate,
668-
locales: this.locales,
666+
gridOptions: this.gridOptions,
669667
};
670668
this.paginationService.totalItems = this.totalItems;
671669
this.paginationService.init(this.grid, this.dataView, paginationOptions, this.backendServiceApi);
@@ -987,7 +985,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
987985

988986
/** Translate all Custom Footer Texts (footer with metrics) */
989987
private translateCustomFooterTexts() {
990-
if (this.translate && this.translate.instant) {
988+
if (this.translate && this.translate.instant && this.translate.currentLang) {
991989
const customFooterOptions = this.gridOptions && this.gridOptions.customFooterOptions || {};
992990
customFooterOptions.metricTexts = customFooterOptions.metricTexts || {};
993991
for (const propName of Object.keys(customFooterOptions.metricTexts)) {

src/app/modules/angular-slickgrid/components/slick-pagination.component.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Component, EventEmitter, Input, OnDestroy, Optional, Output, OnInit } from '@angular/core';
1+
import { Component, Input, OnDestroy, OnInit, Optional } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33
import { Subscription } from 'rxjs';
44

5-
import { Locale, Pagination } from './../models/index';
5+
import { Constants } from '../constants';
6+
import { GridOption, Locale } from './../models/index';
67
import { PaginationService } from '../services/pagination.service';
78
import { unsubscribeAllObservables } from '../services/utilities';
89

@@ -11,29 +12,20 @@ import { unsubscribeAllObservables } from '../services/utilities';
1112
templateUrl: './slick-pagination.component.html'
1213
})
1314
export class SlickPaginationComponent implements OnDestroy, OnInit {
14-
private subscriptions: Subscription[] = [];
15+
@Input() gridOptions: GridOption;
1516

16-
@Input() enableTranslate: boolean;
17-
@Input() locales: Locale;
17+
private subscriptions: Subscription[] = [];
18+
private _enableTranslate = false;
19+
private _locales: Locale;
1820

1921
// text translations (handled by ngx-translate or by custom locale)
20-
textItemsPerPage: string;
21-
textItems: string;
22-
textOf: string;
23-
textPage: string;
22+
textItemsPerPage = 'items per page';
23+
textItems = 'items';
24+
textOf = 'of';
25+
textPage = 'Page';
2426

2527
/** Constructor */
26-
constructor(private paginationService: PaginationService, @Optional() private translate: TranslateService) {
27-
if (this.enableTranslate && !this.translate) {
28-
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
29-
}
30-
31-
// translate all the text using ngx-translate or custom locales
32-
this.translateAllUiTexts(this.locales);
33-
if (translate && translate.onLangChange) {
34-
this.subscriptions.push(this.translate.onLangChange.subscribe(() => this.translateAllUiTexts(this.locales)));
35-
}
36-
}
28+
constructor(private paginationService: PaginationService, @Optional() private translate: TranslateService) { }
3729

3830
get availablePageSizes(): number[] {
3931
return this.paginationService.availablePageSizes;
@@ -71,7 +63,20 @@ export class SlickPaginationComponent implements OnDestroy, OnInit {
7163
}
7264

7365
ngOnInit() {
74-
this.translateAllUiTexts(this.locales);
66+
const gridOptions: GridOption = this.gridOptions || {};
67+
this._enableTranslate = gridOptions && gridOptions.enableTranslate || false;
68+
this._locales = gridOptions && gridOptions.locales || Constants.locales;
69+
70+
if (this._enableTranslate && !this.translate) {
71+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
72+
}
73+
74+
this.translateAllUiTexts(this._locales);
75+
76+
// translate all the text using ngx-translate or custom locales
77+
if (this._enableTranslate && this.translate && this.translate.onLangChange) {
78+
this.subscriptions.push(this.translate.onLangChange.subscribe(() => this.translateAllUiTexts(this._locales)));
79+
}
7580
}
7681

7782
changeToFirstPage(event: any) {
@@ -119,7 +124,7 @@ export class SlickPaginationComponent implements OnDestroy, OnInit {
119124

120125
/** Translate all the texts shown in the UI, use ngx-translate service when available or custom locales when service is null */
121126
private translateAllUiTexts(locales: Locale) {
122-
if (this.translate && this.translate.instant) {
127+
if (this._enableTranslate && this.translate && this.translate.instant && this.translate.currentLang) {
123128
this.textItemsPerPage = this.translate.instant('ITEMS_PER_PAGE');
124129
this.textItems = this.translate.instant('ITEMS');
125130
this.textOf = this.translate.instant('OF');

src/app/modules/angular-slickgrid/editors/__tests__/dateEditor.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('DateEditor', () => {
6363
SAVE: 'Sauvegarder',
6464
});
6565
translate.setDefaultLang('en');
66+
translate.use('en');
6667

6768
mockColumn = { id: 'startDate', field: 'startDate', editable: true, editor: { model: Editors.date }, internalColumnEditor: {} } as Column;
6869

src/app/modules/angular-slickgrid/editors/__tests__/longTextEditor.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('LongTextEditor', () => {
6262
SAVE: 'Sauvegarder',
6363
});
6464
translate.setDefaultLang('fr');
65+
translate.use('fr');
6566

6667
mockColumn = { id: 'title', field: 'title', editable: true, editor: { model: Editors.longText }, internalColumnEditor: {} } as Column;
6768

src/app/modules/angular-slickgrid/editors/__tests__/multipleSelectEditor.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('SelectEditor', () => {
6464
SAVE: 'Sauvegarder',
6565
});
6666
translate.setDefaultLang('fr');
67+
translate.use('fr');
6768

6869
mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.multipleSelect }, internalColumnEditor: {} } as Column;
6970

src/app/modules/angular-slickgrid/editors/__tests__/selectEditor.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('SelectEditor', () => {
6464
SAVE: 'Sauvegarder',
6565
});
6666
translate.setDefaultLang('fr');
67+
translate.use('fr');
6768

6869
mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.multipleSelect }, internalColumnEditor: {} } as Column;
6970

src/app/modules/angular-slickgrid/editors/__tests__/singleSelectEditor.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('SelectEditor', () => {
6464
SAVE: 'Sauvegarder',
6565
});
6666
translate.setDefaultLang('fr');
67+
translate.use('fr');
6768

6869
mockColumn = { id: 'gender', field: 'gender', editable: true, editor: { model: Editors.multipleSelect }, internalColumnEditor: {} } as Column;
6970

src/app/modules/angular-slickgrid/editors/dateEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class DateEditor implements Editor {
4747
throw new Error('[Angular-SlickGrid] Something is wrong with this grid, an Editor must always have valid arguments.');
4848
}
4949
this.grid = args.grid;
50-
this.gridOptions = args.grid && args.grid.getOptions() as GridOption;
50+
this.gridOptions = (args.grid && args.grid.getOptions() || {}) as GridOption;
5151
const options = this.gridOptions || this.args.column.params || {};
5252
if (options && options.i18n instanceof TranslateService) {
5353
this._translate = options.i18n;
@@ -93,7 +93,7 @@ export class DateEditor implements Editor {
9393
this.defaultDate = (this.args.item) ? this.args.item[this.columnDef.field] : null;
9494
const inputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.type || FieldType.dateIso);
9595
const outputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.outputType || FieldType.dateUtc);
96-
let currentLocale = this._translate && this._translate.currentLang || 'en';
96+
let currentLocale = this._translate && this._translate.currentLang || this.gridOptions.locale || 'en';
9797
if (currentLocale && currentLocale.length > 2) {
9898
currentLocale = currentLocale.substring(0, 2);
9999
}

src/app/modules/angular-slickgrid/editors/longTextEditor.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ export class LongTextEditor implements Editor {
7878
}
7979

8080
init(): void {
81+
let cancelText = '';
82+
let saveText = '';
83+
if (this._translate && this._translate.instant && this._translate.currentLang) {
84+
cancelText = this._translate.instant('CANCEL');
85+
saveText = this._translate.instant('SAVE');
86+
} else {
87+
cancelText = this._locales && this._locales.TEXT_CANCEL;
88+
saveText = this._locales && this._locales.TEXT_SAVE;
89+
}
90+
8191
const columnId = this.columnDef && this.columnDef.id;
8292
const placeholder = this.columnEditor && this.columnEditor.placeholder || '';
8393
const title = this.columnEditor && this.columnEditor.title || '';
84-
const cancelText = this._translate && this._translate.instant && this._translate.instant('CANCEL') || this._locales && this._locales.TEXT_CANCEL;
85-
const saveText = this._translate && this._translate.instant && this._translate.instant('SAVE') || this._locales && this._locales.TEXT_SAVE;
8694
const $container = $('body');
8795

8896
this._$wrapper = $(`<div class="slick-large-editor-text editor-${columnId}" />`).appendTo($container);

src/app/modules/angular-slickgrid/editors/selectEditor.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
EditorValidatorOutput,
1616
FieldType,
1717
GridOption,
18+
Locale,
1819
MultipleSelectOption,
1920
SelectOption,
2021
} from './../models/index';
@@ -64,8 +65,11 @@ export class SelectEditor implements Editor {
6465
/** Do we translate the label? */
6566
enableTranslateLabel: boolean;
6667

68+
/** Locales */
69+
protected _locales: Locale;
70+
6771
/** Observable Subscriptions */
68-
_subscriptions: Subscription[] = [];
72+
protected _subscriptions: Subscription[] = [];
6973

7074
// flag to signal that the editor is destroying itself, helps prevent
7175
// commit changes from being called twice and erroring
@@ -85,12 +89,15 @@ export class SelectEditor implements Editor {
8589
throw new Error('[Angular-SlickGrid] Something is wrong with this grid, an Editor must always have valid arguments.');
8690
}
8791
this.grid = args.grid;
88-
this.gridOptions = this.grid.getOptions() as GridOption;
92+
this.gridOptions = (this.grid.getOptions() || {}) as GridOption;
8993
const options = this.gridOptions || this.args.column.params || {};
9094
if (options && options.i18n instanceof TranslateService) {
9195
this._translate = options.i18n;
9296
}
9397

98+
// get locales provided by user in main file or else use default English locales via the Constants
99+
this._locales = this.gridOptions.locales || Constants.locales;
100+
94101
// provide the name attribute to the DOM element which will be needed to auto-adjust drop position (dropup / dropdown)
95102
const fieldId = this.columnDef && this.columnDef.id;
96103
this.elementName = `editor-${fieldId}`;
@@ -118,10 +125,15 @@ export class SelectEditor implements Editor {
118125
libOptions.okButton = true;
119126
libOptions.selectAllDelimiter = ['', ''];
120127

121-
if (this._translate) {
128+
if (this._translate && this._translate.instant && this._translate.currentLang) {
122129
libOptions.countSelected = this._translate.instant('X_OF_Y_SELECTED');
123130
libOptions.allSelected = this._translate.instant('ALL_SELECTED');
124131
libOptions.selectAllText = this._translate.instant('SELECT_ALL');
132+
} else {
133+
libOptions.countSelected = this._locales && this._locales.TEXT_X_OF_Y_SELECTED;
134+
libOptions.allSelected = this._locales && this._locales.TEXT_ALL_SELECTED;
135+
libOptions.selectAllText = this._locales && this._locales.TEXT_SELECT_ALL;
136+
libOptions.okButtonText = this._locales && this._locales.TEXT_OK;
125137
}
126138
}
127139

src/app/modules/angular-slickgrid/extensions/cellMenuExtension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ export class CellMenuExtension implements Extension {
150150

151151
// translate their titles only if they have a titleKey defined
152152
if (columnDef.cellMenu.commandTitleKey) {
153-
columnDef.cellMenu.commandTitle = this.translate && this.translate.instant && this.translate.instant(columnDef.cellMenu.commandTitleKey) || this._locales && this._locales.TEXT_COMMANDS || columnDef.cellMenu.commandTitle;
153+
columnDef.cellMenu.commandTitle = this.translate && this.translate.currentLang && this.translate.instant && this.translate.instant(columnDef.cellMenu.commandTitleKey) || this._locales && this._locales.TEXT_COMMANDS || columnDef.cellMenu.commandTitle;
154154
}
155155
if (columnDef.cellMenu.optionTitleKey) {
156-
columnDef.cellMenu.optionTitle = this.translate && this.translate.instant && this.translate.instant(columnDef.cellMenu.optionTitleKey) || columnDef.cellMenu.optionTitle;
156+
columnDef.cellMenu.optionTitle = this.translate && this.translate.currentLang && this.translate.instant && this.translate.instant(columnDef.cellMenu.optionTitleKey) || columnDef.cellMenu.optionTitle;
157157
}
158158

159159
// translate both command/option items (whichever is provided)

src/app/modules/angular-slickgrid/extensions/contextMenuExtension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ export class ContextMenuExtension implements Extension {
137137
const menuOptions: Partial<ContextMenu> = {};
138138

139139
if (contextMenu.commandTitleKey) {
140-
contextMenu.commandTitle = this.translate && this.translate.instant && this.translate.instant(contextMenu.commandTitleKey) || contextMenu.commandTitle;
140+
contextMenu.commandTitle = this.translate && this.translate.currentLang && this.translate.instant && this.translate.instant(contextMenu.commandTitleKey) || contextMenu.commandTitle;
141141
menuOptions.commandTitle = contextMenu.commandTitle;
142142
}
143143
if (contextMenu.optionTitleKey) {
144-
contextMenu.optionTitle = this.translate && this.translate.instant && this.translate.instant(contextMenu.optionTitleKey) || contextMenu.optionTitle;
144+
contextMenu.optionTitle = this.translate && this.translate.currentLang && this.translate.instant && this.translate.instant(contextMenu.optionTitleKey) || contextMenu.optionTitle;
145145
menuOptions.optionTitle = contextMenu.optionTitle;
146146
}
147147
const originalCommandItems = this._userOriginalContextMenu && Array.isArray(this._userOriginalContextMenu.commandItems) ? this._userOriginalContextMenu.commandItems : [];

0 commit comments

Comments
 (0)