45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class LanguageService {
|
|
private static $translations = [];
|
|
private static $lang = 'id';
|
|
|
|
public static function init() {
|
|
if (isset($_SESSION['lang'])) {
|
|
self::$lang = $_SESSION['lang'];
|
|
} elseif (isset($_COOKIE['lang'])) {
|
|
self::$lang = $_COOKIE['lang'];
|
|
}
|
|
|
|
$langFile = __DIR__ . '/../../lang/' . self::$lang . '.php';
|
|
if (file_exists($langFile)) {
|
|
self::$translations = require $langFile;
|
|
} else {
|
|
// Default to English if the translation file doesn't exist
|
|
self::$lang = 'en';
|
|
$langFile = __DIR__ . '/../../lang/' . self::$lang . '.php';
|
|
if (file_exists($langFile)) {
|
|
self::$translations = require $langFile;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function translate($key, $default = null) {
|
|
if (empty(self::$translations)) {
|
|
self::init();
|
|
}
|
|
return self::$translations[$key] ?? ($default ?? $key);
|
|
}
|
|
|
|
public static function setLang($lang) {
|
|
self::$lang = $lang;
|
|
$_SESSION['lang'] = $lang;
|
|
setcookie('lang', $lang, time() + (86400 * 30), "/");
|
|
}
|
|
|
|
public static function getLang() {
|
|
return self::$lang;
|
|
}
|
|
} |