Translate fix
This commit is contained in:
parent
b0a13c0a3c
commit
8b2e698525
5 changed files with 127 additions and 4 deletions
|
|
@ -153,8 +153,14 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
*/
|
||||
protected function _initTranslate()
|
||||
{
|
||||
$this->bootstrap('autoloader');
|
||||
|
||||
$translate = new Zend_Translate('ini', APPLICATION_PATH . '/languages/se.ini', 'sv');
|
||||
$translate = new Fiktiv_Translate(array(
|
||||
'adapter' => 'Fiktiv_Translate_Adapter_Ini',
|
||||
'locale' => 'sv',
|
||||
'content' => APPLICATION_PATH . '/languages/se.ini'
|
||||
));
|
||||
|
||||
$translate->addTranslation(APPLICATION_PATH . '/languages/en.ini', 'en');
|
||||
|
||||
Zend_Form::setDefaultTranslator($translate);
|
||||
|
|
|
|||
|
|
@ -2,20 +2,20 @@
|
|||
<config>
|
||||
<navigation>
|
||||
<start>
|
||||
<label>home</label>
|
||||
<label>u:home</label>
|
||||
<controller>index</controller>
|
||||
<action>index</action>
|
||||
<module>default</module>
|
||||
<pages>
|
||||
<blog>
|
||||
<label>blog</label>
|
||||
<label>u:blog</label>
|
||||
<controller>index</controller>
|
||||
<action>index</action>
|
||||
<module>blog</module>
|
||||
<route>default</route>
|
||||
</blog>
|
||||
<about>
|
||||
<label>about</label>
|
||||
<label>u:about</label>
|
||||
<controller>index</controller>
|
||||
<action>about</action>
|
||||
<module>default</module>
|
||||
|
|
|
|||
7
library/Fiktiv/Translate.php
Normal file
7
library/Fiktiv/Translate.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Fiktiv_Translate extends Zend_Translate
|
||||
{
|
||||
}
|
||||
74
library/Fiktiv/Translate/Adapter.php
Normal file
74
library/Fiktiv/Translate/Adapter.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
/**
|
||||
* Extended Zend_Translate_Adapter with additional functionality
|
||||
*
|
||||
*/
|
||||
abstract class Fiktiv_Translate_Adapter extends Zend_Translate_Adapter
|
||||
{
|
||||
/**
|
||||
* Performs the translation and call the transform
|
||||
* function to handle transformations.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function translate($messageId, $locale = null)
|
||||
{
|
||||
if ($messageId == null)
|
||||
return;
|
||||
|
||||
$options = func_get_args();
|
||||
array_shift($options);
|
||||
|
||||
$count = count($options);
|
||||
$locale = null;
|
||||
if ($count > 0) {
|
||||
if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
|
||||
$locale = array_pop($options);
|
||||
}
|
||||
}
|
||||
|
||||
if ((count($options) === 1) and (is_array($options[0]) === true)) {
|
||||
$options = $options[0];
|
||||
}
|
||||
|
||||
if ($options[count($options)-1] === true) {
|
||||
foreach ($options as &$opt) {
|
||||
$opt = parent::translate($opt, $locale);
|
||||
}
|
||||
}
|
||||
|
||||
if (stristr($messageId,':')) {
|
||||
|
||||
$parts = explode(':', $messageId);
|
||||
$flag = $parts[0];
|
||||
$key = $parts[1];
|
||||
return $this->transform($flag, parent::translate($key, $options));
|
||||
}
|
||||
|
||||
return parent::translate($messageId, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the translated text.
|
||||
* This function defines the transfomation flags.
|
||||
*
|
||||
* @param string $flag
|
||||
* @param string $text
|
||||
* @return string $text
|
||||
*/
|
||||
protected function transform($flag, $text)
|
||||
{
|
||||
switch ($flag) {
|
||||
case 'u':
|
||||
return ucfirst($text);
|
||||
break;
|
||||
case 'uw':
|
||||
return ucwords($text);
|
||||
break;
|
||||
case 'ua':
|
||||
return strtoupper($text);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
36
library/Fiktiv/Translate/Adapter/Ini.php
Normal file
36
library/Fiktiv/Translate/Adapter/Ini.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Fiktiv_Translate_Adapter_Ini extends Fiktiv_Translate_Adapter
|
||||
{
|
||||
protected $_translateAdapter = null;
|
||||
|
||||
public function __construct($options = array()) {
|
||||
$this->_translateAdapter = new Zend_Translate_Adapter_Ini($options);
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
public function __call($name, $args) {
|
||||
return call_user_func_array(array($this->_translateAdapter, $name), $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just wrap Zend_Translate_Adapter_Ini
|
||||
*/
|
||||
protected function _loadTranslationData($data, $locale, array $options = array())
|
||||
{
|
||||
return $this->_translateAdapter->_loadTranslationData($data, $locale, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the adapters name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return "Ini";
|
||||
}
|
||||
}
|
||||
Reference in a new issue