Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TranslationSearch.vue « TranslationSearch « src « vue « LanguagesManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c84c02df0224e7ddb87aa507a41fa4a19729dce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!--
  Matomo - free/libre analytics platform
  @link https://matomo.org
  @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
-->

<template>
  <div>
    <p>
      This page helps you to find existing translations that you can reuse in your Plugin.
      If you want to know more about translations have a look at our
      <a
        href="https://developer.matomo.org/guides/internationalization"
        rel="noreferrer noopener"
        target="_blank"
      >Internationalization guide</a>.
      Enter a search term to find translations and their corresponding keys:
    </p>
    <div>
      <Field
        uicontrol="text"
        name="alias"
        inline-help="Search for English translation. Max 1000 results will be shown."
        placeholder="Search for English translation"
        v-model="searchTerm"
      >
      </Field>
    </div>
    <div>
      <Field
        uicontrol="select"
        name="translationSearch.compareLanguage"
        inline-help="Optionally select a language to compare the English language with."
        :model-value="compareLanguage"
        @update:model-value="compareLanguage = $event; doCompareLanguage()"
        :options="languages"
      >
      </Field>
    </div>
    <br />
    <br />
    <table
      style="word-break: break-all;"
      v-show="searchTerm"
      v-content-table
    >
      <thead>
        <tr>
          <th style="width:250px;">Key</th>
          <th>English translation</th>
          <th v-show="compareLanguage && compareTranslations">Compare translation</th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="translation in filteredTranslations"
          :key="translation.label"
        >
          <td>{{ translation.label }}</td>
          <td>{{ translation.value }}</td>
          <td v-if="compareLanguage && compareTranslations">
            {{ compareTranslations[translation.label] }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { AjaxHelper, ContentTable, useExternalPluginComponent } from 'CoreHome';

interface Option {
  key: string;
  value: string;
}

interface Translation {
  label: string;
  value: string;
}

interface Language {
  code: string;
  name: string;
}

interface TranslationSearchState {
  compareTranslations: Record<string, string>|null;
  existingTranslations: Translation[];
  languages: Option[];
  compareLanguage: string;
  searchTerm: string;
}

// loading a component this way since during Installation we don't want to load CorePluginsAdmin
// just for the language selector directive
const Field = useExternalPluginComponent('CorePluginsAdmin', 'Field');

export default defineComponent({
  components: {
    Field,
  },
  directives: {
    ContentTable,
  },
  data(): TranslationSearchState {
    return {
      compareTranslations: null,
      existingTranslations: [],
      languages: [],
      compareLanguage: '',
      searchTerm: '',
    };
  },
  created() {
    this.fetchTranslations('en');
    this.fetchLanguages();
  },
  methods: {
    fetchTranslations(languageCode: string) {
      AjaxHelper.fetch<Translation[]>({
        method: 'LanguagesManager.getTranslationsForLanguage',
        filter_limit: -1,
        languageCode,
      }).then((response) => {
        if (!response) {
          return;
        }

        if (languageCode === 'en') {
          this.existingTranslations = response;
        } else {
          this.compareTranslations = {};
          response.forEach((translation) => {
            this.compareTranslations![translation.label] = translation.value;
          });
        }
      });
    },
    fetchLanguages() {
      AjaxHelper.fetch<Language[]>({
        method: 'LanguagesManager.getAvailableLanguagesInfo',
        filter_limit: -1,
      }).then((languages) => {
        this.languages = [{
          key: '',
          value: 'None',
        }];

        if (languages) {
          languages.forEach((language) => {
            if (language.code === 'en') {
              return;
            }

            this.languages.push({
              key: language.code,
              value: language.name,
            });
          });
        }
      });
    },
    doCompareLanguage() {
      if (this.compareLanguage) {
        this.compareTranslations = null;
        this.fetchTranslations(this.compareLanguage);
      }
    },
  },
  computed: {
    filteredTranslations(): Translation[] {
      let filtered = this.existingTranslations.filter(
        (t) => t.label.includes(this.searchTerm) || t.value.includes(this.searchTerm),
      );
      filtered = filtered.slice(0, 1000);
      return filtered;
    },
  },
});
</script>