Explorar el Código

Fix Language change

svalavuo hace 6 días
padre
commit
fba2a1841b
Se han modificado 3 ficheros con 125 adiciones y 6 borrados
  1. 14 6
      includes/translation.php
  2. 65 0
      test_language_logic.php
  3. 46 0
      test_language_switching.php

+ 14 - 6
includes/translation.php

@@ -37,14 +37,22 @@ class Translation {
             
             // Redirect to same page without language parameter to apply new language
             $currentUrl = $_SERVER['REQUEST_URI'];
-            $urlWithoutLang = strtok($currentUrl, '?')[0];
+            $parsedUrl = parse_url($currentUrl);
             
-            // Redirect to same page without language parameter
-            if ($urlWithoutLang !== false) {
-                header('Location: ' . $urlWithoutLang);
-                exit;
+            // Build URL without lang parameter
+            $redirectUrl = $parsedUrl['path'];
+            if (isset($parsedUrl['query'])) {
+                parse_str($parsedUrl['query'], $queryParams);
+                unset($queryParams['lang']); // Remove lang parameter
+                
+                if (!empty($queryParams)) {
+                    $redirectUrl .= '?' . http_build_query($queryParams);
+                }
             }
-            return;
+            
+            // Redirect to clean URL
+            header('Location: ' . $redirectUrl);
+            exit;
         }
         
         // Check browser language preference

+ 65 - 0
test_language_logic.php

@@ -0,0 +1,65 @@
+<?php
+// Test language switching logic without redirects
+session_start();
+require_once 'includes/config.php';
+
+echo "=== Testing Language Switching Logic ===\n\n";
+
+// Test URL parsing logic directly
+function testUrlParsing($testUrl) {
+    echo "Testing URL: $testUrl\n";
+    
+    $parsedUrl = parse_url($testUrl);
+    $redirectUrl = $parsedUrl['path'];
+    
+    if (isset($parsedUrl['query'])) {
+        parse_str($parsedUrl['query'], $queryParams);
+        unset($queryParams['lang']); // Remove lang parameter
+        
+        if (!empty($queryParams)) {
+            $redirectUrl .= '?' . http_build_query($queryParams);
+        }
+    }
+    
+    echo "Parsed redirect URL: $redirectUrl\n\n";
+    return $redirectUrl;
+}
+
+// Test various URL scenarios
+$testUrls = [
+    '/index.php?lang=fi&category=test',
+    '/index.php?category=test&lang=fi',
+    '/index.php?lang=fi',
+    '/index.php?category=test&page=2',
+    '/index.php'
+];
+
+foreach ($testUrls as $url) {
+    testUrlParsing($url);
+}
+
+// Test session handling
+echo "=== Testing Session Handling ===\n";
+
+// Simulate language setting
+$_SESSION['language'] = 'fi';
+echo "Session language set to: " . $_SESSION['language'] . "\n";
+
+// Test translation initialization
+try {
+    require_once 'includes/translation.php';
+    
+    // Clear GET to avoid redirect
+    $_GET = array();
+    
+    $translation = Translation::getInstance();
+    echo "Translation instance created successfully\n";
+    echo "Current language: " . $translation->getCurrentLanguage() . "\n";
+    echo "Supported languages: " . implode(', ', array_keys($translation->getSupportedLanguages())) . "\n";
+    
+} catch (Exception $e) {
+    echo "Error: " . $e->getMessage() . "\n";
+}
+
+echo "\n=== Language Switching Logic Test Complete ===\n";
+?>

+ 46 - 0
test_language_switching.php

@@ -0,0 +1,46 @@
+<?php
+// Start session
+session_start();
+
+require_once 'includes/config.php';
+require_once 'includes/translation.php';
+
+echo "=== Testing Language Switching Fix ===\n\n";
+
+// Test 1: Simulate URL with lang parameter
+$_GET['lang'] = 'fi';
+$_SERVER['REQUEST_URI'] = '/index.php?lang=fi&category=test';
+
+echo "Test 1 - URL with lang parameter: $_SERVER[REQUEST_URI]\n";
+
+try {
+    $translation = Translation::getInstance();
+    echo "Current language after lang parameter: " . $translation->getCurrentLanguage() . "\n";
+    echo "Session language: " . ($_SESSION['language'] ?? 'not set') . "\n";
+    echo "SUCCESS: Language switching works!\n\n";
+} catch (Exception $e) {
+    echo "ERROR: " . $e->getMessage() . "\n\n";
+}
+
+// Test 2: Simulate URL without lang parameter (should use session)
+$_GET = array(); // Clear GET params
+$_SERVER['REQUEST_URI'] = '/index.php?category=test';
+
+echo "Test 2 - URL without lang parameter: $_SERVER[REQUEST_URI]\n";
+
+try {
+    $translation = Translation::getInstance();
+    echo "Current language from session: " . $translation->getCurrentLanguage() . "\n";
+    echo "Session language: " . ($_SESSION['language'] ?? 'not set') . "\n";
+    echo "SUCCESS: Session persistence works!\n\n";
+} catch (Exception $e) {
+    echo "ERROR: " . $e->getMessage() . "\n\n";
+}
+
+// Test 3: Test language switcher generation
+echo "Test 3 - Language switcher generation:\n";
+$switcherHtml = $translation->getLanguageSwitcher('index.php?category=test');
+echo "Language switcher HTML:\n$switcherHtml\n\n";
+
+echo "=== All Tests Completed ===\n";
+?>