PHP code example of residit / gettexttranslator

1. Go to this page and download the library: Download residit/gettexttranslator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

residit / gettexttranslator example snippets


class BasePresenter extends Nette\Application\UI\Presenter
{
  /** @persistent */
  public $lang;

  /** @var \GettextTranslator\Gettext */
  protected $translator;


  /**
   * @param \GettextTranslator\Gettext
   */
  public function injectTranslator(GettextTranslator\Gettext $translator)
  {
    $this->translator = $translator;
  }


  public function createTemplate($class = NULL)
  {
    $template = parent::createTemplate($class);

    // if not set, the default language will be used
    if (!isset($this->lang)) 
    {
      $this->lang = $this->translator->getLang();
    } 
    else 
    {
       $this->translator->setLang($this->lang);
    }

    $template->setTranslator($this->translator);

    return $template;
  }
}

protected function createComponentMyForm()
{
  $form = new Form;
  $form->setTranslator($this->translator);

  // ...

  return $form;
}

public function createTemplate($class = NULL)
{
  $template = parent::createTemplate($class);
  $template->setTranslator($this->parent->translator); // $translator in presenter has to be public
  // or $this->translator via construct/inject

  return $template;
}