I am creating a theme for wordpress and I would like it to be multilanguage. I am using this wordpress function: link that allows you to translate texts.
The problem comes in the use of this class:
class haceTanto extends DateTime {
protected $strings = array(
'y' => array(__('1 year', 'mytheme'), __('%d years', 'mytheme') ),
'm' => array(__('1 month', 'mytheme'), __('%d months', 'mytheme') ),
'd' => array(__('1 day', 'mytheme'), __('%d days', 'mytheme') ),
'h' => array(__('1 hour', 'mytheme'), __('%d hours', 'mytheme') ),
'i' => array(__('1 minute', 'mytheme'), __('%d minutes', 'mytheme') ),
's' => array(__('1 second', 'mytheme'), '%d seconds'),
);
public $profundidad;
public function __construct( $fecha,$profundidad='i')
{
parent::__construct( $fecha );
$this->profundidad = $profundidad;
}
public function __toString() {
try
{
$now = new DateTime('now');
$diff = $this->diff($now);
$text = '';
foreach($this->strings as $key => $value){
$dato=$this->getDiffText($key, $diff);
if($dato!=null ){ $text .= ' '.$dato; break; }
if( ($text .= ' '.$this->getDiffText($key, $diff)) ){
}
if($this->profundidad == $key) break;
}
return $text;
}
catch(Exception $e)
{
trigger_error($e->getMessage(), E_USER_ERROR);
return '';
}
}
protected function getDiffText($intervalKey, $diff){
$pluralKey = 1;
$value = $diff->$intervalKey;
if($value > 0){
if($value < 2){
$pluralKey = 0;
}
return sprintf($this->strings[$intervalKey][$pluralKey], $value);
}
return null;
}
}
As you can see, I use __ in the array
'y' => array(__('1 year', 'mytheme'), __('%d years', 'mytheme') ),
but gives this error:
Fatal error: Constant expression contains invalid operations in C: \ laragon \ www \ mul \ wp-content \ themes \ mytheme \ inc \ class.time.php on line 5
This I guess it is, because the class below uses sprintf
return sprintf($this->strings[$intervalKey][$pluralKey], $value);
And that's why the problem but I do not know how to solve it in order to translate all the chains of year, month, day ... in the plural and in the singular.
I have tried to explain myself as well as possible, I hope someone can help me.