-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_language.php
131 lines (106 loc) · 2.77 KB
/
class_language.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
*
* Hadron Framework: Language Handling Object.
* Created by Azareal.
* Licensed under the terms of the GPLv3.
* Copyright Azareal (c) 2013 - 2017
*
**/
// Hadron Framework namespace..
namespace Hadron;
// Is someone trying to access this directly?
if(!defined("HADRON_START")) die("You are not allowed to access this file directly.");
class Language
{
public $language;
protected $data = [];
public $dir = "";
protected $main = null;
protected $path = null;
function __construct(Container $main, $path, $language = "english", $dir = "")
{
$this->main = $main;
$this->path = $path;
$plugins = $main->getPlugins();
// Does the language pack exist?
if(is_dir("{$path}/{$language}{$dir}")) $this->language = $language;
// Does a plugin want to change this?
elseif($res = $plugins->hook("langs_init", $language)) $this->language = $res;
// Default to english then..
else $this->language = "english";
$this->dir = $dir;
// Stick this..
$tmpls = $this->main->getTemplates();
$tmpls->stick("lang", $this->data);
// Load the global language file..
$this->load("global");
}
function load($name)
{
$plugins = $this->main->getPlugins();
if($res = $plugins->hook("langs_load", $name))
{
$this->data = array_merge($this->data,$res);
return true;
}
elseif($res===false) return false;
if(!require_once("{$this->path}/{$this->language}{$this->dir}/{$name}.php"))
trigger_error("The '{$name}' language file was unable to be loaded.", E_USER_WARNING);
if(!isset($l)) trigger_error("The '{$name}' language file doesn't contain any language strings.", E_USER_WARNING);
if(($res = $plugins->hook("langs_load_end", $name))!==null) $l = array_merge($l,$res);
// Add it to the loaded data..
$this->data = array_merge($this->data,$l);
return true;
}
function get($name)
{
if(!isset($this->data[$name])) return '{$lang_'.name.'}';
return $this->data[$name];
}
function __get($name)
{
if(!isset($this->data[$name])) return '{$lang_'.name.'}';
return $this->data[$name];
}
function sub($name, $data)
{
return str_replace("$1", $data, $this->data[$name]);
}
function multi_sub($name, $data)
{
$str = $this->data[$name];
$i = 1;
foreach($data as $item)
{
$str = str_replace('$'.$i, $item ,$str);
$i++;
}
return $str;
}
function rand($name)
{
if(!is_array($this->data[$name])) return false;
return array_rand($this->data[$name]);
}
function set($name, $data)
{
$this->data[$name] = $data;
}
function __set($name, $data)
{
$this->data[$name] = $data;
}
function import(array $data)
{
$this->data = array_merge($this->data, $data);
}
function exists($name)
{
return isset($this->data[$name]);
}
function __isset($name)
{
return isset($this->data[$name]);
}
}