1
|
|
|
<?php |
|
|
|
|
2
|
|
|
require_once 'Zend/Locale.php'; |
3
|
|
|
require_once 'Zend/Translate/Adapter.php'; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package framework |
7
|
|
|
* @subpackage i18n |
8
|
|
|
*/ |
9
|
|
|
class i18nSSLegacyAdapter extends Zend_Translate_Adapter implements i18nTranslateAdapterInterface { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generates the adapter |
13
|
|
|
* |
14
|
|
|
* @param array|Zend_Config $options Translation content |
15
|
|
|
*/ |
16
|
|
|
public function __construct($options = array()) { |
17
|
|
|
$this->_options['keyDelimiter'] = "."; |
18
|
|
|
parent::__construct($options); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function _loadTranslationData($data, $locale, array $options = array()) { |
22
|
|
|
$options = array_merge($this->_options, $options); |
23
|
|
|
|
24
|
|
|
if ($options['clear'] || !isset($this->_translate[$locale])) { |
25
|
|
|
$this->_translate[$locale] = array(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if(is_array($data)) return array($locale => $data); |
29
|
|
|
|
30
|
|
|
$this->_filename = $data; |
|
|
|
|
31
|
|
|
|
32
|
|
|
// Ignore files with other extensions |
33
|
|
|
if(pathinfo($this->_filename, PATHINFO_EXTENSION) != 'php') return; |
34
|
|
|
|
35
|
|
|
if (!is_readable($this->_filename)) { |
36
|
|
|
require_once 'Zend/Translate/Exception.php'; |
37
|
|
|
throw new Zend_Translate_Exception('Error opening translation file \'' . $this->_filename . '\'.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
global $lang; |
|
|
|
|
41
|
|
|
if(!isset($lang['en_US'])) $lang['en_US'] = array(); |
42
|
|
|
// TODO Diff locale array to avoid re-parsing all previous translations whenever a new module is included. |
43
|
|
|
require_once($this->_filename); |
44
|
|
|
|
45
|
|
|
$flattened = array(); |
46
|
|
|
if($lang[$locale]) { |
47
|
|
|
$iterator = new i18nSSLegacyAdapter_Iterator(new RecursiveArrayIterator($lang[$locale])); |
48
|
|
|
foreach($iterator as $k => $v) { |
49
|
|
|
$flattenedKey = implode($options['keyDelimiter'], array_filter($iterator->getKeyStack())); |
50
|
|
|
$flattened[$flattenedKey] = (is_array($v)) ? $v[0] : $v; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return array($locale => $flattened); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function toString() { |
58
|
|
|
return "i18nSSLegacy"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getFilenameForLocale($locale) { |
62
|
|
|
return "{$locale}.php"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @package framework |
69
|
|
|
* @subpackage i18n |
70
|
|
|
*/ |
71
|
|
|
class i18nSSLegacyAdapter_Iterator extends RecursiveIteratorIterator { |
72
|
|
|
|
73
|
|
|
protected $keyStack = array(); |
74
|
|
|
|
75
|
|
|
public function callGetChildren() { |
76
|
|
|
$this->keyStack[] = parent::key(); |
|
|
|
|
77
|
|
|
return parent::callGetChildren(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function endChildren() { |
81
|
|
|
array_pop($this->keyStack); |
82
|
|
|
parent::endChildren(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function key() { |
86
|
|
|
return json_encode($this->getKeyStack()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getKeyStack() { |
90
|
|
|
return array_merge($this->keyStack, array(parent::key())); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.