'Method not allowed']); exit; } $body = json_decode(file_get_contents('php://input'), true); if (!isset($body['lang'], $body['translations'])) { http_response_code(400); echo json_encode(['error' => 'Missing lang or translations']); exit; } $lang = preg_replace('/[^a-z]/', '', strtolower($body['lang'])); $translations = $body['translations']; if (strlen($lang) < 2 || strlen($lang) > 5) { http_response_code(400); echo json_encode(['error' => 'Invalid lang code']); exit; } // Load English as base — ensures every key exists even if not translated $base = include __DIR__ . '/lang/en.php'; // Overwrite only keys that were translated foreach ($translations as $key => $value) { if (array_key_exists($key, $base)) { $base[$key] = $value; } } // Build PHP file content $lines = [" $value) { $key = addslashes($key); $value = addslashes($value); $lines[] = " '$key' => '$value',"; } $lines[] = "];"; $content = implode("\n", $lines) . "\n"; $langDir = getenv('WEBSITE_LANG_WRITE_DIR') ?: (__DIR__ . '/lang'); if (!is_dir($langDir) && !mkdir($langDir, 0755, true)) { http_response_code(500); echo json_encode(['error' => 'Could not create language directory']); exit; } $path = "$langDir/$lang.php"; if (file_put_contents($path, $content) === false) { http_response_code(500); echo json_encode(['error' => 'Could not write language file']); exit; } echo json_encode(['success' => true, 'lang' => $lang]);