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

Use supported languages #10

Draft
wants to merge 1 commit into
base: develop-v4
Choose a base branch
from
Draft
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
38 changes: 19 additions & 19 deletions src/services/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use craft\base\Component;
use craft\helpers\App;
use DeepL\DeepLException;
use DeepL\Translator;
use statikbe\deepl\Deepl;

Expand All @@ -15,7 +16,7 @@ public function init(): void
{
$authKey = App::parseEnv(Deepl::getInstance()->getSettings()->apiKey);
if (!$authKey) {
// Expand this to show a message and/or throw an exception if appropriate.
throw new DeepLException('authKey must be a non-empty string');
return;
}
$this->translator = new Translator($authKey);
Expand All @@ -33,30 +34,29 @@ public function translateString($text, $sourceLang, $targetLang)
return $translation->text;
}

public function getTargetLanguages()
{
return $this->translator->getTargetLanguages();
}

public function getSourceLanguages()
{
return $this->translator->getSourceLanguages();
}

public function getLanguageString($string, bool $isTarget = true): string
{
$str = explode('-', $string);
$lang = $str[0];
if ($isTarget) {
$supportedLanguages = $this->translator->getTargetLanguages();
} else {
$supportedLanguages = $this->translator->getSourceLanguages();
}

// TODO: Better handling for support languages
if ($lang === 'en') {
if ($isTarget) {
return 'en-GB';
foreach ($supportedLanguages as $language) {
if (str_contains($language->code, strtoupper($string))) {
$lang = $language->code;
break;
} else {
return "EN";
$str = explode('-', $string);
$shortLang = $str[0];

if (str_contains(strtolower($language->code), $shortLang)) {
$lang = $language->code;
break;
}
}
}

return strtoupper($lang);
return $lang ?? $string;
}
}