Skip to content

Commit

Permalink
fix(formatter): translate formatter was causing issues in some occasion
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Mar 13, 2018
1 parent c99a14c commit 8acdc28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ export const translateBooleanFormatter: Formatter = (row: number, cell: number,
const gridOptions = (grid && typeof grid.getOptions === 'function') ? grid.getOptions() : {};
const columnParams = columnDef.params || {};
const gridParams = gridOptions.params || {};
const i18n = gridParams.i18n || columnParams.i18n;

if ((!columnParams.i18n || !(columnParams.i18n instanceof I18N)) && (!gridParams.i18n || !(gridParams.i18n instanceof I18N))) {
throw new Error(`The translate formatter requires the "I18N" Service to be provided as a Column Definition params or a Grid Option params.
if (!i18n || typeof i18n.tr !== 'function') {
throw new Error(`The translate formatter requires the "I18N" Service to be provided as a Grid Options or Column Definition "params".
For example: this.gridOptions = { enableTranslate: true, params: { i18n: this.i18n }}`);
}

const translate = gridParams.i18n || columnParams.i18n;

// make sure the value is a string (for example a boolean value would throw an error)
if (value !== undefined && typeof value !== 'string') {
value = value + '';
}
return value ? translate.tr(value.toUpperCase() as string) : '';
return value ? i18n.tr(value.toUpperCase() as string) : '';
};
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Column, Formatter } from './../models/index';
import { I18N } from 'aurelia-i18n';

/** Takes a cell value and translates it (i18n). Requires an instance of the I18N Service:: `params: { i18n: this.i18n } */
export const translateFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any, grid: any) => {
const gridOptions = (grid && typeof grid.getOptions === 'function') ? grid.getOptions() : {};
const columnParams = columnDef.params || {};
const gridParams = gridOptions.params || {};
const i18n = gridParams.i18n || columnParams.i18n;

if ((!columnParams.i18n || !(columnParams.i18n instanceof I18N)) && (!gridParams.i18n || !(gridParams.i18n instanceof I18N))) {
throw new Error(`The translate formatter requires the "I18N" Service to be provided as a Column Definition params or a Grid Option params.
if (!i18n || typeof i18n.tr !== 'function') {
throw new Error(`The translate formatter requires the "I18N" Service to be provided as a Grid Options or Column Definition "params".
For example: this.gridOptions = { enableTranslate: true, params: { i18n: this.i18n }}`);
}

const translate = gridParams.i18n || columnParams.i18n;

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

return value ? translate.tr(value) : '';
return value ? i18n.tr(value) : '';
};

0 comments on commit 8acdc28

Please sign in to comment.