Gc\Core\Translator::generateCache PHP Method

generateCache() public method

Generate php array file as cache
public generateCache ( ) : void
return void
    public function generateCache()
    {
        $values = $this->getValues();
        $data = array();
        foreach ($values as $value) {
            if (empty($data[$value['locale']])) {
                $data[$value['locale']] = array();
            }
            $data[$value['locale']][$value['source']] = $value['destination'];
        }
        $translatePath = GC_APPLICATION_PATH . '/data/translation/%s.php';
        $templateContent = file_get_contents(GC_APPLICATION_PATH . '/data/install/tpl/language.php.tpl');
        foreach ($data as $locale => $values) {
            file_put_contents(sprintf($translatePath, $locale), sprintf($templateContent, var_export($values, true)));
        }
    }

Usage Example

Example #1
0
 /**
  * List and edit translation
  *
  * @return \Zend\View\Model\ViewModel|array
  */
 public function indexAction()
 {
     $translationForm = new Form\Translation();
     $translationForm->prepareForm($this->getServiceLocator()->get('Config'));
     $translationForm->setAttribute('action', $this->url()->fromRoute('content/translation'));
     if ($this->getRequest()->isPost()) {
         $post = $this->getRequest()->getPost();
         if (empty($post['source']) or empty($post['destination'])) {
             return $this->redirect()->toRoute('content/translation');
         }
         $translator = new Translator();
         foreach ($post['source'] as $sourceId => $source) {
             $translator->update(array('source' => $source), sprintf('id = %d', $sourceId));
             if (!empty($post['destination'][$sourceId])) {
                 $translator->setValue($sourceId, $post['destination'][$sourceId]);
             }
         }
         $translator->generateCache();
         $this->flashMessenger()->addSuccessMessage('Translation saved !');
         return $this->redirect()->toRoute('content/translation');
     }
     $translator = new Translator();
     return array('form' => $translationForm, 'values' => $translator->getValues());
 }