feat: translations (#145)
Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com> Co-authored-by: Faris Ansari <netchamp.faris@gmail.com>
This commit is contained in:
parent
8ceadaccba
commit
6d6602f202
11
index.html
11
index.html
@ -189,6 +189,17 @@
|
|||||||
dynamicRowHeight: true,
|
dynamicRowHeight: true,
|
||||||
treeView: treeView,
|
treeView: treeView,
|
||||||
showTotalRow: true,
|
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) {
|
// filterRows(keyword, cells, colIndex) {
|
||||||
// return cells
|
// return cells
|
||||||
// .filter(cell => cell.content.includes(keyword))
|
// .filter(cell => cell.content.includes(keyword))
|
||||||
|
|||||||
@ -133,7 +133,10 @@ export default class CellManager {
|
|||||||
bindCopyCellContents() {
|
bindCopyCellContents() {
|
||||||
this.keyboard.on('ctrl+c', () => {
|
this.keyboard.on('ctrl+c', () => {
|
||||||
const noOfCellsCopied = this.copyCellContents(this.$focusedCell, this.$selectionCursor);
|
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) {
|
if (noOfCellsCopied) {
|
||||||
this.instance.showToastMessage(message, 2);
|
this.instance.showToastMessage(message, 2);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,8 @@ import RowManager from './rowmanager';
|
|||||||
import BodyRenderer from './body-renderer';
|
import BodyRenderer from './body-renderer';
|
||||||
import Style from './style';
|
import Style from './style';
|
||||||
import Keyboard from './keyboard';
|
import Keyboard from './keyboard';
|
||||||
import DEFAULT_OPTIONS from './defaults';
|
import TranslationManager from './translationmanager';
|
||||||
|
import getDefaultOptions from './defaults';
|
||||||
|
|
||||||
let defaultComponents = {
|
let defaultComponents = {
|
||||||
DataManager,
|
DataManager,
|
||||||
@ -31,6 +32,8 @@ class DataTable {
|
|||||||
throw new Error('Invalid argument given for `wrapper`');
|
throw new Error('Invalid argument given for `wrapper`');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.initializeTranslations(options);
|
||||||
|
this.setDefaultOptions();
|
||||||
this.buildOptions(options);
|
this.buildOptions(options);
|
||||||
this.prepare();
|
this.prepare();
|
||||||
this.initializeComponents();
|
this.initializeComponents();
|
||||||
@ -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) {
|
buildOptions(options) {
|
||||||
this.options = this.options || {};
|
this.options = this.options || {};
|
||||||
|
|
||||||
this.options = Object.assign(
|
this.options = Object.assign(
|
||||||
{}, DEFAULT_OPTIONS,
|
{}, this.DEFAULT_OPTIONS,
|
||||||
this.options || {}, options
|
this.options || {}, options
|
||||||
);
|
);
|
||||||
|
|
||||||
options.headerDropdown = options.headerDropdown || [];
|
options.headerDropdown = options.headerDropdown || [];
|
||||||
this.options.headerDropdown = [
|
this.options.headerDropdown = [
|
||||||
...DEFAULT_OPTIONS.headerDropdown,
|
...this.DEFAULT_OPTIONS.headerDropdown,
|
||||||
...options.headerDropdown
|
...options.headerDropdown
|
||||||
];
|
];
|
||||||
|
|
||||||
// custom user events
|
// custom user events
|
||||||
this.events = Object.assign(
|
this.events = Object.assign(
|
||||||
{}, DEFAULT_OPTIONS.events,
|
{}, this.DEFAULT_OPTIONS.events,
|
||||||
this.options.events || {},
|
this.options.events || {},
|
||||||
options.events || {}
|
options.events || {}
|
||||||
);
|
);
|
||||||
@ -243,6 +259,10 @@ class DataTable {
|
|||||||
console.log.apply(console, arguments);
|
console.log.apply(console, arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
translate(str, args) {
|
||||||
|
return this.translationManager.translate(str, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DataTable.instances = 0;
|
DataTable.instances = 0;
|
||||||
|
|||||||
128
src/defaults.js
128
src/defaults.js
@ -1,71 +1,73 @@
|
|||||||
import filterRows from './filterRows';
|
import filterRows from './filterRows';
|
||||||
import icons from './icons';
|
import icons from './icons';
|
||||||
|
|
||||||
export default {
|
export default function getDefaultOptions(instance) {
|
||||||
columns: [],
|
return {
|
||||||
data: [],
|
columns: [],
|
||||||
dropdownButton: icons.chevronDown,
|
data: [],
|
||||||
headerDropdown: [
|
dropdownButton: icons.chevronDown,
|
||||||
{
|
headerDropdown: [
|
||||||
label: 'Sort Ascending',
|
{
|
||||||
action: function (column) {
|
label: instance.translate('Sort Ascending'),
|
||||||
this.sortColumn(column.colIndex, 'asc');
|
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() {}
|
||||||
},
|
},
|
||||||
{
|
hooks: {
|
||||||
label: 'Sort Descending',
|
columnTotal: null
|
||||||
action: function (column) {
|
|
||||||
this.sortColumn(column.colIndex, 'desc');
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
sortIndicator: {
|
||||||
label: 'Reset sorting',
|
asc: '↑',
|
||||||
action: function (column) {
|
desc: '↓',
|
||||||
this.sortColumn(column.colIndex, 'none');
|
none: ''
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
overrideComponents: {
|
||||||
label: 'Remove column',
|
// ColumnManager: CustomColumnManager
|
||||||
action: function (column) {
|
},
|
||||||
this.removeColumn(column.colIndex);
|
filterRows: filterRows,
|
||||||
}
|
freezeMessage: '',
|
||||||
}
|
getEditor: null,
|
||||||
],
|
serialNoColumn: true,
|
||||||
events: {
|
checkboxColumn: false,
|
||||||
onRemoveColumn(column) {},
|
clusterize: true,
|
||||||
onSwitchColumn(column1, column2) {},
|
logs: false,
|
||||||
onSortColumn(column) {},
|
layout: 'fixed', // fixed, fluid, ratio
|
||||||
onCheckRow(row) {},
|
noDataMessage: instance.translate('No Data'),
|
||||||
onDestroy() {}
|
cellHeight: 40,
|
||||||
},
|
minimumColumnWidth: 30,
|
||||||
hooks: {
|
inlineFilters: false,
|
||||||
columnTotal: null
|
treeView: false,
|
||||||
},
|
checkedRowStatus: true,
|
||||||
sortIndicator: {
|
dynamicRowHeight: false,
|
||||||
asc: '↑',
|
pasteFromClipboard: false,
|
||||||
desc: '↓',
|
showTotalRow: false,
|
||||||
none: ''
|
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: 'No Data',
|
|
||||||
cellHeight: 40,
|
|
||||||
minimumColumnWidth: 30,
|
|
||||||
inlineFilters: false,
|
|
||||||
treeView: false,
|
|
||||||
checkedRowStatus: true,
|
|
||||||
dynamicRowHeight: false,
|
|
||||||
pasteFromClipboard: false,
|
|
||||||
showTotalRow: false,
|
|
||||||
direction: 'ltr',
|
|
||||||
disableReorderColumn: false
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -133,7 +133,10 @@ export default class RowManager {
|
|||||||
const checkedRows = this.getCheckedRows();
|
const checkedRows = this.getCheckedRows();
|
||||||
const count = checkedRows.length;
|
const count = checkedRows.length;
|
||||||
if (count > 0) {
|
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 {
|
} else {
|
||||||
this.bodyRenderer.clearToastMessage();
|
this.bodyRenderer.clearToastMessage();
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/translationmanager.js
Normal file
30
src/translationmanager.js
Normal file
@ -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
src/translations/de.json
Normal file
15
src/translations/de.json
Normal file
@ -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
src/translations/en.json
Normal file
15
src/translations/en.json
Normal file
@ -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
src/translations/fr.json
Normal file
15
src/translations/fr.json
Normal file
@ -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
src/translations/index.js
Normal file
13
src/translations/index.js
Normal file
@ -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
src/translations/it.json
Normal file
15
src/translations/it.json
Normal file
@ -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
src/utils.js
11
src/utils.js
@ -138,3 +138,14 @@ export function numberSortAsc(a, b) {
|
|||||||
export function stripHTML(html) {
|
export function stripHTML(html) {
|
||||||
return html.replace(/<[^>]*>/g, '');
|
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;
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user