-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLanguageProvider.php
More file actions
75 lines (58 loc) · 2.34 KB
/
LanguageProvider.php
File metadata and controls
75 lines (58 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Ergonode\IntegrationShopware\Provider;
use RuntimeException;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\Language\LanguageEntity;
class LanguageProvider
{
private EntityRepository $languageRepository;
public function __construct(EntityRepository $languageRepository)
{
$this->languageRepository = $languageRepository;
}
/**
* @return string Default language locale in Shopware format (ex. en-GB)
*/
public function getDefaultLanguageLocale(Context $context): string
{
$criteria = new Criteria([Defaults::LANGUAGE_SYSTEM]);
$criteria->addAssociation('locale');
$languageEntity = $this->languageRepository->search($criteria, $context)->first();
if (!$languageEntity instanceof LanguageEntity) {
throw new RuntimeException('Could not load default system language entity');
}
return $languageEntity->getLocale()->getCode();
}
public function getLocaleCodeByContext(Context $context): ?string
{
$criteria = new Criteria([$context->getLanguageId()]);
$criteria->addAssociation('locale');
$language = $this->languageRepository->search($criteria, $context)->first();
if ($language instanceof LanguageEntity) {
$locale = $language->getLocale();
if (null !== $locale) {
return $locale->getCode();
}
}
return null;
}
public function getActiveLocaleCodes(Context $context): array
{
$criteria = new Criteria();
$criteria->addAssociation('locale');
$criteria->addAssociation('swagLanguagePackLanguage'); // devEcommerce change
$criteria->addFilter(new EqualsFilter('swagLanguagePackLanguage.salesChannelActive', true)); // devEcommerce change
/** @var LanguageEntity[] $languages */
$languages = $this->languageRepository->search($criteria, $context);
$result = [];
foreach ($languages as $language) {
$result[] = $language->getLocale()->getCode();
}
return $result;
}
}