Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace deprecated translation methods #6567

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 All @@ -52,17 +50,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 @@ -27,7 +27,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 @@ -188,7 +191,7 @@ export default {
STATUS_UNKNOWN,

showTaskList: false,
canTranslate: loadState('text', 'translation_languages', []).length > 0,
canTranslate: loadState('text', 'translation_languages', []).from?.length > 0,
}
},
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 @@ -130,7 +130,7 @@ export default {
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,
resize: null,
}
},
Expand Down
68 changes: 36 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 { generateOcsUrl } from '@nextcloud/router'
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 @@ export default {
return {
input: 'Hallo welt. Das ist ein Test.',
result: '',
fromLanguage: loadState('text', 'translation_can_detect', false) === true ? detectLanguageEntry : null,
fromLanguage: null,
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 }),
})
}
return result
},
Expand All @@ -138,22 +128,18 @@ export default {
return []
}

const languages = this.languages.filter(l => {
const languages = this.languages.to.filter(l => {
if (this.fromLanguage.id === null) {
return true
}
return l.from === this.fromLanguage.id
return l.value !== this.fromLanguage.id
})
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 }),
})
}
return result
},
Expand All @@ -167,6 +153,11 @@ export default {
this.error = null
this.autosize()
},
fromLanguage(newVal) {
if (newVal.id === this.toLanguage.id) {
this.toLanguage = null
}
},
toLanguage() {
this.result = ''
this.error = null
Expand All @@ -179,12 +170,25 @@ export default {
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',
})
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)
} catch (e) {
console.error('Failed to translate', e)
this.error = t('text', 'Translation failed')
Expand Down
Loading