-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(formatter): translate formatter was causing issues in some occasion
- Loading branch information
1 parent
c99a14c
commit 8acdc28
Showing
2 changed files
with
8 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletions
10
aurelia-slickgrid/src/aurelia-slickgrid/formatters/translateFormatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) : ''; | ||
}; |