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

AUT-3764: make translation with new prefix #1119

Closed
wants to merge 2 commits into from
Closed
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
63 changes: 61 additions & 2 deletions common/oatbox/user/UserLanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2018-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2018-2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*/

namespace oat\oatbox\user;

use oat\oatbox\service\ConfigurableService;
use oat\generis\model\GenerisRdf;
use oat\tao\helpers\Layout;

class UserLanguageService extends ConfigurableService implements UserLanguageServiceInterface
{
public const OPTION_LOCK_DATA_LANGUAGE = 'lock_data_language';
public const OPTION_AUTHORING_LANGUAGE = 'authoring_language';
public const OPTION_INTERFACE_LANGUAGE = 'interface_language';

public const LANG_PREFIX = '-S';

/** @var ?string */
private $customInterfaceLanguage;

Expand Down Expand Up @@ -64,8 +67,15 @@ public function getInterfaceLanguage(User $user)
}

$lang = $user->getPropertyValues(GenerisRdf::PROPERTY_USER_UILG);
if (empty($lang)) {
$lang = $this->getOption(self::OPTION_INTERFACE_LANGUAGE, DEFAULT_LANG);
} else {
$lang = (string)current($lang);
}

$lang = $this->checkPrefix($lang);

return empty($lang) ? $this->getOption(self::OPTION_INTERFACE_LANGUAGE, DEFAULT_LANG) : (string)current($lang);
return $lang;
}

/**
Expand All @@ -89,4 +99,53 @@ public function setCustomInterfaceLanguage(?string $customInterfaceLanguage): vo
{
$this->customInterfaceLanguage = $customInterfaceLanguage;
}

/**
* Short description of method notContainPrefix
*
* @param string $language
* @access private
* @author Sultan Sagi, <sultan.sagiyev@taotesting.com>
* @return bool
*/
private function notContainPrefix($language)
{
$pattern = '/' . self::LANG_PREFIX . '$/';

return Layout::isSolarDesignEnabled() && !preg_match($pattern, $language, $matches);
}

/**
* Short description of method addPrefix
*
* @param string $language
* @access private
* @author Sultan Sagi, <sultan.sagiyev@taotesting.com>
* @return string
*/
private function addPrefix($language)
{
return $language . self::LANG_PREFIX;
}

/**
* Short description of method addPrefix
*
* @param string $language
* @access private
* @author Sultan Sagi, <sultan.sagiyev@taotesting.com>
* @return string
*/
private function checkPrefix($language)
{
if ($this->notContainPrefix($language)) {
$localesDir = 'tao/views/locales';
$dir = dirname(__FILE__) . '/../../../../' . $localesDir . '/' . $this->addPrefix($language);
if (is_dir($dir)) {
$language = $this->addPrefix($language);
}
}

return $language;
}
}
6 changes: 6 additions & 0 deletions test/unit/oatbox/user/UserLanguageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use oat\oatbox\user\User;
use oat\generis\model\GenerisRdf;
use oat\generis\test\TestCase;
use oat\tao\helpers\Layout;

/**
* class UserLanguageServiceTest
Expand All @@ -51,6 +52,11 @@ public function testGetDefaultLanguage(): void
*/
public function testGetInterfaceLanguage(string $expected, User $user, array $serviceParams): void
{
$layout = $this->createMock(Layout::class);

$layout->method('isSolarDesignEnabled')
->willReturn(false);

$service = $this->getService($serviceParams);

$this->assertEquals($expected, $service->getInterfaceLanguage($user));
Expand Down
Loading