-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.php
executable file
·27 lines (22 loc) · 900 Bytes
/
lang.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
// Prevent direct access to this file
defined('SECURE_ACCESS') or die('Direct access to this file is not allowed');
// Available languages
const AVAILABLE_LANGUAGES = ['fr', 'en'];
const DEFAULT_LANGUAGE = 'en';
// Set language based on browser preference if not set in session
if (!isset($_SESSION['lang'])) {
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', 0, 2);
$_SESSION['lang'] = in_array($browser_lang, AVAILABLE_LANGUAGES) ? $browser_lang : DEFAULT_LANGUAGE;
}
// Allow language switching via GET parameter
if (isset($_GET['lang']) && in_array($_GET['lang'], AVAILABLE_LANGUAGES)) {
$_SESSION['lang'] = $_GET['lang'];
}
// Load language file
$translations = require_once "lang/{$_SESSION['lang']}.php";
function __($key, ...$args) {
global $translations;
$text = $translations[$key] ?? $key;
return $args ? sprintf($text, ...$args) : $text;
}