forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.php
More file actions
185 lines (152 loc) · 3.68 KB
/
Copy pathLanguage.php
File metadata and controls
185 lines (152 loc) · 3.68 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php namespace CodeIgniter\Language;
use Config\Services;
class Language
{
/**
* Stores the retrieved language lines
* from files for faster retrieval on
* second use.
*
* @var array
*/
protected $language = [];
/**
* The current language/locale to work with.
*
* @var string
*/
protected $locale;
/**
* Boolean value whether the intl
* libraries exist on the system.
*
* @var bool
*/
protected $intlSupport = false;
/**
* Stores filenames that have been
* loaded so that we don't load them again.
*
* @var array
*/
protected $loadedFiles = [];
//--------------------------------------------------------------------
public function __construct(string $locale)
{
$this->locale = $locale;
if (class_exists('\MessageFormatter'))
{
$this->intlSupport = true;
};
}
//--------------------------------------------------------------------
/**
* Parses the language string for a file, loads the file, if necessary,
* getting
*
* @param string $line
* @param array $args
*
* @return string
*/
public function getLine(string $line, array $args = []): string
{
// Parse out the file name and the actual alias.
// Will load the language file and strings.
list($file, $line) = $this->parseLine($line);
$output = isset($this->language[$file][$line])
? $this->language[$file][$line]
: $line;
// Do advanced message formatting here
// if the 'intl' extension is available.
if ($this->intlSupport && count($args))
{
$output = \MessageFormatter::formatMessage($this->locale, $output, $args);
}
return $output;
}
//--------------------------------------------------------------------
/**
* Parses the language string which should include the
* filename as the first segment (separated by period).
*
* @param string $line
*
* @return string
*/
protected function parseLine(string $line): array
{
if (strpos($line, '.') === false)
{
throw new \InvalidArgumentException('No language file specified in line: '.$line);
}
$file = substr($line, 0, strpos($line, '.'));
$line = substr($line, strlen($file)+1);
if (! array_key_exists($line, $this->language))
{
$this->load($file, $this->locale);
}
return [
$file,
$this->language[$line] ?? $line
];
}
//--------------------------------------------------------------------
/**
* Loads a language file in the current locale. If $return is true,
* will return the file's contents, otherwise will merge with
* the existing language lines.
*
* @param string $file
* @param string $locale
* @param bool $return
*
* @return array|null
*/
protected function load(string $file, string $locale, bool $return = false)
{
if (in_array($file, $this->loadedFiles))
{
return [];
}
if (! array_key_exists($file, $this->language))
{
$this->language[$file] = [];
}
$path = "Language/{$locale}/{$file}.php";
$lang = $this->requireFile($path);
// Don't load it more than once.
$this->loadedFiles[] = $file;
if ($return)
{
return $lang;
}
// Merge our string
$this->language[$file] = $lang;
}
//--------------------------------------------------------------------
/**
* A simple method for including files that can be
* overridden during testing.
*
* @todo - should look into loading from other locations, also probably...
*
* @param string $path
*
* @return array
*/
protected function requireFile(string $path): array
{
$files = service('locator')->search($path);
foreach ($files as $file)
{
if (! is_file($file))
{
continue;
}
return require_once $file;
}
return [];
}
//--------------------------------------------------------------------
}