title: Zend Framework 2 passing variable to models
body: <p>I'm currently developing a multilangual website. For the multilangual part i use translator / poedit. I store the selected language in session. It works fine.</p>
  
  <p>Module.php:</p>
  
  <pre><code>public function onBootstrap(MvcEvent $e)
  {
      // ...
  
      $session = new Container('base');
  
      if ($session-&gt;language !== NULL) {
          $e-&gt;getApplication()-&gt;getServiceManager()-&gt;get('translator')-&gt;setLocale($session-&gt;language);
      }
  }
  </code></pre>
  
  <p>Action for setting language in a controller:</p>
  
  <pre><code>public function setLanguageAction()
  {
      $language = $this-&gt;params()-&gt;fromRoute('language');
  
      if (isset($this-&gt;languages[$language])) {
  
          $session = new Container('base');
  
          if ($language == 'en') {
              $session-&gt;language = NULL;
          } else {
              $session-&gt;language = $language;
          }
      }
  
      return $this-&gt;redirect()-&gt;toRoute('home');
  }
  </code></pre>
  
  <p>In module.config.php the default locale is set to en.</p>
  
  <p>As i said everything works fine, except for one thing.</p>
  
  <p>I store some language dependent data in DB too, so in my models i need to know what the current language is. The current language is needed for other purposes in the models too.</p>
  
  <p>So i include the following code in every model's construct function:</p>
  
  <pre><code>$session = new Container('base');
  
  if ($session-&gt;language !== NULL) {
      $this-&gt;language = $session-&gt;language;
  } else {
      $this-&gt;language = 'default';
  }
  </code></pre>
  
  <p>I think it's not the best solution. I have too much models to always include this code.</p>
  
  <p>I would like to know if there is a solution to pass a $language variable automatically to all of my models for example from Module.php / getServiceConfig function:</p>
  
  <pre><code>public function getServiceConfig()
  {        
      $session = new Container('base');
  
      return array(
          'factories' =&gt; array(
              'Application\Model\SomeThing' =&gt; function($sm) {
                  $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                  $c = new SomeThing($dbAdapter);
                  $c-&gt;language = $session-&gt;language;
                  return $c;
              }
          )
      );
  }
  </code></pre>
  
  <p>Of course this is not working, but it would be great to be able to do something similar like this, or a more general solution, where it is not necessary to assign the value of the current language to every model's language variable in the factory array (for example a common bootstrap for models, where this assignment can be done for all models in one place).</p>
  
  <p>Is there a solution for my problem?</p>
  
  <p>Thank you for your help!</p>
  
  <p>M </p>