如何在view.yml中国际化诸如title之类的元组合?

时间:2011-01-17 14:04:26

标签: internationalization symfony1 symfony-1.4

我想把我的view.yml的字符串国际化,我找不到怎么做。

在我看来,我的解决方案很糟糕:

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

我想在不调用“sfConfig :: getInstance()”的情况下找到一种方法。有可能吗?

3 个答案:

答案 0 :(得分:2)

永远不要在配置文件中使用sfContext作为I18n!在这种情况下,请使用View(而不是控制器)中的setTitle函数

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

答案 1 :(得分:2)

由于include_title()没有翻译它在view.yml中找到的东西,我在自定义助手中创建了这个非常简单的函数:

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

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

然后我在layout.php文件中使用它:

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

这样,我可以在view.yml中使用翻译键

答案 2 :(得分:0)

您可以在行动中执行此操作:

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

我不认为有使用sfContext的方法。您可以通过删除布局/视图中的默认<?php include_title() ?>并使用模板i18n格式将其国际化来执行此类操作:

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