| Server IP : 46.105.57.169 / Your IP : 216.73.216.67 Web Server : Apache System : Linux webm002.cluster120.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64 User : verseaumee ( 152031) PHP Version : 8.5.7 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/verseaumee/quaadraa/app/Core/ |
Upload File : |
<?php
namespace App\Core;
use App\Controllers\PageController;
class Lang
{
private string $current = 'fr';
private array $supported = ['fr', 'en'];
private array $strings = [];
public function __construct()
{
$this->detect();
$this->load();
}
private function detect(): void
{
$uri = $_SERVER['REQUEST_URI'] ?? '/';
$seg = explode('/', trim($uri, '/'))[0] ?? 'fr';
if (in_array($seg, $this->supported, true)) {
$this->current = $seg;
}
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$_SESSION['lang'] = $this->current;
}
private function load(): void
{
$file = dirname(__DIR__, 2) . "/lang/{$this->current}.json";
if (file_exists($file)) {
$this->strings = json_decode(file_get_contents($file), true) ?? [];
}
}
public function t(string $key, array $replace = []): string
{
$parts = explode('.', $key);
$value = $this->strings;
foreach ($parts as $part) {
if (!is_array($value) || !isset($value[$part])) {
return $key; // Clé manquante → retourne la clé
}
$value = $value[$part];
}
$str = is_string($value) ? $value : $key;
foreach ($replace as $k => $v) {
$str = str_replace('{{' . $k . '}}', $v, $str);
}
return $str;
}
public function getCurrent(): string { return $this->current; }
public function getSupported(): array { return $this->supported; }
public function switchUrl(string $targetLang): string
{
$uri = $_SERVER['REQUEST_URI'] ?? '/';
$path = trim($uri, '/');
$parts = explode('/', $path);
$count = count($parts);
if ($count === 2) {
$slug = $parts[1];
$newSlug = PageController::getTranslatedUrlFromSlug($slug, $targetLang);
return "/{$targetLang}/{$newSlug}";
}
if ($count === 3) {
$cat = ($targetLang === 'fr') ? 'activites' : 'activities';
$pole = $parts[2];
return "/{$targetLang}/{$cat}/{$pole}";
}
if ($count >= 4) {
$pole = $parts[2];
$slug = $parts[3];
$newSlug = PageController::getTranslatedUrlFromSlug($slug, $targetLang);
$cat = ($targetLang === 'fr') ? 'activites' : 'activities';
return "/{$targetLang}/{$cat}/{$pole}/{$newSlug}";
}
return '/' . $targetLang;
}
}