Skip to content
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
11 changes: 3 additions & 8 deletions lib/OldProcessing/Translation/TranslationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCA\OpenAi\Translation\LanguageList;
use OCP\IAppConfig;
use OCP\ICacheFactory;
use OCP\L10N\IFactory;
Expand Down Expand Up @@ -46,8 +47,7 @@ public function getAvailableLanguages(): array {
}, $cached);
}

$coreL = $this->l10nFactory->getLanguages();
$languages = array_merge($coreL['commonLanguages'], $coreL['otherLanguages']);
$languages = LanguageList::getLanguages();

$availableLanguages = [];
foreach ($languages as $sourceLanguage) {
Expand Down Expand Up @@ -90,12 +90,7 @@ public function detectLanguage(string $text): ?string {
}

private function getCoreLanguagesByCode(): array {
$coreL = $this->l10nFactory->getLanguages();
$coreLanguages = array_reduce(array_merge($coreL['commonLanguages'], $coreL['otherLanguages']), function ($carry, $val) {
$carry[$val['code']] = $val['name'];
return $carry;
});
return $coreLanguages;
return LanguageList::getLanguagesByCode();
}

public function translate(?string $fromLanguage, string $toLanguage, string $text): string {
Expand Down
11 changes: 3 additions & 8 deletions lib/TaskProcessing/TranslateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\OpenAi\Service\ChunkService;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCA\OpenAi\Translation\LanguageList;
use OCP\IAppConfig;
use OCP\ICacheFactory;
use OCP\IL10N;
Expand Down Expand Up @@ -82,8 +83,7 @@ public function getExpectedRuntime(): int {
}

public function getInputShapeEnumValues(): array {
$coreL = $this->l10nFactory->getLanguages();
$languages = array_merge($coreL['commonLanguages'], $coreL['otherLanguages']);
$languages = LanguageList::getLanguages();
$languageEnumValues = array_map(static function (array $language) {
return new ShapeEnumValue($language['name'], $language['code']);
}, $languages);
Expand Down Expand Up @@ -144,12 +144,7 @@ public function getOptionalOutputShapeEnumValues(): array {
}

private function getCoreLanguagesByCode(): array {
$coreL = $this->l10nFactory->getLanguages();
$coreLanguages = array_reduce(array_merge($coreL['commonLanguages'], $coreL['otherLanguages']), function ($carry, $val) {
$carry[$val['code']] = $val['name'];
return $carry;
});
return $coreLanguages;
return LanguageList::getLanguagesByCode();
}

public function process(?string $userId, array $input, callable $reportProgress): array {
Expand Down
127 changes: 127 additions & 0 deletions lib/Translation/LanguageList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenAi\Translation;

/**
* Static list of languages supported by AI translation providers.
*
* AI models can translate between any language pair regardless of which
* UI translations are installed on the server. Using l10nFactory->getLanguages()
* breaks when force_language is set (returns only the forced language).
*
* @see https://github.com/nextcloud/integration_openai/issues/357
*/
class LanguageList {

/**
* Returns a comprehensive list of common languages with English display names.
*
* @return array<array{code: string, name: string}>
*/
public static function getLanguages(): array {
return [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The languages should be internationalized. I would just use the endonyms (https://en.wikipedia.org/wiki/Endonym_and_exonym) for them. Also could you merge it with the list in https://github.com/nextcloud/integration_openai/blob/main/lib/AppInfo/Application.php#L83 and use your getLanguages for where that constant is being used so there is only one list.

['code' => 'af', 'name' => 'Afrikaans'],
['code' => 'sq', 'name' => 'Albanian'],
['code' => 'am', 'name' => 'Amharic'],
['code' => 'ar', 'name' => 'Arabic'],
['code' => 'hy', 'name' => 'Armenian'],
['code' => 'az', 'name' => 'Azerbaijani'],
['code' => 'eu', 'name' => 'Basque'],
['code' => 'be', 'name' => 'Belarusian'],
['code' => 'bn', 'name' => 'Bengali'],
['code' => 'bs', 'name' => 'Bosnian'],
['code' => 'bg', 'name' => 'Bulgarian'],
['code' => 'my', 'name' => 'Burmese'],
['code' => 'ca', 'name' => 'Catalan'],
['code' => 'zh', 'name' => 'Chinese (Simplified)'],
['code' => 'zh_TW', 'name' => 'Chinese (Traditional)'],
['code' => 'hr', 'name' => 'Croatian'],
['code' => 'cs', 'name' => 'Czech'],
['code' => 'da', 'name' => 'Danish'],
['code' => 'nl', 'name' => 'Dutch'],
['code' => 'en', 'name' => 'English'],
['code' => 'et', 'name' => 'Estonian'],
['code' => 'fi', 'name' => 'Finnish'],
['code' => 'fr', 'name' => 'French'],
['code' => 'gl', 'name' => 'Galician'],
['code' => 'ka', 'name' => 'Georgian'],
['code' => 'de', 'name' => 'German'],
['code' => 'el', 'name' => 'Greek'],
['code' => 'gu', 'name' => 'Gujarati'],
['code' => 'ha', 'name' => 'Hausa'],
['code' => 'he', 'name' => 'Hebrew'],
['code' => 'hi', 'name' => 'Hindi'],
['code' => 'hu', 'name' => 'Hungarian'],
['code' => 'is', 'name' => 'Icelandic'],
['code' => 'id', 'name' => 'Indonesian'],
['code' => 'ga', 'name' => 'Irish'],
['code' => 'it', 'name' => 'Italian'],
['code' => 'ja', 'name' => 'Japanese'],
['code' => 'kn', 'name' => 'Kannada'],
['code' => 'kk', 'name' => 'Kazakh'],
['code' => 'km', 'name' => 'Khmer'],
['code' => 'ko', 'name' => 'Korean'],
['code' => 'ku', 'name' => 'Kurdish'],
['code' => 'ky', 'name' => 'Kyrgyz'],
['code' => 'lo', 'name' => 'Lao'],
['code' => 'lv', 'name' => 'Latvian'],
['code' => 'lt', 'name' => 'Lithuanian'],
['code' => 'mk', 'name' => 'Macedonian'],
['code' => 'ms', 'name' => 'Malay'],
['code' => 'ml', 'name' => 'Malayalam'],
['code' => 'mt', 'name' => 'Maltese'],
['code' => 'mn', 'name' => 'Mongolian'],
['code' => 'ne', 'name' => 'Nepali'],
['code' => 'no', 'name' => 'Norwegian'],
['code' => 'ps', 'name' => 'Pashto'],
['code' => 'fa', 'name' => 'Persian'],
['code' => 'pl', 'name' => 'Polish'],
['code' => 'pt', 'name' => 'Portuguese'],
['code' => 'pt_BR', 'name' => 'Portuguese (Brazil)'],
['code' => 'pa', 'name' => 'Punjabi'],
['code' => 'ro', 'name' => 'Romanian'],
['code' => 'ru', 'name' => 'Russian'],
['code' => 'sr', 'name' => 'Serbian'],
['code' => 'si', 'name' => 'Sinhala'],
['code' => 'sk', 'name' => 'Slovak'],
['code' => 'sl', 'name' => 'Slovenian'],
['code' => 'so', 'name' => 'Somali'],
['code' => 'es', 'name' => 'Spanish'],
['code' => 'sw', 'name' => 'Swahili'],
['code' => 'sv', 'name' => 'Swedish'],
['code' => 'tl', 'name' => 'Tagalog'],
['code' => 'ta', 'name' => 'Tamil'],
['code' => 'te', 'name' => 'Telugu'],
['code' => 'th', 'name' => 'Thai'],
['code' => 'tr', 'name' => 'Turkish'],
['code' => 'tk', 'name' => 'Turkmen'],
['code' => 'uk', 'name' => 'Ukrainian'],
['code' => 'ur', 'name' => 'Urdu'],
['code' => 'uz', 'name' => 'Uzbek'],
['code' => 'vi', 'name' => 'Vietnamese'],
['code' => 'cy', 'name' => 'Welsh'],
['code' => 'yo', 'name' => 'Yoruba'],
['code' => 'zu', 'name' => 'Zulu'],
];
}

/**
* Returns a code => name map of all supported languages.
*
* @return array<string, string>
*/
public static function getLanguagesByCode(): array {
$map = [];
foreach (self::getLanguages() as $lang) {
$map[$lang['code']] = $lang['name'];
}
return $map;
}
}
Loading