113 lines
4.0 KiB
PHP
113 lines
4.0 KiB
PHP
<?php
|
|
class Language {
|
|
private static $translations = [];
|
|
private static $currentLocale = 'ar';
|
|
private static $fallbackLocale = 'en';
|
|
private static $supportedLocales = ['ar', 'en'];
|
|
|
|
public static function init($locale = null) {
|
|
if ($locale && in_array($locale, self::$supportedLocales)) {
|
|
self::$currentLocale = $locale;
|
|
} elseif (isset($_SESSION['locale'])) {
|
|
self::$currentLocale = $_SESSION['locale'];
|
|
} else {
|
|
// Try to detect from browser
|
|
$browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
|
|
self::$currentLocale = in_array($browserLang, self::$supportedLocales) ? $browserLang : self::$fallbackLocale;
|
|
}
|
|
|
|
$_SESSION['locale'] = self::$currentLocale;
|
|
self::loadTranslations();
|
|
}
|
|
|
|
public static function setLocale($locale) {
|
|
if (in_array($locale, self::$supportedLocales)) {
|
|
self::$currentLocale = $locale;
|
|
$_SESSION['locale'] = $locale;
|
|
self::loadTranslations();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function getCurrentLocale() {
|
|
return self::$currentLocale;
|
|
}
|
|
|
|
public static function isRTL() {
|
|
return in_array(self::$currentLocale, ['ar']);
|
|
}
|
|
|
|
public static function getDirection() {
|
|
return self::isRTL() ? 'rtl' : 'ltr';
|
|
}
|
|
|
|
public static function translate($key, $params = []) {
|
|
$translation = self::$translations[self::$currentLocale][$key] ??
|
|
self::$translations[self::$fallbackLocale][$key] ??
|
|
$key;
|
|
|
|
if (!empty($params)) {
|
|
foreach ($params as $param => $value) {
|
|
$translation = str_replace(':' . $param, $value, $translation);
|
|
}
|
|
}
|
|
|
|
return $translation;
|
|
}
|
|
|
|
private static function loadTranslations() {
|
|
// Load current locale
|
|
$localePath = __DIR__ . '/../lang/' . self::$currentLocale . '.php';
|
|
if (file_exists($localePath)) {
|
|
self::$translations[self::$currentLocale] = require $localePath;
|
|
}
|
|
|
|
// Load fallback locale if different
|
|
if (self::$currentLocale !== self::$fallbackLocale) {
|
|
$fallbackPath = __DIR__ . '/../lang/' . self::$fallbackLocale . '.php';
|
|
if (file_exists($fallbackPath)) {
|
|
self::$translations[self::$fallbackLocale] = require $fallbackPath;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function getAllTranslations() {
|
|
return self::$translations;
|
|
}
|
|
|
|
public static function getSupportedLocales() {
|
|
return self::$supportedLocales;
|
|
}
|
|
|
|
// Helper function for date formatting
|
|
public static function formatDate($timestamp, $format = 'full') {
|
|
$locale = self::$currentLocale . '_' . strtoupper(self::$currentLocale);
|
|
setlocale(LC_TIME, $locale . '.UTF-8');
|
|
|
|
$formats = [
|
|
'full' => '%A %d %B %Y',
|
|
'long' => '%d %B %Y',
|
|
'medium' => '%d %b %Y',
|
|
'short' => '%d/%m/%Y'
|
|
];
|
|
|
|
return strftime($formats[$format] ?? $formats['full'], $timestamp);
|
|
}
|
|
|
|
// Helper function for number formatting
|
|
public static function formatNumber($number, $decimals = 0) {
|
|
$locale = self::$currentLocale . '_' . strtoupper(self::$currentLocale);
|
|
$formatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
|
|
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $decimals);
|
|
return $formatter->format($number);
|
|
}
|
|
|
|
// Helper function for currency formatting
|
|
public static function formatCurrency($amount, $currency = 'EGP') {
|
|
$locale = self::$currentLocale . '_' . strtoupper(self::$currentLocale);
|
|
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
|
return $formatter->formatCurrency($amount, $currency);
|
|
}
|
|
}
|