This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Model\StoreView;
class LanguageGroup
{
/**
* @param int[] $storeIds
* @param string[] $storeCodes
*/
public function __construct(
private readonly string $label,
private readonly string $targetLocale,
private readonly array $storeIds,
private readonly array $storeCodes
) {
}
public function getLabel(): string
{
return $this->label;
}
public function getTargetLocale(): string
{
return $this->targetLocale;
}
/**
* @return int[]
*/
public function getStoreIds(): array
{
return $this->storeIds;
}
/**
* @return string[]
*/
public function getStoreCodes(): array
{
return $this->storeCodes;
}
public function toArray(): array
{
return [
'label' => $this->label,
'target_locale' => $this->targetLocale,
'store_ids' => $this->storeIds,
'store_codes' => $this->storeCodes,
];
}
}
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Model\StoreView;
use Magento\Store\Model\StoreManagerInterface;
class LanguageGroupProvider
{
public function __construct(
private readonly StoreManagerInterface $storeManager
) {
}
/**
* @return LanguageGroup[]
*/
public function getGroups(): array
{
$groups = [];
foreach ($this->storeManager->getStores(false) as $store) {
if (!$store->isActive()) {
continue;
}
$languageCode = $this->getLanguageCode((string)$store->getCode());
if ($languageCode === '') {
continue;
}
$groups[$languageCode]['label'] = $this->getLanguageLabel($languageCode);
$groups[$languageCode]['store_ids'][] = (int)$store->getId();
$groups[$languageCode]['store_codes'][] = (string)$store->getCode();
}
ksort($groups);
return array_map(
static fn (array $group): LanguageGroup => new LanguageGroup(
$group['label'],
$group['target_locale'],
$group['store_ids'],
$group['store_codes']
),
array_map(
static fn (array $group, string $languageCode): array => $group + ['target_locale' => $languageCode],
$groups,
array_keys($groups)
)
);
}
public function getGroup(string $label): ?LanguageGroup
{
foreach ($this->getGroups() as $group) {
if ($group->getLabel() === $label || $group->getTargetLocale() === $label) {
return $group;
}
}
return null;
}
private function getLanguageCode(string $storeCode): string
{
if (!preg_match('/^([a-z]{2,3})(?:[_-]|$)/i', $storeCode, $matches)) {
return '';
}
return strtolower($matches[1]);
}
private function getLanguageLabel(string $languageCode): string
{
if (class_exists(\Locale::class)) {
$label = \Locale::getDisplayLanguage($languageCode, 'en');
if (is_string($label) && $label !== '') {
return ucfirst($label);
}
}
return strtoupper($languageCode);
}
}