| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.6 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base\Factory; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use \FPFramework\Libs\Registry; |
| 20 |
|
| 21 |
class Language |
| 22 |
{ |
| 23 |
private $data = []; |
| 24 |
|
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
$this->setData(); |
| 28 |
} |
| 29 |
|
| 30 |
private function setData() |
| 31 |
{ |
| 32 |
$this->data = $this->getData(); |
| 33 |
} |
| 34 |
|
| 35 |
private function getData() |
| 36 |
{ |
| 37 |
$data = [ |
| 38 |
'url' => $this->getLanguageURL(), |
| 39 |
'locale' => $this->getLocale(), |
| 40 |
'user_locale' => $this->getUserLocale() |
| 41 |
]; |
| 42 |
|
| 43 |
$data = new Registry($data); |
| 44 |
return $data; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Retrieves the language URL |
| 49 |
* Example: en_US => en |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
private function getLanguageURL() |
| 54 |
{ |
| 55 |
$default_lang = 'en'; |
| 56 |
$locale = $this->getUserLocale(); |
| 57 |
|
| 58 |
if (!empty($locale)) |
| 59 |
{ |
| 60 |
$lang = explode( '_', $locale ); |
| 61 |
if (!empty($lang) && is_array($lang)) |
| 62 |
{ |
| 63 |
$default_lang = strtolower($lang[0]); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $default_lang; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the current locale |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
protected function getLocale() |
| 76 |
{ |
| 77 |
return get_locale(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the user locale |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
protected function getUserLocale() |
| 86 |
{ |
| 87 |
return get_user_locale(); |
| 88 |
} |
| 89 |
|
| 90 |
public function get($key) |
| 91 |
{ |
| 92 |
if (!$this->data->get($key)) |
| 93 |
{ |
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
return $this->data->get($key); |
| 98 |
} |
| 99 |
} |