在Zend Framework 2中获取布局或视图中的局部值 [英] Get local value in layout or view in Zend Framework 2
问题描述
如何在layout.phtml或Zend Framework 2中的视图中获取本地值(即:"en"或"en_US","de"等)?
How can we get local value (i.e: 'en' or 'en_US', 'de' etc) in layout.phtml or views in Zend Framework 2?
我的本地设置与此处解释
<?php
namespace FileManager;
use Zend\Mvc\ModuleRouteListener;
class Module
{
public function onBootstrap($e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator
->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale('en_US');
}
//...
}
我想获得像这样的当地价值:
I want to get local value something like this:
$locale = $this->translate()->getLocale(); // <-- It's not working anyway
在调用google map api url以获得匹配的语言环境/语言时,我需要使用" $ locale ".我在整个layout.phtml
I need to use '$locale' it while calling google map api url to get matched locale/language. I'm calling it throughtout the application in layout.phtml
$this->headScript()->appendFile('http://maps.googleapis.com/maps/api/js?language=' . $locale);
所以我想在调用api时使语言选项动态化.
So I want to make language option dynamic while calling api.
PS:我没有任何查询字符串参数,例如'language',这是我需要在脚本url中设置的google api内容(如果您不知道的话),请不要感到困惑. 在此处
PS: I don't have any query string parameter such as 'language', It's a google api thing which I need to set in script url (if you don't know) Please don't get confused. Not answered here
推荐答案
取决于要从何处获取Locale值.无论如何,您都可以在控制器中执行此操作,例如:
Depends on where you want to get the Locale value from. In any case, you can do it in your controller, e.g.:
$locale = $this->request->getQuery('language');
$this->layout()->locale = $locale;
或
return new ViewModel(array('locale' => $locale));
编辑,如果您只想从翻译器中获取语言环境,则可以在查看脚本中尝试以下操作:
Edit if you just want to get the locale from the translator, you can try this in view script:
$this->plugin('translate')->getTranslator()->getLocale();
这篇关于在Zend Framework 2中获取布局或视图中的局部值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!