1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\I18n\Translator\Backend; |
4
|
|
|
|
5
|
|
|
use Nip_File_System as FileSystem; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Nip Framework |
9
|
|
|
* |
10
|
|
|
* @category Nip |
11
|
|
|
* @copyright 2009 Nip Framework |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
13
|
|
|
* @version SVN: $Id$ |
14
|
|
|
*/ |
15
|
|
|
class File extends AbstractBackend |
16
|
|
|
{ |
17
|
|
|
protected $variableName = 'lang'; |
18
|
|
|
protected $dictionary; |
19
|
|
|
|
20
|
|
|
protected $baseDirectory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
1 |
|
public function getBaseDirectory() |
26
|
|
|
{ |
27
|
1 |
|
return $this->baseDirectory; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $baseDirectory |
32
|
|
|
*/ |
33
|
1 |
|
public function setBaseDirectory($baseDirectory) |
34
|
|
|
{ |
35
|
1 |
|
$this->baseDirectory = $baseDirectory; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $languages |
40
|
|
|
*/ |
41
|
1 |
|
public function addLanguages($languages) |
42
|
|
|
{ |
43
|
1 |
|
foreach ($languages as $language) { |
44
|
1 |
|
$this->addLanguage($language); |
45
|
|
|
} |
46
|
1 |
|
} |
47
|
|
|
|
48
|
1 |
|
public function addLanguage($language) |
49
|
|
|
{ |
50
|
1 |
|
$directory = $this->compileLanguageDirectory($language); |
51
|
1 |
|
return $this->addLanguageFromPath($language, $directory); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
protected function compileLanguageDirectory($lang) |
55
|
|
|
{ |
56
|
1 |
|
return $this->getBaseDirectory() . DIRECTORY_SEPARATOR . $lang; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Adds a language to the dictionary |
61
|
|
|
* |
62
|
|
|
* @param string $language |
63
|
|
|
* @param string $path Path to file containing translations |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
1 |
|
public function addLanguageFromPath($language, $path) |
67
|
|
|
{ |
68
|
1 |
|
$this->languages[] = $language; |
69
|
|
|
|
70
|
1 |
|
$resolvedIncludePath = stream_resolve_include_path($path); |
71
|
1 |
|
$fromIncludePath = ($resolvedIncludePath !== false) ? $resolvedIncludePath : $path; |
72
|
|
|
|
73
|
1 |
|
if (is_dir($fromIncludePath)) { |
74
|
1 |
|
$this->loadDirectory($language, $fromIncludePath); |
75
|
|
|
} elseif (is_file($fromIncludePath)) { |
76
|
|
|
$this->loadFile($language, $fromIncludePath); |
77
|
|
|
} else { |
78
|
|
|
trigger_error( |
79
|
|
|
"Language file [" . $language . "][" . $path . "][" . $fromIncludePath . "] does not exist", |
80
|
|
|
E_USER_ERROR |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $language |
89
|
|
|
* @param $path |
90
|
|
|
*/ |
91
|
1 |
|
public function loadDirectory($language, $path) |
92
|
|
|
{ |
93
|
1 |
|
$files = FileSystem::instance()->scanDirectory($path, true, true); |
94
|
1 |
|
if (is_array($files)) { |
95
|
1 |
|
foreach ($files as $file) { |
96
|
1 |
|
if (FileSystem::instance()->getExtension($file) == 'php') { |
97
|
1 |
|
$this->loadFile($language, $file); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $language |
105
|
|
|
* @param $path |
106
|
|
|
*/ |
107
|
1 |
|
protected function loadFile($language, $path) |
108
|
|
|
{ |
109
|
1 |
|
if (file_exists($path)) { |
110
|
|
|
/** @noinspection PhpIncludeInspection */ |
111
|
1 |
|
$messages = include $path; |
112
|
|
|
|
113
|
1 |
|
if (is_array($messages)) { |
114
|
1 |
|
$this->loadMessages($language, $messages); |
115
|
|
|
} else { |
116
|
|
|
trigger_error( |
117
|
|
|
sprintf( |
118
|
|
|
'Expected an array, but received %s [%s][%s]', |
119
|
|
|
gettype($messages), $language, $messages |
120
|
|
|
), |
121
|
|
|
E_USER_ERROR |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $language |
129
|
|
|
* @param $messages |
130
|
|
|
*/ |
131
|
1 |
|
protected function loadMessages($language, $messages) |
132
|
|
|
{ |
133
|
1 |
|
foreach ($messages as $slug => $translation) { |
134
|
|
|
if ($slug) { |
135
|
|
|
$this->dictionary[$language][$slug] = $translation; |
136
|
|
|
} |
137
|
|
|
} |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns dictionary entry for $slug in $language |
142
|
|
|
* @param string $slug |
143
|
|
|
* @param string|bool $language |
144
|
|
|
* @return string|bool |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
protected function doTranslation($slug, $language = false) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
if (isset($this->dictionary[$language][$slug])) { |
149
|
|
|
return $this->dictionary[$language][$slug]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.