<?php
function get_lang() {
    if (!isset($_SESSION['lang'])) {
        $pdo = db();
        $stmt = $pdo->query("SELECT setting_value FROM site_settings WHERE setting_key = 'default_language'");
        $default_lang = $stmt->fetchColumn() ?: 'en';
        $_SESSION['lang'] = $default_lang;
    }
    
    if (isset($_GET['lang']) && in_array($_GET['lang'], ['en', 'id'])) {
        $_SESSION['lang'] = $_GET['lang'];
    }
    
    return $_SESSION['lang'];
}

function __($key) {
    static $translations = null;
    if ($translations === null) {
        $lang = get_lang();
        $translations = require __DIR__ . "/$lang.php";
    }
    
    return $translations[$key] ?? $key;
}
