Skip to content

Commit 2a0e2ec

Browse files
feat(i18n): add translations managment
1 parent 72307a4 commit 2a0e2ec

File tree

9 files changed

+101
-6
lines changed

9 files changed

+101
-6
lines changed

js/Components/HeaderCell.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ export default {
55
type: Object,
66
required: true,
77
},
8+
translations: {
9+
type: Object,
10+
default: () => {
11+
return {};
12+
},
13+
required: true,
14+
},
815
},
916
1017
methods: {
@@ -15,4 +22,4 @@ export default {
1522
},
1623
},
1724
};
18-
</script>
25+
</script>

js/Components/Pagination.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ const Pagination = {
99
1010
computed: {
1111
translations() {
12+
if (
13+
this.meta.hasOwnProperty("translations") &&
14+
Object.keys(this.meta.translations).length > 0
15+
) {
16+
return this.meta.translations;
17+
}
1218
return Pagination.defaultTranslations;
1319
},
1420

js/Components/Table.vue

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export default {
2525
required: false,
2626
},
2727
28+
translations: {
29+
type: Object,
30+
default: () => {
31+
return {};
32+
},
33+
required: true,
34+
},
35+
2836
search: {
2937
type: Object,
3038
default: () => {
@@ -69,18 +77,23 @@ export default {
6977
},
7078
7179
paginationMeta() {
80+
let paginationMeta = Object.assign({}, this.meta, {
81+
translations: this.translations,
82+
});
7283
if (this.hasBody) {
73-
return this.meta;
84+
return paginationMeta;
7485
}
7586
76-
const hasPagination = 'meta' in this.meta || ('total' in this.meta && 'to' in this.meta && 'from' in this.meta);
87+
const hasPagination =
88+
"meta" in this.meta ||
89+
("total" in this.meta && "to" in this.meta && "from" in this.meta);
7790
7891
if (hasPagination) {
79-
return this.meta;
92+
return paginationMeta;
8093
}
8194
8295
return { meta: { total: 0 } };
83-
}
96+
},
8497
},
8598
8699
data() {
@@ -141,4 +154,4 @@ export default {
141154
},
142155
},
143156
};
144-
</script>
157+
</script>

js/Components/TableAddSearchRow.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export default {
1818
type: Function,
1919
required: true,
2020
},
21+
translations: {
22+
type: Object,
23+
default: () => {
24+
return {};
25+
},
26+
required: true,
27+
},
2128
},
2229
2330
computed: {

js/Components/TableColumns.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export default {
1414
type: Function,
1515
required: true,
1616
},
17+
translations: {
18+
type: Object,
19+
default: () => {
20+
return {};
21+
},
22+
required: true,
23+
},
1724
},
1825
1926
methods: {

js/Components/TableFilter.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export default {
1212
type: Function,
1313
required: true,
1414
},
15+
translations: {
16+
type: Object,
17+
default: () => {
18+
return {};
19+
},
20+
required: true,
21+
},
1522
},
1623
1724
computed: {

js/Components/TableGlobalSearch.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export default {
1111
type: Function,
1212
required: true,
1313
},
14+
translations: {
15+
type: Object,
16+
default: () => {
17+
return {};
18+
},
19+
required: true,
20+
},
1421
},
1522
};
1623
</script>

js/Components/TableSearchRows.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ export default {
2323
type: Function,
2424
required: true,
2525
},
26+
translations: {
27+
type: Object,
28+
default: () => {
29+
return {};
30+
},
31+
required: true,
32+
},
2633
},
2734
2835
methods: {

php/InertiaTable.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class InertiaTable
1616
private Collection $search;
1717
private Collection $filters;
1818
private bool $globalSearch = true;
19+
private array $translations;
1920

2021
public function __construct(Request $request)
2122
{
@@ -25,6 +26,7 @@ public function __construct(Request $request)
2526
$this->sortables = [];
2627
$this->search = new Collection;
2728
$this->filters = new Collection;
29+
$this->translations = $this->getDefaultTranlsations();
2830
}
2931

3032
/**
@@ -57,6 +59,7 @@ public function getQueryBuilderProps(): array
5759
'columns' => $columns->isNotEmpty() ? $columns->all() : (object) [],
5860
'search' => $search->isNotEmpty() ? $search->all() : (object) [],
5961
'filters' => $filters->isNotEmpty() ? $filters->all() : (object) [],
62+
'translations' => $this->translations,
6063
];
6164
}
6265

@@ -202,6 +205,19 @@ public function addColumns(array $columns = []): self
202205
return $this;
203206
}
204207

208+
/**
209+
* Add and update translations array
210+
*
211+
* @param array $updatedTranslations
212+
* @return self
213+
*/
214+
public function updateTranslations(array $updatedTranslations = []): self
215+
{
216+
$this->translations = array_merge($this->translations, $updatedTranslations);
217+
218+
return $this;
219+
}
220+
205221
/**
206222
* Add a search row to the query builder.
207223
*
@@ -248,4 +264,22 @@ public function addFilter(string $key, string $label, array $options): self
248264

249265
return $this;
250266
}
267+
268+
private function getDefaultTranlsations(): array
269+
{
270+
$translationsArray = __('pagination');
271+
272+
$translationsArray = array_merge($translationsArray, [
273+
'filter' => "Filter",
274+
"next" => "suivant",
275+
"no_results_found" => "Aucun résultats",
276+
"of" => "sur",
277+
"previous" => "précédent",
278+
"results" => "résultats",
279+
"search_placeholder" => "Chercher",
280+
"to" => "à",
281+
]);
282+
283+
return $translationsArray;
284+
}
251285
}

0 commit comments

Comments
 (0)