2015-05-05 03:00:01 +02:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
2018-08-03 18:59:23 +02:00
|
|
|
class Language extends dbJSON {
|
2015-07-14 06:41:32 +02:00
|
|
|
public $data;
|
|
|
|
public $db;
|
2017-09-03 23:29:09 +02:00
|
|
|
public $currentLanguage;
|
2017-12-10 22:14:12 +01:00
|
|
|
public $dates;
|
2018-08-21 20:11:31 +02:00
|
|
|
public $unicodeChars;
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
function __construct($currentLanguage)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2015-06-29 04:51:48 +02:00
|
|
|
$this->data = array();
|
2015-07-14 06:41:32 +02:00
|
|
|
$this->db = array();
|
2017-09-03 23:29:09 +02:00
|
|
|
$this->currentLanguage = $currentLanguage;
|
2018-01-21 16:35:47 +01:00
|
|
|
$this->dates = array();
|
2018-08-21 20:11:31 +02:00
|
|
|
$this->unicodeChars = array();
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Load default language
|
|
|
|
$filename = PATH_LANGUAGES.DEFAULT_LANGUAGE_FILE;
|
|
|
|
if (Sanitize::pathFile($filename)) {
|
2015-07-14 06:41:32 +02:00
|
|
|
$Tmp = new dbJSON($filename, false);
|
2015-07-16 03:34:16 +02:00
|
|
|
$this->db = array_merge($this->db, $Tmp->db);
|
2015-06-29 04:51:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// If the user defined a new language replace the content of the default language
|
|
|
|
// If the new dictionary has missing keys this are going to take from the default language
|
|
|
|
$filename = PATH_LANGUAGES.$currentLanguage.'.json';
|
|
|
|
if (Sanitize::pathFile($filename) && (DEFAULT_LANGUAGE_FILE!==$currentLanguage.'.json')) {
|
2015-07-14 06:41:32 +02:00
|
|
|
$Tmp = new dbJSON($filename, false);
|
2015-07-16 03:34:16 +02:00
|
|
|
$this->db = array_merge($this->db, $Tmp->db);
|
2015-06-29 04:51:48 +02:00
|
|
|
}
|
2015-07-03 22:44:26 +02:00
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Language-data
|
2015-07-14 06:41:32 +02:00
|
|
|
$this->data = $this->db['language-data'];
|
2015-07-03 22:44:26 +02:00
|
|
|
unset($this->db['language-data']);
|
2017-12-10 22:14:12 +01:00
|
|
|
|
|
|
|
// Dates
|
2018-01-21 16:35:47 +01:00
|
|
|
if (isset($this->db['dates'])) {
|
|
|
|
$this->dates = $this->db['dates'];
|
|
|
|
unset($this->db['dates']);
|
|
|
|
}
|
|
|
|
|
2018-08-21 20:11:31 +02:00
|
|
|
// Unicode chars
|
|
|
|
if (isset($this->db['unicode-chars'])) {
|
|
|
|
$this->unicodeChars = $this->db['unicode-chars'];
|
|
|
|
unset($this->db['unicode-chars']);
|
2018-01-21 16:35:47 +01:00
|
|
|
}
|
2015-06-29 04:51:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
public function locale()
|
2015-08-17 02:24:22 +02:00
|
|
|
{
|
2017-09-03 23:29:09 +02:00
|
|
|
if (isset($this->data['locale'])) {
|
|
|
|
return $this->data['locale'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->currentLanguage;
|
2015-08-17 02:24:22 +02:00
|
|
|
}
|
|
|
|
|
2017-09-05 23:46:45 +02:00
|
|
|
public function currentLanguage()
|
|
|
|
{
|
|
|
|
return $this->currentLanguage;
|
|
|
|
}
|
|
|
|
|
2018-07-30 23:43:12 +02:00
|
|
|
public function currentLanguageShortVersion()
|
|
|
|
{
|
|
|
|
$current = $this->currentLanguage;
|
|
|
|
$explode = explode('_', $current);
|
|
|
|
return $explode[0];
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Return the translation, if the translation doesn't exist returns the English translation
|
2015-07-04 00:36:37 +02:00
|
|
|
public function get($string)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2015-07-04 00:36:37 +02:00
|
|
|
$key = Text::lowercase($string);
|
2015-05-31 03:06:55 +02:00
|
|
|
$key = Text::replace(' ', '-', $key);
|
2018-08-05 17:54:20 +02:00
|
|
|
$key = Text::replace('.', '', $key);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
//file_put_contents(DEBUG_FILE, $key.PHP_EOL, FILE_APPEND);
|
2017-05-03 21:10:03 +02:00
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
if (isset($this->db[$key])) {
|
2015-05-05 03:00:01 +02:00
|
|
|
return $this->db[$key];
|
2015-07-14 06:41:32 +02:00
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2018-08-08 00:16:35 +02:00
|
|
|
//$line = '"'.$key.'": "'.$string.'",';
|
|
|
|
//file_put_contents(DEBUG_FILE, $line.PHP_EOL, FILE_APPEND);
|
2016-03-27 21:01:23 +02:00
|
|
|
return $string;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Returns translation
|
2015-07-04 00:36:37 +02:00
|
|
|
public function g($string)
|
|
|
|
{
|
|
|
|
return $this->get($string);
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Print translation
|
2015-07-24 05:28:25 +02:00
|
|
|
public function printMe($string)
|
|
|
|
{
|
|
|
|
echo $this->get($string);
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Print translation
|
2015-07-04 00:36:37 +02:00
|
|
|
public function p($string)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2015-07-04 00:56:24 +02:00
|
|
|
echo $this->get($string);
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Add keys=>values to the current dicionary
|
|
|
|
// This method overwrite the key=>value
|
2015-07-03 22:44:26 +02:00
|
|
|
public function add($array)
|
|
|
|
{
|
2016-06-06 04:24:15 +02:00
|
|
|
$this->db = array_merge($array, $this->db);
|
2015-07-03 22:44:26 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 21:26:06 +02:00
|
|
|
/*
|
2017-09-03 23:29:09 +02:00
|
|
|
// Returns the item from language-data
|
2015-07-14 06:41:32 +02:00
|
|
|
public function getData($key)
|
|
|
|
{
|
2017-09-03 23:29:09 +02:00
|
|
|
if (isset($this->data[$key])) {
|
2015-07-14 06:41:32 +02:00
|
|
|
return $this->data[$key];
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
return false;
|
2015-07-14 06:41:32 +02:00
|
|
|
}
|
2017-09-15 21:26:06 +02:00
|
|
|
*/
|
2015-07-14 06:41:32 +02:00
|
|
|
|
2017-09-03 23:29:09 +02:00
|
|
|
// Returns an array with all dictionaries
|
2015-07-14 06:41:32 +02:00
|
|
|
public function getLanguageList()
|
|
|
|
{
|
2015-09-15 01:07:15 +02:00
|
|
|
$files = Filesystem::listFiles(PATH_LANGUAGES, '*', 'json');
|
2015-07-14 06:41:32 +02:00
|
|
|
$tmp = array();
|
2017-09-03 23:29:09 +02:00
|
|
|
foreach($files as $file) {
|
2015-07-14 06:41:32 +02:00
|
|
|
$t = new dbJSON($file, false);
|
2017-09-03 23:29:09 +02:00
|
|
|
if (isset($t->db['language-data']['native'])) {
|
2015-11-20 04:21:39 +01:00
|
|
|
$native = $t->db['language-data']['native'];
|
|
|
|
$locale = basename($file, '.json');
|
|
|
|
$tmp[$locale] = $native;
|
|
|
|
}
|
2015-07-14 06:41:32 +02:00
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
2017-12-10 22:14:12 +01:00
|
|
|
|
|
|
|
// Returns array with all the dates and months
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return $this->dates;
|
|
|
|
}
|
2018-01-21 16:35:47 +01:00
|
|
|
|
|
|
|
// Returns array with all the special characters from this language
|
2018-08-21 20:11:31 +02:00
|
|
|
public function getunicodeChars()
|
2018-01-21 16:35:47 +01:00
|
|
|
{
|
2018-08-21 20:11:31 +02:00
|
|
|
return $this->unicodeChars;
|
2018-01-21 16:35:47 +01:00
|
|
|
}
|
2017-09-03 23:29:09 +02:00
|
|
|
}
|