Skip to content

Commit 6327a8a

Browse files
committed
feat: basic translation search
It's now possible to search for a specific text or text fragment. The kook up is performed either in translated strings or its corresponding key. A "nothing found" message is shown if the search string doesn't match anything. A search for an empty string "" returns a paginated list of all translations. Closes #1
1 parent dbfcf45 commit 6327a8a

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

api.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Kirby\Filesystem\F;
66
use Kirby\Http\Response;
77
use Kirby\Toolkit\Collection;
8+
use Kirby\Toolkit\Str;
89

910
$contentRoot = kirby()->root('content');
1011
$path = $contentRoot.DS.'localizer';
@@ -66,8 +67,18 @@
6667
'action' => function ($code) {
6768
$page = get('page', 1);
6869
$limit = get('limit', 10);
70+
$q = trim(get('q'));
6971
$translations = kirby()->translations()->find($code ?? 'en');
70-
$collection = new Collection($translations->data());
72+
73+
if (!empty($q)) {
74+
$filteredTranslations = array_filter(
75+
$translations->data(),
76+
fn($v, $k) => Str::contains($k, $q) || Str::contains($v, $q),
77+
ARRAY_FILTER_USE_BOTH
78+
);
79+
}
80+
81+
$collection = new Collection($filteredTranslations ?? $translations->data());
7182

7283
$pagedCollection = $collection->paginate([
7384
'page' => $page,

index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ an [issue](https://github.com/gearsdigital/localizer-for-kirby/issues/new).
77

88
### Known issues
99

10-
- No search functionality
11-
- No translations (I know, I know ¯\_(ツ)_/¯)
12-
- No tests
10+
- [x] Add search functionality
11+
- [ ] Add translations (I know, I know ¯\_(ツ)_/¯)
12+
- [ ] Add tests
1313

1414
## What is this about?
1515

@@ -19,14 +19,18 @@ help you to make that possible.
1919
## Screenshots
2020

2121
### Translation dialog
22+
2223
<img width="977" alt="Bildschirmfoto 2021-11-27 um 17 03 47" src="https://user-images.githubusercontent.com/965069/143829633-11284c55-e8ae-4f0c-b074-1f1d08dfead2.png">
2324

2425
### Unsaved changes
26+
2527
<img width="977" alt="Bildschirmfoto 2021-11-27 um 17 04 05" src="https://user-images.githubusercontent.com/965069/143829640-cfeab1b9-ad58-4e48-9da1-974a6439ebfe.png">
2628

2729
### Confirm dialog
30+
2831
<img width="977" alt="Bildschirmfoto 2021-11-27 um 17 04 18" src="https://user-images.githubusercontent.com/965069/143829715-e2ee5a1b-cce1-431e-bc48-9e61a58b9170.png">
2932

3033
### Applied changes
34+
3135
<img width="977" alt="Bildschirmfoto 2021-11-27 um 17 04 49" src="https://user-images.githubusercontent.com/965069/143829776-717aa93f-4286-4b7a-a82c-99c2b4a4c67d.png">
3236

src/components/LocalizerTranslations.vue

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
<template>
2-
<k-loader v-if="!pagedTranslationDataList.length" />
3-
<article v-else>
4-
<k-items class="k-panel-localizer-translations">
2+
<div>
3+
<k-input
4+
v-model="q"
5+
:icon="isLoading ? 'loader' : 'search'"
6+
:placeholder="$t('search') + ' …'"
7+
autofocus="true"
8+
class="k-dialog-search"
9+
type="text" />
10+
11+
<k-items v-if="pagedTranslationDataList.length > 0" class="k-panel-localizer-translations">
512
<k-item
613
v-for="entry in pagedTranslationDataList"
714
:key="entry.key"
@@ -11,14 +18,17 @@
1118
layout="cards"
1219
@click="$emit('select', entry)" />
1320
</k-items>
21+
22+
<k-empty v-else>Nothing found</k-empty>
23+
1424
<k-pagination
1525
ref="pagination"
1626
align="center"
1727
:details="true"
1828
:limit="pagination.limit"
1929
:total="pagination.total"
2030
@paginate="updatePagination" />
21-
</article>
31+
</div>
2232
</template>
2333

2434
<script>
@@ -38,6 +48,7 @@ export default {
3848
total: 0,
3949
page: 1,
4050
},
51+
q: null,
4152
};
4253
},
4354
computed: {
@@ -54,6 +65,19 @@ export default {
5465
selectedTranslations() {
5566
return this.$store.getters.getTranslationsForLanguage(this.code);
5667
},
68+
isLoading() {
69+
return this.$store.state.isLoading;
70+
},
71+
},
72+
watch: {
73+
q: function (val, oldVal) {
74+
if (val !== oldVal) {
75+
this.fetchTranslations();
76+
}
77+
},
78+
},
79+
created() {
80+
this.fetchTranslations = this.$helper.debounce(this.fetchTranslations, 250);
5781
},
5882
mounted() {
5983
this.fetchTranslations();
@@ -62,8 +86,9 @@ export default {
6286
async fetchTranslations(page = 1) {
6387
const { data, pagination } = await this.$api.get(`localizer/translations/${this.code}`, {
6488
page,
89+
q: this.q,
6590
});
66-
this.translations = data;
91+
this.translations = data ?? [];
6792
this.pagination = pagination;
6893
},
6994
updatePagination(pagination) {

0 commit comments

Comments
 (0)