2011-11-26 14:34:11 +01:00
|
|
|
<?php
|
|
|
|
require_once 'Zend/Locale.php';
|
|
|
|
require_once 'Zend/Translate/Adapter.php';
|
|
|
|
|
|
|
|
/**
|
2012-06-20 23:59:16 +02:00
|
|
|
* @package framework
|
2011-11-26 14:34:11 +01:00
|
|
|
* @subpackage i18n
|
|
|
|
*/
|
|
|
|
class i18nSSLegacyAdapter extends Zend_Translate_Adapter implements i18nTranslateAdapterInterface {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-26 14:34:11 +01:00
|
|
|
/**
|
|
|
|
* Generates the adapter
|
|
|
|
*
|
|
|
|
* @param array|Zend_Config $options Translation content
|
|
|
|
*/
|
|
|
|
public function __construct($options = array()) {
|
|
|
|
$this->_options['keyDelimiter'] = ".";
|
|
|
|
parent::__construct($options);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
protected function _loadTranslationData($data, $locale, array $options = array()) {
|
2011-11-26 14:34:11 +01:00
|
|
|
$options = array_merge($this->_options, $options);
|
|
|
|
|
|
|
|
if ($options['clear'] || !isset($this->_translate[$locale])) {
|
|
|
|
$this->_translate[$locale] = array();
|
|
|
|
}
|
|
|
|
|
2011-12-04 18:36:58 +01:00
|
|
|
if(is_array($data)) return array($locale => $data);
|
|
|
|
|
|
|
|
$this->_filename = $data;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-26 14:34:11 +01:00
|
|
|
// Ignore files with other extensions
|
|
|
|
if(pathinfo($this->_filename, PATHINFO_EXTENSION) != 'php') return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-26 14:34:11 +01:00
|
|
|
if (!is_readable($this->_filename)) {
|
|
|
|
require_once 'Zend/Translate/Exception.php';
|
2011-12-04 18:36:58 +01:00
|
|
|
throw new Zend_Translate_Exception('Error opening translation file \'' . $this->_filename . '\'.');
|
2011-11-26 14:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
global $lang;
|
|
|
|
if(!isset($lang['en_US'])) $lang['en_US'] = array();
|
|
|
|
// TODO Diff locale array to avoid re-parsing all previous translations whenever a new module is included.
|
|
|
|
require_once($this->_filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-26 14:34:11 +01:00
|
|
|
$flattened = array();
|
|
|
|
if($lang[$locale]) {
|
|
|
|
$iterator = new i18nSSLegacyAdapter_Iterator(new RecursiveArrayIterator($lang[$locale]));
|
|
|
|
foreach($iterator as $k => $v) {
|
|
|
|
$flattenedKey = implode($options['keyDelimiter'], array_filter($iterator->getKeyStack()));
|
|
|
|
$flattened[$flattenedKey] = (is_array($v)) ? $v[0] : $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($locale => $flattened);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toString() {
|
|
|
|
return "i18nSSLegacy";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFilenameForLocale($locale) {
|
2011-11-26 14:34:11 +01:00
|
|
|
return "{$locale}.php";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-26 14:34:11 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 05:12:47 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage i18n
|
|
|
|
*/
|
2011-11-26 14:34:11 +01:00
|
|
|
class i18nSSLegacyAdapter_Iterator extends RecursiveIteratorIterator {
|
|
|
|
|
|
|
|
protected $keyStack = array();
|
|
|
|
|
|
|
|
public function callGetChildren() {
|
|
|
|
$this->keyStack[] = parent::key();
|
|
|
|
return parent::callGetChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function endChildren() {
|
|
|
|
array_pop($this->keyStack);
|
|
|
|
parent::endChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function key() {
|
|
|
|
return json_encode($this->getKeyStack());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getKeyStack() {
|
|
|
|
return array_merge($this->keyStack, array(parent::key()));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|