Skip to content

Commit 06fedfd

Browse files
kingjia90Copilot
andauthored
[Bug]: Pimcore Core requirements to Admin (pimcore#19010)
* Pimcore\Tool\Admin::GetLanguages independent from admin ui * Update lib/Tool/Admin.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update lib/Tool/Admin.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0779434 commit 06fedfd

1 file changed

Lines changed: 63 additions & 25 deletions

File tree

lib/Tool/Admin.php

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,77 @@ class Admin
3535
*/
3636
public static function getLanguages(): array
3737
{
38-
$baseResource = Pimcore::getContainer()->getParameter('pimcore_admin.translations.path');
39-
$languageDir = Pimcore::getKernel()->locateResource($baseResource);
40-
$adminLanguages = Pimcore::getContainer()->getParameter('pimcore_admin.admin_languages');
41-
$appDefaultPath = Pimcore::getContainer()->getParameter('translator.default_path');
42-
43-
$languageDirs = [$languageDir, $appDefaultPath];
38+
$languageDirs = [];
4439
$translatedLanguages = [];
40+
$languages = [];
41+
42+
$container = Pimcore::getContainer();
43+
44+
if ($container->hasParameter('pimcore_admin.translations.path')) {
45+
$baseResource = $container->getParameter('pimcore_admin.translations.path');
46+
$languageDir = Pimcore::getKernel()->locateResource($baseResource);
47+
48+
if (is_dir($languageDir)) {
49+
$languageDirs[] = $languageDir;
50+
}
51+
}
52+
53+
if ($container->hasParameter('translator.default_path')) {
54+
$appDefaultPath = $container->getParameter('translator.default_path');
55+
56+
if (is_dir($appDefaultPath)) {
57+
$languageDirs[] = $appDefaultPath;
58+
}
59+
}
60+
61+
$adminLanguages = $container->hasParameter('pimcore_admin.admin_languages')
62+
? $container->getParameter('pimcore_admin.admin_languages')
63+
: [];
64+
65+
$localeService = $container->get(LocaleServiceInterface::class);
66+
4567
foreach ($languageDirs as $filesDir) {
46-
if (is_dir($filesDir)) {
47-
$files = scandir($filesDir);
48-
foreach ($files as $file) {
49-
if (is_file($filesDir . '/' . $file)) {
50-
$parts = explode('.', $file);
51-
52-
$languageCode = $parts[0];
53-
if ($parts[0] === 'admin') {
54-
// this is for the app specific translations
55-
$languageCode = $parts[1];
56-
}
57-
58-
if ($parts[1] === 'json' || $parts[0] === 'admin') {
59-
if (Pimcore::getContainer()->get(LocaleServiceInterface::class)->isLocale($languageCode)) {
60-
$translatedLanguages[] = $languageCode;
61-
}
62-
}
68+
$files = scandir($filesDir);
69+
70+
if ($files === false) {
71+
continue;
72+
}
73+
foreach ($files as $file) {
74+
$filePath = $filesDir . '/' . $file;
75+
76+
if (!is_file($filePath)) {
77+
continue;
78+
}
79+
80+
$parts = explode('.', $file);
81+
82+
if (count($parts) < 2) {
83+
continue;
84+
}
85+
86+
$languageCode = $parts[0];
87+
88+
if ($parts[0] === 'admin' && isset($parts[1])) {
89+
$languageCode = $parts[1];
90+
}
91+
92+
$extension = end($parts);
93+
94+
if ($extension === 'json' || $parts[0] === 'admin') {
95+
if ($localeService->isLocale($languageCode)) {
96+
$translatedLanguages[] = $languageCode;
6397
}
6498
}
6599
}
66100
}
67101

68-
$languages = [];
102+
$translatedLanguages = array_unique($translatedLanguages);
103+
69104
foreach ($adminLanguages as $adminLanguage) {
70-
if (in_array($adminLanguage, $translatedLanguages, true) || in_array(Locale::getPrimaryLanguage($adminLanguage), $translatedLanguages, true)) {
105+
if (
106+
in_array($adminLanguage, $translatedLanguages, true) ||
107+
in_array(Locale::getPrimaryLanguage($adminLanguage), $translatedLanguages, true)
108+
) {
71109
$languages[] = $adminLanguage;
72110
}
73111
}

0 commit comments

Comments
 (0)