I have the following setup in my application Bootstrap.php
protected function _initTranslation()
{
$langPath = APPLICATION_PATH.'/languages/';
$translate = new Zend_Translate_Adapter_Gettext($langPath . 'site-ro.mo', 'ro');
$translate = new Zend_Translate_Adapter_Gettext($langPath . 'site-en.mo', 'en');
$translate->setLocale('en');
Zend_Registry::set('Zend_Translate', $translate);
}
and in my add.phtml file I have it like this
<label for="page_title" class="sr-only"><?= $this->translate("Page title") ?></label>
I know this works only if i have the setLocale to 'en' and if there exists an translation. But i don't know how to set for multiple translations and also not to throw errors if the .po file doesn't have a translation.
look at here:
Additional features for translation
You can always change your language in controller by these code :
$translate = Zend_Registry::get('Zend_Translate');
$translate->setLocale('ro');
after that your locale will be changed.
Related
I am unable to access zend session in included file via layout.
what i have done so far -
//bootstrap
public function _initSession()
{
Zend_Session::start();
$test = new Zend_Session_Namespace('test');
}
//controller
public function init(){
$test = new Zend_Session_Namespace('test');
$test->abc = 'defghi';
}
//layout include file
<?php include_once( APPLICATION_PATH . '/data/ga_test.php');?>
//ga_test.php
$test = new Zend_Session_Namespace('test');
echo 'this is ' . $test->abc;
I am not able to access the variable in ga_test file. I am getting an empty variable. But if I include ga_test end of each view file then it works. Obviously I don't want to go to every view file and include ga_test.php. Can I do this via layout.
I am sure, I am doing something wrong here. Any help would be really appreciated.
Thanks
I am trying to use Zend Frame work and Ext-Js4 together.
But I don't know how to setup file and folder structure correctly.
I setup like this,
And in application/controllers/IndexController.php
$this->view->headScript()->appendFile('/js/ext-4.0.7/ext-all.js','text/javascript');
$this->view->headScript()->appendFile('/js/app.js','text/javascript');
$this->view->headLink()->appendStylesheet('/js/ext-4.0.7/resources/css/ext-all.css');
Is it right structure? anyone has a better idea?
Thank you.
If you're using Ext-Js4 in your entire application, a better idea would be to add this in your bootstap, so that you don't have to include your javascript paths in every controllers.
protected function _initView()
{
$view = new Zend_View();
$view->headScript()->appendFile('/js/ext-4.0.7/ext-all.js','text/javascript');
$view->headScript()->appendFile('/js/app.js','text/javascript');
$view->headLink()->appendStylesheet('/js/ext-4.0.7/resources/css/ext-all.css');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
return $view;
}
I tend to use a layout for things like this:
function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('HTML4_STRICT');
$view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
->appendName('description', 'My App');
$view->headTitle()->setSeparator(' - ')
->headTitle('My App');
}
Then in my application.ini file I include:
resources.view[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts
There are always more ways to skin the proverbial cat!
I dont have an idea why translations are not working in with Zend_Form.
I would like to be able translate options for selects.
For now i have something like this:
my form class:
(...)
$this->translate = Zend_Registry::get('translate');
Zend_Form::setDefaultTranslator( Zend_Registry::get('translate') );
(...)
$select = new Zend_Form_Element_Select('select');
// $select->addMultiOption('0', $this->translate('Aktywny'));
$select->addMultiOption('0', $this->translate->_('Aktywny'));
$select->addMultiOption('1', 'Nieaktywny');
in my bootstrap file i have something like this:
protected function _initTranslate()
{
Zend_Loader::loadClass('Zend_Translate');
Zend_Loader::loadClass('Zend_Registry');
$translate = new Zend_Translate('gettext', APPLICATION_PATH.'/languages',
'browser',
array('scan' => Zend_Translate::LOCALE_FILENAME));
//changing language and setting it to session if changed
$session = new Zend_Session_Namespace('jezyk');
if(isset($session->language)) {
$translate->setLocale($session->language);
} else
$translate->setLocale('pl');
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}
and it works fine for controllers, phtml files and plugins where i call it by
$this->translate('string to translate');
and in plugins
$this->view->translate('string to translate');
but those methods won't work in form. It throws exception:
Warning: Exception caught by form: No entry is registered for key 'translate' Stack Trace: #0
to make it working as i wrote in comment just have to change line:
$this->translate = Zend_Registry::get('translate');
for
$this->translate = Zend_Registry::get('Zend_Translate');
cause i didn't saw that i'm getting wrong translate from registry. It should be Zend_Translate like in Bootstrap file, not translate as i did.
And this is solution for my problems with translate and now i can make translations in form files :)
I'm using application resource plugins in a .ini file to set up my Zend_Translate with this code:
resources.translate.data = APPLICATION_PATH "/../languages"
resources.translate.adapter = "gettext"
resources.translate.options.scan = "directory"
Now I would like to add the log functionality to the translate, which in bootstrap I would do like this:
$writer = new Zend_Log_Writer_Stream( APPLICATION_PATH . '/../logs/translate.log');
$log = new Zend_Log($writer);
$translate->setOptions(
array(
'log' => $log,
'logUntranslated' => true
)
);
2 questions about this:
First, is it possible to this in the .ini file?
Secondly, it is possible to "extend" resource settings in the bootstrap? In other words, could I add for example this log option in the bootstrap to the translate while maintaining the other settings already made in the .ini file?
Alright, I haven't found a solution to this in the ini file, but I have found a way to "extend" my settings from the ini file in the bootstrap without overwriting them. I managed to do that like this:
protected function _initTranslate()
{
$writer = new Zend_Log_Writer_Stream( APPLICATION_PATH . '/../somedir/somefile.log');
$log = new Zend_Log($writer);
// get the translate resource from the ini file and fire it up
$resource = $this->getPluginResource('translate');
$translate = $resource->getTranslate();
// add the log to the translate
$translate->setOptions(
array(
'log' => $log,
'logUntranslated' => true
)
);
// return the translate to get it in the registry and so on
return $translate;
}
This works just fine. I'm going to remove the translate from the .ini though because I'm switching to my own adapter and don't know (yet) how to pull that off from the ini.
I want a basic:
<input type="text" />
And I would like the default value to clear when the user puts in a value (kinda like this). It would be ideal if the default value returned onBlur.
I don't want the default value to be submitted if they leave it and click submit.
I'm generating the form using Zend, and imagine my solution can fit entirely into a Zend Form Decorator.
I can't find any existing ones, so I ask:
Do you have said decorator? Or something that will help me make one?
Just use corresponding jQuery plugins: defaultvalue
Ok, I've built a decorator which allows me to implement the jquery plugin Ololo posted.
It checks to see if the element has a Label set, and if it does, defaults to that:
require_once 'Zend/Form/Decorator/Abstract.php';
class Application_Form_Decorator_DefaultEnabledInput extends Zend_Form_Decorator_Abstract
{
private $attribs = array();
public function render($content)
{
$element = $this->getElement();
if(get_class($element) != 'Zend_Form_Element_Text') throw new Exception("Application_Form_Decorator_DefaultEnabledInput only works on text fields");
$element->setAttrib('type', 'text');
$element->setAttrib('name', htmlspecialchars($element->getName()));
$element->setAttrib('value', htmlspecialchars($element->getValue()));
$attribs = '';
$default = $element->getLabel();
if($default)
{
$element->setAttrib('rel', $default);
$element->setAttrib('title', $default);
$class = $element->getAttrib('class');
$element->setAttrib('class', "$class hasDefault");
$default = "";
}
foreach($element->getAttribs() as $key => $val) $attribs .= "$key='$val' ";
return "<input $attribs/>";
}
}
It allows me to define a default value in the form object (using setLabel).
$element = $this->createElement('text', 'suburb');
$element->setDecorators(array('DefaultEnabledInput'));
$element->setLabel('enter suburb here');
$this->addElement($element);
And all I need to do then is ensure that query and plugin are included on the page, and this piece of code:
$(document).ready(function() {
// default values
$('.hasDefault').each(function(){
$(this).defaultValue();
});
});
Then in the template, I can display the object like this:
<?= $this->form->getElement('suburb') ?>