Internationalisation/Translation of Page Title

Hi,

The default page title set by the Controllers, using the Controller id and the action, are not translatable, so they are not using Yii::t(), is this correct? Is there a reason why this is not done?

To achieve this I would have to extend an own base Controller class whith it’s own getPageTitle method, which uses Yii::t() then.

Regards

Paul.

I believe this is not done because you are responsable for setting the title of the pages, yii can only guess and there’s no way to yii automatically translate guessed titles, as they are application specific

extend it or use


$this->setPageTitle(Yii::t('app','my page'));

Of course it is translatable. I solved it overriding the getPageTitle() and setPageTitle() methods in my own Controller base class like this:


private $_pageTitle;

public function getPageTitle()

{

	if($this->_pageTitle!==null) {

		return Yii::t('wm', $this->_pageTitle);

	} else {

		$controller = Yii::t('wm', ucfirst(basename($this->getId())));

		

		if($this->getAction()!==null && strcasecmp($this->getAction()->getId(),$this->defaultAction)) {

			$action = Yii::t('wm', ucfirst($this->getAction()->getId()));

			return $this->_pageTitle=Yii::app()->name.' - '.Yii::t('wm', '{action} {controller}', array('{action}' => $action, '{controller}' => $controller));

		} else {

			return $this->_pageTitle=Yii::app()->name.' - '.$controller;

		}

	}

}

Then, in my translations file de/wm.php, I have, between all translations for the controller IDs and actions, this lines:


return array(

    'Comment' => 'Kommentar',

    'Create' => 'Erstellen',

    'Delete' => 'Löschen',

    'Update' => 'Bearbeiten',

    '{action} {controller}' => '{controller} {action}', // This is flipped in German: "subject verb"

);

This allows me to translate a automatic guessed page title like ‘Create Comment’ (en) to ‘Kommentar Erstellen’ (de).

For the spanish version it would look like this:


return array(

    'Comment' => 'Comentario',

    'Create' => 'Crear',

    'Delete' => 'Borrar',

    'Update' => 'Editar',

    '{action} {controller}' => '{action} {controller}', // same syntax as in English: "verb subject"

);

Of course, there are still cases where I have to adjust the page title manually in the controller, but for a lot of typical CRUD operations, this works great.

Regards

Paul.

I think this could be good to implement in Yii2 by default.

Why I am not able to customize the page title from the view?

I need some default translated titles based on the action and controller names but where needed to set the title from the view.