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(language): return fallback translations even if language argument is specifically provided, and language is not supported #335

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
7 changes: 7 additions & 0 deletions src/Language/Contract/LanguageServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ interface LanguageServiceInterface
*/
public function getIso2(?string $language = null): string;

/**
* Given an ISO-639-1 language code, returns the language code back if supported or a fallback language if not.
* @param string $isoCode
* @return string
*/
public function getLanguageWithFallback(string $isoCode): string;

/**
* Returns the IETF language tag of the current language.
*
Expand Down
22 changes: 16 additions & 6 deletions src/Language/Service/AbstractLanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(LanguageRepository $repository, FileSystemInterface
abstract protected function getFilePath(?string $language = null): string;

/**
* @param null|string $language
* @param null|string $language The language code in IETF format (eg. en, en-US)
*
* @return string
*/
Expand All @@ -50,20 +50,30 @@ public function getIso2(?string $language = null): string
return $this->getIsoFromIetf($language);
}

$iso = $this->getIsoFromIetf($this->getLanguage());
return $this->getLanguageWithFallback(
$this->getIsoFromIetf($this->getLanguage())
);

}

return in_array($iso, Pdk::get('availableLanguages'), true) ? $iso : Pdk::get('defaultLanguage');
/**
* @param string $isoCode
* @return string
*/
public function getLanguageWithFallback(string $isoCode): string
{
return in_array($isoCode, Pdk::get('availableLanguages'), true) ? $isoCode : Pdk::get('defaultLanguage');
}

/**
* @return array<string, string>
*/
public function getTranslations(?string $language = null): array
{
$iso2 = $this->getIso2($language);
$lang = $this->getLanguageWithFallback($this->getIso2($language));

return $this->repository->getTranslations($iso2, function () use ($iso2) {
return json_decode($this->fileSystem->get($this->getFilePath($iso2)), true);
return $this->repository->getTranslations($lang, function () use ($lang) {
return json_decode($this->fileSystem->get($this->getFilePath($lang)), true);
});
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Bootstrap/MockLanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function getIso2(?string $language = null): string
return substr($language ?? $this->getLanguage(), 0, 2);
}

/**
* @param string $isoCode
* @return string
*/
public function getLanguageWithFallback(string $isoCode): string
{
return $isoCode;
}

/**
* @return string
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Language/Service/AbstractLanguageServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/** @noinspection PhpUndefinedMethodInspection,PhpUnhandledExceptionInspection,StaticClosureCanBeUsedInspection */

declare(strict_types=1);
Expand All @@ -10,6 +11,7 @@
use MyParcelNL\Pdk\Language\Contract\LanguageServiceInterface;
use MyParcelNL\Pdk\Tests\Bootstrap\MockAbstractLanguageService;
use MyParcelNL\Pdk\Tests\Uses\UsesEachMockPdkInstance;

use function DI\get;
use function MyParcelNL\Pdk\Tests\usesShared;

Expand All @@ -34,6 +36,12 @@
expect(Language::getTranslations($language))->toBeArray();
})->with('languages');


it('loads fallback translations from file if language is not supported', function () {
expect(Language::getTranslations('tr-TR'))->toBeArray(['send_help' => 'Send help']);
});


it('translates strings in current language', function (?string $language, string $translation) {
if ($language) {
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockAbstractLanguageService $languageService */
Expand Down