Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to change language dynamically by user?
  • Hi,

    what is the best way to let the user change the language of the site himself?

    I want to create some flags or link to change the language.

    But how can I do that? Do I need to overwrite some Configuration in the session or what?

    Thanks
    Kay
  • Hi

    I would look to tackle this via a few different methods. I've done something similar in the past and this was achieved by doing the following;

    • Store the users language in their settings as default. 
    • Storing the current users language in the session.

    When someone clicks a flag or dropdown for the language have a controller change the language and store the session. If the user hasn't changed the language and they're not logged in, use the sites default, however if they're logged in and have a language set, revert to that one. You could make it easier for you and store the language session of the current user when they login so you don't have to be too complicated with your script.

    Either way is up to you. That's what I did previously and it worked a treat.


    J
  • You can also use a separate cookie, so that the language 'sticks' when the user logs out.
  • Storing the language for a user would be my second thing, but how can i change it just for the one who is actually viewing the site?
  • \Config::set('language', $myspeciallanguage);

  • Sorry, I seem to have a big wall before my head...

    I have made an Lang Controller like
    class Controller_Lang extends Controller_Base {

    public function action_change()
    {
    $actual_lang = Uri::segment(3);
    Debug::dump($actual_lang); #die;
    Config::set('language', $actual_lang);
    Response::redirect('admin/users');
    }

    }
    I call this with a DropDown like

    echo \Uri::base(false).'lang/change/de';
    When I dump
    $actual_lang
    it shows the correct value, but this is not stored in Config.
    What do I wrong?

    Thanks for our help...


  • You only set it at runtime, it is not written to the config file.

    And you don't want that, because it's runtime data, and potentionally different for every user using your application. While the code in your config is system wide, and for every request. So you use it as a default or fallback language.

    So what you need to to do here in this controller is to:
    - store it in a separate language cookie (sticky between sessions, even if logged out)
    - or store it in the session (disappears when the session is destroyed)
    - or store it in the user record (only for logged in users, not kept if logged out)

    Then when a page is requested, in your base controller (either in _init() or in the before() method), you retrieve the language code you stored (if possible), and use it to tell FuelPHP which language files to use.

    And that you do using \Config::set('language', $retrieved_language);

Howdy, Stranger!

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

In this Discussion