17 if (!defined(
"PSF_ENTRY_POINT"))
18 die(
"Not a valid psf entry point");
20 require_once (dirname(__FILE__) .
"/../default_config.php");
28 if (!array_key_exists($key, $this->data))
30 return $this->data[$key];
33 function __construct($datafile)
35 $handle = fopen(
"$datafile",
"r");
37 throw new Exception(
'Unable to load ' . $datafile);
38 while (($line = fgets($handle)) !==
false)
40 $line = str_replace(
"\n",
"", $line);
42 if (psf_string_startsWith($line,
"//"))
44 if (!strpos($line,
":"))
46 $key = substr($line, 0, strpos($line,
':'));
47 $value = substr($line, strpos($line,
':') + 1);
48 $this->data[$key] = $value;
56 protected static $__is_initialized =
false;
57 protected static $fallback = NULL;
58 protected static $keys = [];
61 public static function SetDefaultLanguage($id)
64 if (!array_key_exists($id, self::$keys))
65 throw new Exception(
'Unknown language id');
68 public static function Get($key, $l = NULL)
71 $x = self::$dl->Get($key);
72 if (!$x && self::$fallback != self::$dl)
73 $x = self::$fallback->Get($key);
79 public static function SetLanguage($id)
82 if (!array_key_exists($id, self::$keys))
83 throw new Exception(
'Unknown language');
85 self::$dl = self::$keys[$id];
88 public static function IsKnown($id)
91 return array_key_exists($id, self::$keys);
94 public static function Initialize($folder = NULL)
96 global $psf_localization, $psf_localization_default_language;
97 if (Localization::$__is_initialized)
100 self::$__is_initialized =
true;
102 if ($folder === NULL)
103 $folder = $psf_localization;
105 if (!file_exists($folder))
106 throw new Exception(
"Localization folder " . $folder .
" can't be found");
108 if (is_file($folder))
109 throw new Exception(
"Localization folder " . $folder .
" is a file");
111 $files = scandir($folder);
113 foreach ($files as $file)
115 if (!psf_string_endsWith($file,
".txt"))
118 $id = substr($file, 0, strlen($file) - 4);
119 self::$keys[$id] =
new Language($folder .
"/" . $file);
120 self::$dl = self::$keys[$id];
122 if (array_key_exists($psf_localization_default_language, self::$keys))
123 self::$dl = self::$keys[$psf_localization_default_language];
124 self::$fallback = self::$dl;