Files
breadcrumb-the-shire/templates/partials/app-language-switcher.phtml

69 lines
2.8 KiB
PHTML
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
use MintyPHP\I18n;
use MintyPHP\Http\Request;
$locales = defined('APP_LOCALES') ? APP_LOCALES : [I18n::$defaultLocale];
$returnTarget = Request::pathWithQuery();
$variant = $variant ?? 'select';
$baseUrl = lurl('lang');
$isLoggedIn = !empty($_SESSION['user']['id']);
$publicTarget = Request::stripLocale($returnTarget);
$publicTarget = ltrim($publicTarget, '/');
$base = \MintyPHP\Router::getBaseUrl();
$base = rtrim($base, '/');
$publicTarget = ltrim($publicTarget, '/');
?>
2026-03-06 00:44:52 +01:00
<?php if ($variant === 'menu-items'): ?>
2026-03-05 12:51:53 +01:00
<?php foreach ($locales as $locale): ?>
<?php
$isActive = $locale === I18n::$locale;
2026-03-05 13:07:43 +01:00
$normalizedLocale = strtolower(trim((string) $locale));
if (strpos($normalizedLocale, '-') !== false) {
$normalizedLocale = explode('-', $normalizedLocale, 2)[0];
}
$languageLabels = [
'de' => t('German'),
'en' => t('English'),
];
$label = $languageLabels[$normalizedLocale] ?? strtoupper((string) $locale);
2026-03-05 12:51:53 +01:00
$publicUrl = $base . '/' . $locale . ($publicTarget !== '' ? '/' . $publicTarget : '');
$switchUrl = $isLoggedIn
? ($baseUrl . '?locale=' . $locale . '&return=' . urlencode($returnTarget))
: $publicUrl;
?>
<li>
<a href="<?php e($switchUrl); ?>"
class="<?php e($isActive ? 'active' : ''); ?>"
<?php if ($isActive) { ?>aria-current="true"<?php } ?>>
2026-03-05 13:07:43 +01:00
<?php e($label); ?>
2026-03-05 12:51:53 +01:00
</a>
</li>
<?php endforeach; ?>
2026-02-04 23:31:53 +01:00
<?php else: ?>
<?php if ($isLoggedIn): ?>
2026-03-06 00:44:52 +01:00
<form class="nav nav--lang" method="get" action="<?php e($baseUrl); ?>" aria-label="<?php e(t('Language switcher')); ?>">
2026-02-04 23:31:53 +01:00
<input type="hidden" name="return" value="<?php e($returnTarget); ?>">
<select class="lang-select" name="locale" data-auto-submit>
2026-02-04 23:31:53 +01:00
<?php foreach ($locales as $locale): ?>
<option value="<?php e($locale); ?>" <?php if ($locale === I18n::$locale) { ?>selected<?php } ?>>
<?php e(strtoupper((string) $locale)); ?>
</option>
<?php endforeach; ?>
</select>
</form>
<?php else: ?>
2026-03-06 00:44:52 +01:00
<div class="nav nav--lang" role="listbox" aria-label="<?php e(t('Language switcher')); ?>">
2026-02-04 23:31:53 +01:00
<?php foreach ($locales as $locale): ?>
<?php
$publicUrl = $base . '/' . $locale . ($publicTarget !== '' ? '/' . $publicTarget : '');
?>
<a href="<?php e($publicUrl); ?>" class="lang-link"
<?php if ($locale === I18n::$locale) { ?>aria-current="true"<?php } ?>>
<?php e(strtoupper((string) $locale)); ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endif; ?>