Skip to content

Commit

Permalink
feat: translations (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
Co-authored-by: Faris Ansari <netchamp.faris@gmail.com>
  • Loading branch information
3 people committed Jan 3, 2022
1 parent 8ceadac commit 6d6602f
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 69 deletions.
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ <h1>Frappé DataTable</h1>
dynamicRowHeight: true,
treeView: treeView,
showTotalRow: true,
// language: 'myLang',
// translations: {
// myLang: {
// "Sort Ascending": "Sort low to high",
// "{count} cells copied": {
// "1": "1 cell was copied",
// "2": "2 cells were copied",
// "default": "Many cells were copied"
// }
// }
// },
// filterRows(keyword, cells, colIndex) {
// return cells
// .filter(cell => cell.content.includes(keyword))
Expand Down
5 changes: 4 additions & 1 deletion src/cellmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export default class CellManager {
bindCopyCellContents() {
this.keyboard.on('ctrl+c', () => {
const noOfCellsCopied = this.copyCellContents(this.$focusedCell, this.$selectionCursor);
const message = `${noOfCellsCopied} cell${noOfCellsCopied > 1 ? 's' : ''} copied`;
const message = this.instance.translate('{count} cells copied', {
count: noOfCellsCopied
});

if (noOfCellsCopied) {
this.instance.showToastMessage(message, 2);
}
Expand Down
28 changes: 24 additions & 4 deletions src/datatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import RowManager from './rowmanager';
import BodyRenderer from './body-renderer';
import Style from './style';
import Keyboard from './keyboard';
import DEFAULT_OPTIONS from './defaults';
import TranslationManager from './translationmanager';
import getDefaultOptions from './defaults';

let defaultComponents = {
DataManager,
Expand All @@ -31,6 +32,8 @@ class DataTable {
throw new Error('Invalid argument given for `wrapper`');
}

this.initializeTranslations(options);
this.setDefaultOptions();
this.buildOptions(options);
this.prepare();
this.initializeComponents();
Expand All @@ -41,23 +44,36 @@ class DataTable {
}
}

initializeTranslations(options) {
this.language = options.language || 'en';
this.translationManager = new TranslationManager(this.language);

if (options.translations) {
this.translationManager.addTranslations(options.translations);
}
}

setDefaultOptions() {
this.DEFAULT_OPTIONS = getDefaultOptions(this);
}

buildOptions(options) {
this.options = this.options || {};

this.options = Object.assign(
{}, DEFAULT_OPTIONS,
{}, this.DEFAULT_OPTIONS,
this.options || {}, options
);

options.headerDropdown = options.headerDropdown || [];
this.options.headerDropdown = [
...DEFAULT_OPTIONS.headerDropdown,
...this.DEFAULT_OPTIONS.headerDropdown,
...options.headerDropdown
];

// custom user events
this.events = Object.assign(
{}, DEFAULT_OPTIONS.events,
{}, this.DEFAULT_OPTIONS.events,
this.options.events || {},
options.events || {}
);
Expand Down Expand Up @@ -243,6 +259,10 @@ class DataTable {
console.log.apply(console, arguments);
}
}

translate(str, args) {
return this.translationManager.translate(str, args);
}
}

DataTable.instances = 0;
Expand Down
128 changes: 65 additions & 63 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,73 @@
import filterRows from './filterRows';
import icons from './icons';

export default {
columns: [],
data: [],
dropdownButton: icons.chevronDown,
headerDropdown: [
{
label: 'Sort Ascending',
action: function (column) {
this.sortColumn(column.colIndex, 'asc');
export default function getDefaultOptions(instance) {
return {
columns: [],
data: [],
dropdownButton: icons.chevronDown,
headerDropdown: [
{
label: instance.translate('Sort Ascending'),
action: function (column) {
this.sortColumn(column.colIndex, 'asc');
}
},
{
label: instance.translate('Sort Descending'),
action: function (column) {
this.sortColumn(column.colIndex, 'desc');
}
},
{
label: instance.translate('Reset sorting'),
action: function (column) {
this.sortColumn(column.colIndex, 'none');
}
},
{
label: instance.translate('Remove column'),
action: function (column) {
this.removeColumn(column.colIndex);
}
}
],
events: {
onRemoveColumn(column) {},
onSwitchColumn(column1, column2) {},
onSortColumn(column) {},
onCheckRow(row) {},
onDestroy() {}
},
{
label: 'Sort Descending',
action: function (column) {
this.sortColumn(column.colIndex, 'desc');
}
hooks: {
columnTotal: null
},
{
label: 'Reset sorting',
action: function (column) {
this.sortColumn(column.colIndex, 'none');
}
sortIndicator: {
asc: '↑',
desc: '↓',
none: ''
},
{
label: 'Remove column',
action: function (column) {
this.removeColumn(column.colIndex);
}
}
],
events: {
onRemoveColumn(column) {},
onSwitchColumn(column1, column2) {},
onSortColumn(column) {},
onCheckRow(row) {},
onDestroy() {}
},
hooks: {
columnTotal: null
},
sortIndicator: {
asc: '↑',
desc: '↓',
none: ''
},
overrideComponents: {
// ColumnManager: CustomColumnManager
},
filterRows: filterRows,
freezeMessage: '',
getEditor: null,
serialNoColumn: true,
checkboxColumn: false,
clusterize: true,
logs: false,
layout: 'fixed', // fixed, fluid, ratio
noDataMessage: 'No Data',
cellHeight: 40,
minimumColumnWidth: 30,
inlineFilters: false,
treeView: false,
checkedRowStatus: true,
dynamicRowHeight: false,
pasteFromClipboard: false,
showTotalRow: false,
direction: 'ltr',
disableReorderColumn: false
overrideComponents: {
// ColumnManager: CustomColumnManager
},
filterRows: filterRows,
freezeMessage: '',
getEditor: null,
serialNoColumn: true,
checkboxColumn: false,
clusterize: true,
logs: false,
layout: 'fixed', // fixed, fluid, ratio
noDataMessage: instance.translate('No Data'),
cellHeight: 40,
minimumColumnWidth: 30,
inlineFilters: false,
treeView: false,
checkedRowStatus: true,
dynamicRowHeight: false,
pasteFromClipboard: false,
showTotalRow: false,
direction: 'ltr',
disableReorderColumn: false
};
};
5 changes: 4 additions & 1 deletion src/rowmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export default class RowManager {
const checkedRows = this.getCheckedRows();
const count = checkedRows.length;
if (count > 0) {
this.bodyRenderer.showToastMessage(`${count} row${count > 1 ? 's' : ''} selected`);
let message = this.instance.translate('{count} rows selected', {
count: count
});
this.bodyRenderer.showToastMessage(message);
} else {
this.bodyRenderer.clearToastMessage();
}
Expand Down
30 changes: 30 additions & 0 deletions src/translationmanager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { format } from './utils';
import getTranslations from './translations';

export default class TranslationManager {
constructor(language) {
this.language = language;
this.translations = getTranslations();
}

addTranslations(translations) {
this.translations = Object.assign(this.translations, translations);
}

translate(sourceText, args) {
let translation = (this.translations[this.language] &&
this.translations[this.language][sourceText]) || sourceText;

if (typeof translation === 'object') {
translation = args && args.count ?
this.getPluralizedTranslation(translation, args.count) :
sourceText;
}

return format(translation, args || {});
}

getPluralizedTranslation(translations, count) {
return translations[count] || translations['default'];
}
};
15 changes: 15 additions & 0 deletions src/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Sort Ascending": "Aufsteigend sortieren",
"Sort Descending": "Absteigend sortieren",
"Reset sorting": "Sortierung zurücksetzen",
"Remove column": "Spalte entfernen",
"No Data": "Keine Daten",
"{count} cells copied": {
"1": "{count} Zelle kopiert",
"default": "{count} Zellen kopiert"
},
"{count} rows selected": {
"1": "{count} Zeile ausgewählt",
"default": "{count} Zeilen ausgewählt"
}
}
15 changes: 15 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Sort Ascending": "Sort Ascending",
"Sort Descending": "Sort Descending",
"Reset sorting": "Reset sorting",
"Remove column": "Remove column",
"No Data": "No Data",
"{count} cells copied": {
"1": "{count} cell copied",
"default": "{count} cells copied"
},
"{count} rows selected": {
"1": "{count} row selected",
"default": "{count} rows selected"
}
}
15 changes: 15 additions & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Sort Ascending": "Trier par ordre croissant",
"Sort Descending": "Trier par ordre décroissant",
"Reset sorting": "Réinitialiser le tri",
"Remove column": "Supprimer colonne",
"No Data": "Pas de données",
"{count} cells copied": {
"1": "{count} cellule copiée",
"default": "{count} cellules copiées"
},
"{count} rows selected": {
"1": "{count} ligne sélectionnée",
"default": "{count} lignes sélectionnées"
}
}
13 changes: 13 additions & 0 deletions src/translations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import en from './en.json';
import de from './de.json';
import fr from './fr.json';
import it from './it.json';

export default function getTranslations() {
return {
en,
de,
fr,
it,
};
};
15 changes: 15 additions & 0 deletions src/translations/it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Sort Ascending": "Ordinamento ascendente",
"Sort Descending": "Ordinamento decrescente",
"Reset sorting": "Azzeramento ordinamento",
"Remove column": "Rimuovi colonna",
"No Data": "Nessun dato",
"{count} cells copied": {
"1": "Copiato {count} cella",
"default": "{count} celle copiate"
},
"{count} rows selected": {
"1": "{count} linea selezionata",
"default": "{count} linee selezionate"
}
}
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ export function numberSortAsc(a, b) {
export function stripHTML(html) {
return html.replace(/<[^>]*>/g, '');
};

export function format(str, args) {
if (!str) return str;

Object.keys(args).forEach(arg => {
let regex = new RegExp(`{(${arg})}`, 'g');
str = str.replace(regex, args[arg]);
});

return str;
};

0 comments on commit 6d6602f

Please sign in to comment.