Skip to content

Commit

Permalink
fix(core): rename i18n to translater & fix few other issues (#174)
Browse files Browse the repository at this point in the history
* fix(core): rename i18n to translater & fix few other issues
- fix couple of issues found for the lib to work with Aurelia-Slickgrid
  • Loading branch information
ghiscoding authored Nov 30, 2020
1 parent 38414a1 commit 34c963a
Show file tree
Hide file tree
Showing 32 changed files with 128 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Example10 {

this.gridOptions = {
enableTranslate: true,
i18n: this.translateService, // pass the TranslateService instance to the grid
translater: this.translateService, // pass the TranslateService instance to the grid
enableAutoResize: false,
gridHeight: 275,
gridWidth: 900,
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/editors/__tests__/dateEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const dataViewStub = {
const gridOptionMock = {
autoCommitEdit: false,
editable: true,
i18n: null,
translater: null,
} as unknown as GridOption;

const getEditorLockMock = {
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('DateEditor', () => {
});

it('should initialize the editor', () => {
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;
editor = new DateEditor(editorArguments);
const editorCount = divContainer.querySelectorAll('input.editor-text.editor-startDate').length;
expect(editorCount).toBe(1);
Expand Down Expand Up @@ -435,7 +435,7 @@ describe('DateEditor', () => {

describe('with different locale', () => {
it('should display text in new locale', (done) => {
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;

translateService.use('fr-CA'); // will be trimmed to "fr"
editor = new DateEditor(editorArguments);
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/editors/__tests__/longTextEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const dataViewStub = {
const gridOptionMock = {
autoCommitEdit: false,
editable: true,
i18n: null,
translater: null,
editorTypingDebounce: 0,
} as unknown as GridOption;

Expand Down Expand Up @@ -101,7 +101,7 @@ describe('LongTextEditor', () => {
});

it('should initialize the editor', () => {
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;
editor = new LongTextEditor(editorArguments);
const editorCount = document.body.querySelectorAll('.slick-large-editor-text.editor-title textarea').length;
const editorTextCounter = document.body.querySelectorAll<HTMLDivElement>('.slick-large-editor-text.editor-title .editor-footer .counter');
Expand All @@ -120,7 +120,7 @@ describe('LongTextEditor', () => {
});

it('should initialize the editor with default constant text when translate service is not provided', () => {
gridOptionMock.i18n = undefined as any;
gridOptionMock.translater = undefined as any;
editor = new LongTextEditor(editorArguments);
const editorCount = document.body.querySelectorAll('.slick-large-editor-text.editor-title textarea').length;
const editorFooterElm = document.body.querySelector('.slick-large-editor-text.editor-title .editor-footer') as HTMLDivElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const dataViewStub = {
const gridOptionMock = {
autoCommitEdit: false,
editable: true,
i18n: null,
translater: null,
} as unknown as GridOption;

const getEditorLockMock = {
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('MultipleSelectEditor', () => {

it('should initialize the editor', () => {
(mockColumn.internalColumnEditor as ColumnEditor).collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }];
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;
editor = new MultipleSelectEditor(editorArguments);
const editorCount = document.body.querySelectorAll('select.ms-filter.editor-gender').length;
const spy = jest.spyOn(editor, 'show');
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/editors/__tests__/selectEditor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const dataViewStub = {
const gridOptionMock = {
autoCommitEdit: false,
editable: true,
i18n: null as any,
translater: null as any,
} as unknown as GridOption;

const getEditorLockMock = {
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('SelectEditor', () => {

it('should initialize the editor', () => {
(mockColumn.internalColumnEditor as ColumnEditor).collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }];
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;
editor = new SelectEditor(editorArguments, true);
editor.focus();
const editorCount = document.body.querySelectorAll('select.ms-filter.editor-gender').length;
Expand All @@ -154,7 +154,7 @@ describe('SelectEditor', () => {
const promise = new Promise(resolve => resolve(mockCollection));
(mockColumn.internalColumnEditor as ColumnEditor).collection = null as any;
(mockColumn.internalColumnEditor as ColumnEditor).collectionAsync = promise;
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;

editor = new SelectEditor(editorArguments, true);
const disableSpy = jest.spyOn(editor, 'disable');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const dataViewStub = {
const gridOptionMock = {
autoCommitEdit: false,
editable: true,
i18n: null,
translater: null,
} as unknown as GridOption;

const getEditorLockMock = {
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('SingleSelectEditor', () => {

it('should initialize the editor', () => {
(mockColumn.internalColumnEditor as ColumnEditor).collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }];
gridOptionMock.i18n = translateService;
gridOptionMock.translater = translateService;
editor = new SingleSelectEditor(editorArguments);
const editorCount = document.body.querySelectorAll('select.ms-filter.editor-gender').length;

Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/editors/dateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export class DateEditor implements Editor {
}
this.grid = args.grid;
this.gridOptions = (this.grid.getOptions() || {}) as GridOption;
if (this.gridOptions && this.gridOptions.i18n) {
this._translaterService = this.gridOptions.i18n;
if (this.gridOptions?.translater) {
this._translaterService = this.gridOptions.translater;
}
this.init();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/editors/longTextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class LongTextEditor implements Editor {
this.grid = args.grid;
this.gridOptions = args.grid && args.grid.getOptions() as GridOption;
const options = this.gridOptions || this.args.column.params || {};
if (options && options.i18n) {
this._translater = options.i18n;
if (options?.translater) {
this._translater = options.translater;
}

// get locales provided by user in forRoot or else use default English locales via the Constants
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/editors/selectEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export class SelectEditor implements Editor {
}
this.grid = args.grid;
this.gridOptions = (this.grid.getOptions() || {}) as GridOption;
if (this.gridOptions && this.gridOptions.i18n) {
this._translaterService = this.gridOptions.i18n;
if (this.gridOptions?.translater) {
this._translaterService = this.gridOptions.translater;
}

// get locales provided by user in main file or else use default English locales via the Constants
Expand Down
32 changes: 16 additions & 16 deletions packages/common/src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
export * from './caseType.enum';
export * from './columnReorderFunction.type';
export * from './compositeEditorModalType.type';
export * from './fieldType.enum';
export * from './extensionList.type';
export * from './eventNamingStyle.enum';
export * from './emitterType.enum';
export * from './fileType.enum';
export * from './delimiterType.enum';
export * from './columnReorderFunction.type';
export * from './emitterType.enum';
export * from './eventNamingStyle.enum';
export * from './extensionList.type';
export * from './extensionName.enum';
export * from './fieldType.enum';
export * from './fileType.enum';
export * from './filterMultiplePassType.enum';
export * from './filterMultiplePassTypeString.type';
export * from './gridAutosizeColsMode.enum';
export * from './gridStateType.enum';
export * from './keyCode.enum';
export * from './operatorString.type';
export * from './operatorType.enum';
export * from './searchTerm.type';
export * from './slickControlList.enum';
export * from './slickPluginList.enum';
export * from './sortDirection.enum';
export * from './sortDirectionString.type';
export * from './sortDirectionNumber.enum';
export * from './sortDirection.enum';
export * from './slickPluginList.enum';
export * from './slickControlList.enum';
export * from './searchTerm.type';
export * from './operatorType.enum';
export * from './operatorString.type';
export * from './keyCode.enum';
export * from './gridStateType.enum';
export * from './gridAutosizeColsMode.enum';
export * from './filterMultiplePassTypeString.type';
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,48 @@ describe('the Translate Boolean Formatter', () => {

it('should return an empty string when null value is passed', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateBooleanFormatter(1, 1, null, {} as Column, {}, gridStub);
expect(output).toBe('');
});

it('should return an empty string when empty string value is passed', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateBooleanFormatter(1, 1, '', {} as Column, {}, gridStub);
expect(output).toBe('');
});

it('should return the translated value when value passed is boolean', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateBooleanFormatter(1, 1, 'TRUE', {} as Column, {}, gridStub);
expect(output).toBe('Vrai');
});

it('should return the translated value when value passed is a string', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateBooleanFormatter(1, 1, 'TRUE', {} as Column, {}, gridStub);
expect(output).toBe('Vrai');
});

it('should return the translated value when value passed is a string and i18n service is passed as a ColumnDef Params', async () => {
it('should return the translated value when value passed is a string and translater service is passed as a ColumnDef Params', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({});
const output = translateBooleanFormatter(1, 1, 'TRUE', { params: { i18n: translateService } } as Column, {}, gridStub);
const output = translateBooleanFormatter(1, 1, 'TRUE', { params: { translater: translateService } } as Column, {}, gridStub);
expect(output).toBe('Vrai');
});

it('should return the translated value when value passed is a string and i18n service is passed as a ColumnDef Params without any Grid object', async () => {
it('should return the translated value when value passed is a string and translater service is passed as a ColumnDef Params without any Grid object', async () => {
await translateService.use('fr');
const output = translateBooleanFormatter(1, 1, 'TRUE', { params: { i18n: translateService } } as Column, {});
const output = translateBooleanFormatter(1, 1, 'TRUE', { params: { translater: translateService } } as Column, {}, {} as any);
expect(output).toBe('Vrai');
});

it('should convert any type of value to string', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateBooleanFormatter(1, 1, 99, {} as Column, {}, gridStub);
expect(output).toBe('99');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ describe('the Translate Formatter', () => {

it('should return an empty string when null value is passed', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateFormatter(1, 1, null, {} as Column, {}, gridStub);
expect(translateService.getCurrentLanguage()).toBe('fr');
expect(output).toBe('');
});

it('should return an empty string when no value is passed', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateFormatter(1, 1, '', {} as Column, {}, gridStub);
expect(translateService.getCurrentLanguage()).toBe('fr');
expect(output).toBe('');
});

it('should return the translated value as string', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateFormatter(1, 1, 'HELLO', {} as Column, {}, gridStub);
expect(output).toBe('Bonjour');
});

it('should return the translated value when value passed is a string and i18n service is passed as a ColumnDef Params', async () => {
it('should return the translated value when value passed is a string and translater service is passed as a ColumnDef Params', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({});
const output = translateFormatter(1, 1, 'HELLO', { params: { i18n: translateService } } as Column, {}, gridStub);
const output = translateFormatter(1, 1, 'HELLO', { params: { translater: translateService } } as Column, {}, gridStub);
expect(output).toBe('Bonjour');
});

it('should return the translated value when value passed is a string and i18n service is passed as a ColumnDef Params without any Grid object', async () => {
it('should return the translated value when value passed is a string and translater service is passed as a ColumnDef Params without any Grid object', async () => {
await translateService.use('fr');
const output = translateFormatter(1, 1, 'HELLO', { params: { i18n: translateService } } as Column, {});
const output = translateFormatter(1, 1, 'HELLO', { params: { translater: translateService } } as Column, {}, {} as any);
expect(output).toBe('Bonjour');
});

it('should convert any type of value to string', async () => {
await translateService.use('fr');
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ i18n: translateService });
(gridStub.getOptions as jest.Mock).mockReturnValueOnce({ translater: translateService });
const output = translateFormatter(1, 1, 99, {} as Column, {}, gridStub);
expect(output).toBe('99');
});
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/formatters/formatters.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ export const Formatters = {
/** Takes a cell value number (between 0-100) and displays Bootstrap "progress-bar" a red (<30), silver (>30 & <70) or green (>=70) bar */
progressBar: progressBarFormatter,

/** Takes a cell value and translates it (i18n). Requires an instance of the Translate Service:: `i18n: this.translate */
/** Takes a cell value and translates it. Requires an instance of the Translate Service:: `translater: this.translate */
translate: translateFormatter,

/** Takes a boolean value, cast it to upperCase string and finally translates it (i18n). */
/** Takes a boolean value, cast it to upperCase string and finally translates it. Requires an instance of the Translate Service:: `translater: this.translate */
translateBoolean: translateBooleanFormatter,

/** Formatter that must be use with a Tree Data column */
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/formatters/translateBooleanFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { Formatter } from './../interfaces/index';
/** Takes a boolean value, cast it to upperCase string and finally translates it (i18n). */
export const translateBooleanFormatter: Formatter = (_row, _cell, value, columnDef, _dataContext, grid) => {
const gridOptions = (grid && typeof grid.getOptions === 'function') ? grid.getOptions() : {};
const i18n = gridOptions.i18n || (columnDef && columnDef.params && columnDef.params.i18n);
const translater = gridOptions.translater || (columnDef && columnDef.params && columnDef.params.translater);

if (!i18n || typeof i18n.translate !== 'function') {
if (!translater || typeof translater.translate !== 'function') {
throw new Error(`The translate formatter requires the Translate Service to be provided as a Grid Options or Column Definition "i18n".
For example: this.gridOptions = { enableTranslate: true, i18n: this.translateService }`);
For example: this.gridOptions = { enableTranslate: true, translater: this.translateService }`);
}

// make sure the value is a string (for example a boolean value would throw an error)
if (value !== undefined && value !== null && typeof value !== 'string') {
value = value + '';
}
return value ? i18n.translate(value.toUpperCase() as string) : '';
return value ? translater.translate(value.toUpperCase() as string) : '';
};
12 changes: 6 additions & 6 deletions packages/common/src/formatters/translateFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Formatter } from './../interfaces/index';

/** Takes a cell value and translates it (i18n). Requires an instance of the Translate Service:: `i18n: this.translate */
/** Takes a cell value and translates it (translater). Requires an instance of the Translate Service:: `translater: this.translate */
export const translateFormatter: Formatter = (_row, _cell, value, columnDef, _dataContext, grid) => {
const gridOptions = (grid && typeof grid.getOptions === 'function') ? grid.getOptions() : {};
const i18n = gridOptions.i18n || (columnDef && columnDef.params && columnDef.params.i18n);
const translater = gridOptions.translater || (columnDef && columnDef.params && columnDef.params.translater);

if (!i18n || typeof i18n.translate !== 'function') {
throw new Error(`The translate formatter requires the Translate Service to be provided as a Grid Options or Column Definition "i18n".
For example: this.gridOptions = { enableTranslate: true, i18n: this.translateService }`);
if (!translater || typeof translater.translate !== 'function') {
throw new Error(`The translate formatter requires the Translate Service to be provided as a Grid Options or Column Definition "translater".
For example: this.gridOptions = { enableTranslate: true, translater: this.translateService }`);
}

// make sure the value is a string (for example a boolean value would throw an error)
if (value !== undefined && value !== null && typeof value !== 'string') {
value = value + '';
}

return value ? i18n.translate(value) : '';
return value ? translater.translate(value) : '';
};
1 change: 1 addition & 0 deletions packages/common/src/global-grid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const GlobalGridOptions: GridOption = {
asyncEditorLoading: false,
autoFitColumnsOnFirstLoad: true,
autoResize: {
applyResizeToContainer: true,
calculateAvailableSizeBy: 'window',
bottomPadding: 20,
minHeight: 180,
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/interfaces/columnEditor.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldType } from '../enums';
import { FieldType } from '../enums/index';
import {
CollectionCustomStructure,
CollectionFilterBy,
Expand Down
Loading

0 comments on commit 34c963a

Please sign in to comment.