Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/Service/InitialStateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use OCP\AppFramework\Services\IInitialState;
use OCP\TaskProcessing\IManager;
use OCP\Translation\ITranslationManager;

class InitialStateProvider {
private const ASSISTANT_TASK_TYPES = [
Expand All @@ -25,7 +24,6 @@ class InitialStateProvider {
public function __construct(
private IInitialState $initialState,
private ConfigService $configService,
private ITranslationManager $translationManager,
private IManager $taskProcessingManager,
private ?string $userId,
) {
Expand Down Expand Up @@ -57,17 +55,19 @@ public function provideState(): void {
$this->configService->isRichEditingEnabled()
);

$this->initialState->provideInitialState(
'translation_can_detect',
$this->translationManager->canDetectLanguage()
);
$taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
$fromLanguages = $taskTypes['core:text2text:translate']['inputShapeEnumValues']['origin_language'] ?? [];
$toLanguages = $taskTypes['core:text2text:translate']['inputShapeEnumValues']['target_language'] ?? [];

$this->initialState->provideInitialState(
'translation_languages',
$this->translationManager->getLanguages()
[
'from' => $fromLanguages,
'to' => $toLanguages,
]
);

$filteredTypes = array_filter($this->taskProcessingManager->getAvailableTaskTypes(), static function (string $taskType) {
$filteredTypes = array_filter($taskTypes, static function (string $taskType) {
return in_array($taskType, self::ASSISTANT_TASK_TYPES, true);
}, ARRAY_FILTER_USE_KEY);
$this->initialState->provideInitialState(
Expand Down
7 changes: 5 additions & 2 deletions src/components/Assistant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
</template>
{{ type.name }}
</NcActionButton>
<NcActionButton data-cy="open-translate" close-after-click @click="openTranslateDialog">
<NcActionButton v-if="canTranslate"
data-cy="open-translate"
close-after-click
@click="openTranslateDialog">
<template #icon>
<TranslateVariant :size="20" />
</template>
Expand Down Expand Up @@ -189,7 +192,7 @@
STATUS_UNKNOWN,

showTaskList: false,
canTranslate: loadState('text', 'translation_languages', []).length > 0,
canTranslate: loadState('text', 'translation_languages', []).from?.length > 0,

Check warning on line 195 in src/components/Assistant.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Assistant.vue#L195

Added line #L195 was not covered by tests
}
},
computed: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Menu/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
randomID: `menu-bar-${(Math.ceil((Math.random() * 10000) + 500)).toString(16)}`,
displayHelp: false,
isReady: false,
canTranslate: loadState('text', 'translation_languages', []).length > 0,
canTranslate: loadState('text', 'translation_languages', []).from?.length > 0,

Check warning on line 137 in src/components/Menu/MenuBar.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Menu/MenuBar.vue#L137

Added line #L137 was not covered by tests
resize: null,
}
},
Expand Down
70 changes: 38 additions & 32 deletions src/components/Modal/Translate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@
import { NcModal, NcButton, NcSelect, NcLoadingIcon, NcTextArea } from '@nextcloud/vue'
import { useIsMobileMixin } from '../Editor.provider.js'

const detectLanguageEntry = {
id: null,
label: t('text', 'Detect language'),
}

export default {
name: 'Translate',
components: {
Expand All @@ -109,27 +104,22 @@
return {
input: 'Hallo welt. Das ist ein Test.',
result: '',
fromLanguage: loadState('text', 'translation_can_detect', false) === true ? detectLanguageEntry : null,
fromLanguage: null,

Check warning on line 107 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L107

Added line #L107 was not covered by tests
toLanguage: null,
languages: loadState('text', 'translation_languages', []),
canDetect: loadState('text', 'translation_can_detect'),
loading: false,
error: null,
disableFromLanguageSelect: true,
}
},
computed: {
fromLanguages() {
const result = this.canDetect ? [detectLanguageEntry] : []
const set = new Set()
for (const item of this.languages) {
if (!set.has(item.from)) {
set.add(item.from)
result.push({
id: item.from,
label: !this.$isMobile ? item.fromLabel : t('text', 'Translate from {language}', { language: item.fromLabel }),
})
}
const result = []
for (const item of this.languages.from) {
result.push({
id: item.value,
label: !this.$isMobile ? item.name : t('text', 'Translate from {language}', { language: item.name }),
})

Check warning on line 122 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L117-L122

Added lines #L117 - L122 were not covered by tests
}
return result
},
Expand All @@ -138,22 +128,18 @@
return []
}

const languages = this.languages.filter(l => {
const languages = this.languages.to.filter(l => {

Check warning on line 131 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L131

Added line #L131 was not covered by tests
if (this.fromLanguage.id === null) {
return true
}
return l.from === this.fromLanguage.id
return l.value !== this.fromLanguage.id

Check warning on line 135 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L135

Added line #L135 was not covered by tests
})
const result = []
const set = new Set()
for (const item of languages) {
if (!set.has(item.to)) {
set.add(item.to)
result.push({
id: item.to,
label: !this.$isMobile ? item.toLabel : t('text', 'Translate to {language}', { language: item.toLabel }),
})
}
result.push({
id: item.value,
label: !this.$isMobile ? item.name : t('text', 'Translate to {language}', { language: item.name }),
})

Check warning on line 142 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L139-L142

Added lines #L139 - L142 were not covered by tests
}
return result
},
Expand All @@ -167,6 +153,13 @@
this.error = null
this.autosize()
},
fromLanguage(newVal) {
if (newVal?.id === this.toLanguage?.id) {
this.toLanguage = null
}
this.result = ''
this.error = null
},

Check warning on line 162 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L156-L162

Added lines #L156 - L162 were not covered by tests
toLanguage() {
this.result = ''
this.error = null
Expand All @@ -179,12 +172,25 @@
async translate() {
this.loading = true
try {
const result = await axios.post(generateOcsUrl('translation/translate'), {
text: this.input,
fromLanguage: this.fromLanguage?.id ?? null,
toLanguage: this.toLanguage.id,
const scheduleResponse = await axios.post(generateOcsUrl('taskprocessing/schedule'), {
input: {
origin_language: this.fromLanguage?.id ?? null,
input: this.input,
target_language: this.toLanguage.id,
},
type: 'core:text2text:translate',
appId: 'text',

Check warning on line 182 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L175-L182

Added lines #L175 - L182 were not covered by tests
})
this.result = result.data.ocs.data.text
const task = scheduleResponse.data.ocs.data.task
const getTaskOutput = async (task) => {
if (task.output) {
return task.output.output
}
await new Promise(resolve => setTimeout(resolve, 2000))
const taskResponse = await axios.get(generateOcsUrl(`taskprocessing/task/${task.id}`))
return getTaskOutput(taskResponse.data.ocs.data.task)
}
this.result = await getTaskOutput(task)

Check warning on line 193 in src/components/Modal/Translate.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Modal/Translate.vue#L184-L193

Added lines #L184 - L193 were not covered by tests
} catch (e) {
console.error('Failed to translate', e)
this.error = t('text', 'Translation failed')
Expand Down
Loading