forked from ironsmile/php-translit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslit.php
More file actions
235 lines (216 loc) · 7.08 KB
/
Translit.php
File metadata and controls
235 lines (216 loc) · 7.08 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Translit
*
* Provide interface to transliterate text into Roman (Latin) characters. Class
* could be used as singleton or object itself. It's delivered with a few
* transliteration tables and classes:
* - ru - Russian cyrillic chars,
* - uk - Ukrainian cyrillic chars,
* - be - Belarusian cyrillic chars (converted to latin with diacritical),
* - bg - Bulgarian cyrillic chars (converted to latin with diacritical),
* - kk - Kazakh cyrillic chars (converted to latin with diacritical),
* - ka - Georgian chars,
* - hy - Armenian chars (converted to latin with diacritical),
* - el - Greek chars (converted to latin with diacritical),
* - cyrillic - all cyrillic chars according to ISO 9:1995,
* - latin - only latin chars without diacritical marks,
* - ascii - ASCII chars only, other chars will be replaced with question mark.
*
* Language codes could be combined by comma to handle more cases, e.g.
*
* echo Translit::object()->convert('Беларусь', 'be') . ' vs ' .
* Translit::object()->convert('Беларусь', 'be,latin');
*
* produce output:
*
* Bielaruś vs Bielarus
*
* By default wrong language codes ignored. But this behavior could be
* changed by using string mode for really required language code. Just put
* exclamation mark before language code. For example:
*
* // following code do nothing because handler ru_ru not defined
* echo Translit::object()->convert('Привет', 'ru_ru');
* // but next code fires Exception, because transliteration required
* echo Translit::object()->convert('Привет', '!ru_ru');
*
* @package translit
* @version 0.1
* @author Alexey Shtokalo <alexey@shtokalo.net>
*/
class Translit
{
/**
* Path to directory with data files.
*
* @var string empty string by default assumes directory "data" located at
* directory with current class
*/
public $dataPath = '';
/**
* Path to directory with transliteration classes.
*
* @var string empty string by default assumes directory with the class
*/
public $classPath = '';
/**
* Converts given text to the Roman (Latin) script.
*
* The method accept a few codes at once. In this case codes must be splited
* by command and will be applied in order they noted. For example
* 'ru,cyrillic,ascii' converts russian cyrillic chars, then all other
* cyrillic chars and finally produce clean ascii without diacritical marks.
*
* @param string $text text to convert
* @param $code $code language code or a few codes by comma
*
* @return string converted text
* @throws Exception if handler not available or wrong (strict mode only)
*/
public function convert($text, $code)
{
foreach (explode(',', $code) as $code)
{
$language = $this->getLanguage($code);
if (is_object($language))
{
$text = $language->convert($text);
}
else if (is_array($language))
{
$text = str_replace($language['from'], $language['to'], $text);
}
}
return $text;
}
/**
* Returns Translit object, used to avoid global variables
*
* @param string $dataPath
*
* @return Translit
*/
public static function object($dataPath = '')
{
static $object = null;
if (!is_object($object))
{
$object = new Translit();
if ($dataPath) $object->dataPath = $dataPath;
}
return $object;
}
/**
* Returns language handler - array or object.
*
* The object must have method convert. Array must have two sub arrays
* identified by keys "from" and "to".
*
* @param string $code language code
*
* @return object|array
*
* @throws Exception if handler not available or wrong (strict mode only)
*/
protected function getLanguage($code)
{
// all language codes prepended with exclamation mark really required
$strict = false;
if (strncmp($code, '!', 1) === 0)
{
$code = substr($code, 1);
$strict = true;
}
if ($code && !isset($this->languages[$code]))
{
$dataFile = $this->getDataPath() . $code . '.php';
$className = 'Translit' . ucfirst($code);
$classFile = $this->getClassPath() . $className . '.php';
if (file_exists($dataFile))
{
$language = include $dataFile;
if (is_array($language))
{
$this->languages[$code] = array (
'from' => array_keys($language),
'to' => array_values($language),
);
}
else if ($strict)
{
throw new Exception(
sprintf('expected array with alphabet, but %s found',
gettype($language)));
}
}
else
{
if (!@class_exists($className) && file_exists($classFile))
{
include $classFile;
}
if (@class_exists($className))
{
$language = new $className;
if (method_exists($language, 'convert'))
{
if (property_exists($language, 'translit'))
{
$language->translit = $this;
}
$this->languages[$code] = $language;
}
else if ($strict)
{
throw new Exception(
sprintf('class "%s" does not have convert() method',
$className));
}
}
else if ($strict)
{
throw new Exception(
sprintf('language "%s" does not have handlers', $code));
}
}
}
return @$this->languages[$code];
}
/**
* Returns path to data directory
*
* @return string
*/
protected function getDataPath()
{
if (!$this->dataPath)
{
$this->dataPath = dirname(__FILE__) . DIRECTORY_SEPARATOR .
'data' . DIRECTORY_SEPARATOR;
}
return $this->dataPath;
}
/**
* Returns path to directory with transliteration classes
*
* @return string
*/
protected function getClassPath()
{
if (!$this->classPath)
{
$this->classPath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
return $this->classPath;
}
/**
* Cached language handlers, could be an associative array or object.
*
* The object must have method convert. Array must have two sub arrays
* identified by keys "from" and "to".
*
* @var array
*/
protected $languages = array ();
}