Skip to content

Commit a04607d

Browse files
authored
Merge pull request #371 from youda97/table-strings
table: Externalize hardcoded strings
2 parents 8515022 + a6dd376 commit a04607d

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

src/i18n/en.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@
117117
"NEXT_3": "Scroll to next 3 pages",
118118
"FILTER": "Filter",
119119
"END_OF_DATA": "You've reached the end of your content",
120-
"SCROLL_TOP": "Scroll to top"
120+
"SCROLL_TOP": "Scroll to top",
121+
"CHECKBOX_HEADER": "Select all rows",
122+
"CHECKBOX_ROW": "Select row",
123+
"EXPAND_BUTTON": "Expand row",
124+
"SORT_DESCENDING": "Sort rows by this header in descending order",
125+
"SORT_ASCENDING": "Sort rows by this header in ascending order"
126+
121127
},
122128
"TABS": {
123129
"BUTTON_ARIA_LEFT": "Go to the previous tab",

src/table/table.component.ts

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ import { I18n } from "./../i18n/i18n.module";
178178
[size]="size !== ('lg' ? 'sm' : 'md')"
179179
[(ngModel)]="selectAllCheckbox"
180180
[indeterminate]="selectAllCheckboxSomeSelected"
181-
aria-label="Select all rows"
181+
[attr.aria-label]="checkboxHeaderLabel | async"
182182
(change)="onSelectAllCheckboxChange()">
183183
</ibm-checkbox>
184184
</th>
@@ -199,7 +199,7 @@ import { I18n } from "./../i18n/i18n.module";
199199
<button
200200
class="bx--table-sort-v2"
201201
*ngIf="this.sort.observers.length > 0 && column.sortable"
202-
[attr.aria-label]="(column.sorted && column.ascending ? sortDescendingLabel : sortAscendingLabel)"
202+
[attr.aria-label]="(column.sorted && column.ascending ? sortDescendingLabel : sortAscendingLabel) | async"
203203
aria-live="polite"
204204
[ngClass]="{
205205
'bx--table-sort-v2--active': column.sorted,
@@ -232,7 +232,7 @@ import { I18n } from "./../i18n/i18n.module";
232232
aria-haspopup="true"
233233
[ibmTooltip]="column.filterTemplate"
234234
trigger="click"
235-
[title]="translations.FILTER"
235+
[title]="filterTitle | async"
236236
placement="bottom,top"
237237
[data]="column.filterData">
238238
<svg
@@ -307,7 +307,7 @@ import { I18n } from "./../i18n/i18n.module";
307307
<button
308308
*ngIf="model.isRowExpandable(i)"
309309
(click)="model.expandRow(i, !model.rowsExpanded[i])"
310-
[attr.aria-label]="expandButtonAriaLabel"
310+
[attr.aria-label]="expandButtonAriaLabel | async"
311311
class="bx--table-expand-v2__button">
312312
<svg class="bx--table-expand-v2__svg" width="7" height="12" viewBox="0 0 7 12">
313313
<path fill-rule="nonzero" d="M5.569 5.994L0 .726.687 0l6.336 5.994-6.335 6.002L0 11.27z" />
@@ -316,7 +316,7 @@ import { I18n } from "./../i18n/i18n.module";
316316
</td>
317317
<td *ngIf="!skeleton && showSelectionColumn">
318318
<ibm-checkbox
319-
aria-label="Select row"
319+
[attr.aria-label]="checkboxRowLabel | async"
320320
[size]="size !== ('lg' ? 'sm' : 'md')"
321321
[(ngModel)]="model.rowsSelected[i]"
322322
(change)="onRowCheckboxChange(i)">
@@ -356,9 +356,9 @@ import { I18n } from "./../i18n/i18n.module";
356356
</tr>
357357
<tr *ngIf="this.model.isEnd">
358358
<td class="table_end-indicator">
359-
<h5>{{translations.END_OF_DATA}}</h5>
359+
<h5>{{endOfDataText | async}}</h5>
360360
<button (click)="scrollToTop($event)" class="btn--secondary-sm">
361-
{{translations.SCROLL_TOP}}
361+
{{scrollTopText | async}}
362362
</button>
363363
</td>
364364
</tr>
@@ -409,11 +409,6 @@ export class Table {
409409
* Set to `true` for a loading table.
410410
*/
411411
@Input() skeleton = false;
412-
/**
413-
* Object of all the strings table needs.
414-
* Defaults to the `TABLE` value from the i18n service.
415-
*/
416-
@Input() translations = this.i18n.get().TABLE;
417412

418413
/**
419414
* `TableModel` with data the table is to display.
@@ -497,9 +492,64 @@ export class Table {
497492
*/
498493
@Input() columnsDraggable = false;
499494

500-
@Input() expandButtonAriaLabel = "Expand row";
501-
@Input() sortDescendingLabel = "Sort rows by this header in descending order";
502-
@Input() sortAscendingLabel = "Sort rows by this header in ascending order";
495+
@Input()
496+
set expandButtonAriaLabel(value) {
497+
this._expandButtonAriaLabel.next(value);
498+
}
499+
get expandButtonAriaLabel() {
500+
return this._expandButtonAriaLabel;
501+
}
502+
@Input()
503+
set sortDescendingLabel(value) {
504+
this._sortDescendingLabel.next(value);
505+
}
506+
get sortDescendingLabel() {
507+
return this._sortDescendingLabel;
508+
}
509+
@Input()
510+
set sortAscendingLabel(value) {
511+
this._sortAscendingLabel.next(value);
512+
}
513+
get sortAscendingLabel() {
514+
return this._sortAscendingLabel;
515+
}
516+
517+
/**
518+
* Expects an object that contains some or all of:
519+
* ```
520+
* {
521+
* "FILTER": "Filter",
522+
* "END_OF_DATA": "You've reached the end of your content",
523+
* "SCROLL_TOP": "Scroll to top",
524+
* "CHECKBOX_HEADER": "Select all rows",
525+
* "CHECKBOX_ROW": "Select row"
526+
* }
527+
* ```
528+
*/
529+
@Input()
530+
set translations (value) {
531+
if (value.FILTER) {
532+
this.filterTitle.next(value.FILTER);
533+
}
534+
if (value.END_OF_DATA) {
535+
this.endOfDataText.next(value.END_OF_DATA);
536+
}
537+
if (value.SCROLL_TOP) {
538+
this.scrollTopText.next(value.SCROLL_TOP);
539+
}
540+
if (value.CHECKBOX_HEADER) {
541+
this.checkboxHeaderLabel.next(value.CHECKBOX_HEADER);
542+
}
543+
if (value.CHECKBOX_ROW) {
544+
this.checkboxRowLabel.next(value.CHECKBOX_ROW);
545+
}
546+
}
547+
548+
checkboxHeaderLabel = this.i18n.get("TABLE.CHECKBOX_HEADER");
549+
checkboxRowLabel = this.i18n.get("TABLE.CHECKBOX_ROW");
550+
endOfDataText = this.i18n.get("TABLE.END_OF_DATA");
551+
scrollTopText = this.i18n.get("TABLE.SCROLL_TOP");
552+
filterTitle = this.i18n.get("TABLE.FILTER");
503553

504554
/**
505555
* Controls if all checkboxes are viewed as selected.
@@ -580,6 +630,10 @@ export class Table {
580630

581631
protected _model: TableModel;
582632

633+
protected _expandButtonAriaLabel = this.i18n.get("TABLE.EXPAND_BUTTON");
634+
protected _sortDescendingLabel = this.i18n.get("TABLE.SORT_DESCENDING");
635+
protected _sortAscendingLabel = this.i18n.get("TABLE.SORT_ASCENDING");
636+
583637
protected columnResizeWidth: number;
584638
protected columnResizeMouseX: number;
585639
protected mouseMoveSubscription: Subscription;

0 commit comments

Comments
 (0)