thomasW wrote:
Mixing calculations and translations for the whole application within one
class is really a bad coding standard.
This opens many other problems.
To get you the point why static:
$my = new SomeObject();
$other = $my;
$other->_('translation');
Now you're broken again.
Using a static function you are not error-free but you limit possible
problems.
Also ::_ can be searched which would be impossible when you extend your
SomeObject.
Still:
In my eyes this behaviour is completly useless and unnecessary.
It's better to stick with what's available because there is always a good
reason why a implementation works this way and not different.
I think my original example might have been confusing and be leading you astray, so I'll try and clarify my object.
One example that I'm working on is Exceptions. E.g. I want to be able to write,
throw new MyProject_Exception_NotSupported('Adapter does not implement this method');
I would prefer NOT to write:
// get the default translator
$translate = Zend_Registry('Zend_Translate');
throw new MyProject_Exception_NotSupported(
$translate->_('Adapter does not implement this method')
);
In Zend_Validate_Abstract (ZF 1.7, lines 185-210), the _createMessage function basically does the same thing I'm looking to do. i.e. embed a translate instance inside the object. So that it doesn't Translate immediately but only when called.
I think it's neater than having to translate the exception string explicitly as in the second example.
Reproducing code form zend framework below:
/**
* Constructs and returns a validation failure message with the given message key and value.
*
* Returns null if and only if $messageKey does not correspond to an existing template.
*
* If a translator is available and a translation exists for $messageKey,
* the translation will be used.
*
* @param string $messageKey
* @param string $value
* @return string
*/
protected function _createMessage($messageKey, $value)
{
if (!isset($this->_messageTemplates[$messageKey])) {
return null;
}
$message = $this->_messageTemplates[$messageKey];
if (null !== ($translator = $this->getTranslator())) {
if ($translator->isTranslated($message)) {
$message = $translator->translate($message);
} elseif ($translator->isTranslated($messageKey)) {
$message = $translator->translate($messageKey);
}
}
if ($this->getObscureValue()) {
$value = str_repeat('*', strlen($value));
}
$message = str_replace('%value%', (string) $value, $message);
foreach ($this->_messageVariables as $ident => $property) {
$message = str_replace("%$ident%", (string) $this->$property, $message);
}
return $message;
}
Then if you do use the above method of embedding the translator instance in the object, you need to extract the strings from the code to build up your string translation table. From here you can opt for scanning the source code or else using your runtime logging of untranslated strings.
Hopefully that was a clearer example of what I am trying to achieve.
-Axel