Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
HELP -- Change language in Before Method
  • Hi all !

    I have some trouble with Fuelphp 1.5.1, in 1.3 everythings work great.
    I try to change dynamicaly the language.

    when i do In my view :

    Config::set('language', 'fr');
    Lang::load('welcome');

    It's ok !

    But when i try to do the samething in my Controller_Template in the before mehod,
    Lang::get('something') return NULL in my view....

    Anybody have a idea ? it's weird :/
  • i donnnn't understaaaand :D

    ------------------------------------------------------------------------------------------------------------
    class Controller_Welcome extends Controller_Template
    {
      public function before()
      {
      parent::before();
      (Session::get('lang')) or Session::set('lang', 'fr');
      Config::set('language', Session::get('lang'));
      Lang::load('welcome');
      }

      public function action_changelang($lang = 'fr')
      {
      Session::set('lang', $lang);
      Response::redirect(Uri::base());
      }

      public function action_index()
      {
        $this->template->content = \View::forge('welcome/index');
      }
    ------------------------------------------------------------------------------------------------------------

    In my :: template.php :  Lang::get('title_logo') == NULL
    In my view :: welcome/index.php : Lang::get('title_logo') == SOMESTRING!!!

    need some logic :)


  • You should not use (framework) logic in your views. Views are presentation templates.

    Views by default are lazy rendered, which means they are only rendered long after your code has finished. So you can not assume that code in your view runs the moment you create the view.

    As for language, the View object stores the language active when the View object is created. In your case, because you use the Template controller, the template View is created by calling parent::before(). Which is before you switch languages.

    So, make sure you have the correct language set before your code runs. I do mine in the config file, so my controllers don't have to bother with language issues, they just use whatever language is set.

    If you insist doing it this way, make sure the language is set correctly before you create your View objects. In the case of the template, do that before you call parent::before().
  • Thanks a lot!

    It's working when i do that before parent::before().

    But i want to understand how you can do that in the config file. I try to put this code in it.
    But nothing append, Any language is loaded.

    Thanks again.
  • HarroHarro
    Accepted Answer
    In your app/config/config.php, add

        'language'           => \Session::get('lang', 'fr'), // Get the language from the session

    Which will fetch the language from the session, and if not set, will set it to 'fr'.

  • Cool !!

    Thanks, thanks ! !

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion