How to internationalize metas such as title in vie

2020-08-01 05:44发布

问题:

I'd like to internationalize the strings of my view.yml, I can't find how to do this.

I have a solution that is bad, in my opinion:

metas:
  title: <?php echo sfContext::getInstance()->getI18n()->__('TITLE'); ?>

I'd like to find a way to do it without calling "sfConfig::getInstance()". Is it possible?

回答1:

Never ever use sfContext for I18n in Configuration-Files! In such a case use the setTitle function in the View (not the controller)

<?php $sf_response->setTitle(__('TITLE'));?>


回答2:

Since include_title() does not translate what it finds in view.yml, I made this very simple function in my custom helper:

function include_translated_title($context)
{
  $title = $context->getI18N()->__($context->getResponse()->getTitle());

  echo content_tag('title', $title)."\n";
}

Then I use it in my layout.php files:

<head>
<?php include_http_metas() ?>
<?php include_metas() ?>
<?php include_translated_title($sf_context) ?>

This way, I can use translation keys in my view.yml



回答3:

You can do this in your action instead:

$this->getResponse()->setTitle(sfContext::getInstance()->getI18n()->__('TITLE'));

I dont think there's a way around using sfContext. You might be able to do something like this by getting rid of the default <?php include_title() ?> in your layout/view and the using the template i18n format to internationalise it:

<title><?php echo __('TITLE') ?></title>