Skip to content

Commit

Permalink
fix: replace deprecated translation methods
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Oct 28, 2024
1 parent d583122 commit e879703
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
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
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
65 changes: 37 additions & 28 deletions src/components/Modal/Translate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { NcModal, NcButton, NcSelect, NcLoadingIcon, NcTextArea } from '@nextclo
import { useIsMobileMixin } from '../Editor.provider.js'

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

Expand Down Expand Up @@ -104,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: detectLanguageEntry,
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 = [detectLanguageEntry]
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 @@ -133,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 @@ -162,6 +153,11 @@ export default {
this.error = null
this.autosize()
},
fromLanguage(newVal) {
if (newVal.id === this.toLanguage.id) {
this.toLanguage = null
}
},
toLanguage() {
this.result = null
this.error = null
Expand All @@ -174,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

0 comments on commit e879703

Please sign in to comment.