-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocalization.php
More file actions
162 lines (127 loc) · 3.28 KB
/
localization.php
File metadata and controls
162 lines (127 loc) · 3.28 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* PHP Localization Library
*
* Translate application texts using INI files
*
* @copyright 2016
* @author Ante Laca <ante.laca@gmail.com>
*
*/
class Localization {
private static
$instance = null,
$settings = [],
$translations = [];
private $translationFile;
private function __construct()
{
static::$settings = require 'config.php';
$this->translationFile = static::$settings['path'] . '/' . $this->getCurrentLanguage() . '.ini';
if (is_dir(dirname($this->translationFile))) {
if (file_exists($this->translationFile)) {
static::$translations = parse_ini_file($this->translationFile);
} else {
if (file_exists($default = static::$settings['path'] . '/' . $this->getDefaultLanguage() . '.ini')) {
copy($default, $this->translationFile);
}
}
} else {
printf('Directory <strong>%s</strong> not exists, check path setting', dirname($this->translationFile));
exit;
}
}
/**
* Localization instance
*
*/
public static function instance()
{
if (null === static::$instance) {
static::$instance = new self();
}
return static::$instance;
}
/**
* Get default language
* first element in the languages array
*
* @return string
*/
public function getDefaultLanguage()
{
$languages = static::$settings['languages'];
reset($languages);
return key($languages);
}
/**
* Get current language
*
* @return string
*/
public function getCurrentLanguage()
{
if (isset($_GET[static::$settings['input']])) {
if (array_key_exists($_GET[static::$settings['input']], $this->getLanguages())) {
return $_GET[static::$settings['input']];
}
}
return $this->getDefaultLanguage();
}
/**
* Get all languages
*
* @return array
*/
public function getLanguages()
{
return static::$settings['languages'];
}
/**
* Get translations
*
* @param array $strings
* @return {$string|$string[]}
*/
public function getTranslations($strings = [])
{
if (empty($strings)) {
return static::$translations;
}
$translations = [];
foreach ($strings as $string) {
$translations[$string] = isset(static::$translations[$string]) ? static::$translations[$string] : $this->translate($string);
}
return $translations;
}
/**
* Get json
*
* @return json
*/
public function getJson($strings = [], $options = JSON_UNESCAPED_UNICODE)
{
return json_encode($this->getTranslations($strings), $options);
}
/**
* Translate string
*
* @param string $string
* @param array $args
*/
public function translate($string, $args = null)
{
if (array_key_exists($string, static::$translations)) {
return is_array($args) ? vsprintf(static::$translations[$string], $args) : static::$translations[$string];
}
file_put_contents(static::$translationFile, PHP_EOL . "{$string} = \"{$string}\"", FILE_APPEND | LOCK_EX);
return is_array($args) ? vsprintf($string, $args) : static::$translations[$string] = $string;
}
}
/**
* helper function
*/
function __($string, $args = null)
{
return Localization::instance()->translate($string, $args);
}