Quick links: Content - sections - sub sections
EN

Trace: autolocale

Wiki: Sitemap - Recent Changes - Back link

Coord Plugin "autolocale"

(supported version Jelix 1.3.x)

Jelix and your portal in the user's language

I'm not going to repeat the documentation of the plugin coord “autolocale”, plugin which permits to manage automatically the internationalisation of your templates.

The main subject of this article will consist to show how with Jelix, jAuth and jCommunity in particular, we can change the user's language and thus get the pages for his language

So jCommunity, provides event, and one that permits to save modifications on the user's account, this event is jcommunity_save_account.

In the class of my listener authhavefnubb.listener.php I will have what is below for each modification

   function onjcommunity_save_account ($event) {
        global $gJConfig;
	// get the data put in my form
        $form = $event->getParam('form');
        if ( $form->getData('member_language') != '') {
            $_SESSION['JX_LANG'] = $form->getData('member_language');
            $gJConfig->locale = $form->getData('member_language');
        }
	// a little message which say the profile is updated
        jMessage::add(jLocale::get('havefnubb~member.profile.updated'),'ok');
   }

The trick here, is to put $gJConfig→locale = $form→getData('member_language'); before the jMessage::add , so the message which comes just after, will be translated immediatly in the choosen language, otherwise, this one will appear again in the language previously choosen or the language of the portal

Then we act the same way with the events of jAuth which are AuthLogin (when logged in) and AuthLogout (when logged out)

function onAuthLogin ($event) {
   ...
    $_SESSION['JX_LANG'] = $user->member_language;
    $gJConfig->locale = $user->member_language;
}
 
function onAuthLogout ($event) {
       // remove the language from the current session to get the portal's one
        $_SESSION['JX_LANG'] = '';
        unset($_SESSION['JX_LANG']);
}

and we will define our events.xml file as follow :

  <listener name="authhavefnubb">
      <event name="AuthLogin" />
      <event name="AuthLogout" />
      <event name="jcommunity_save_account" />
   </listener>

Now, with more or less of 10 lines in a listener and 3 XML nodes, we have permitted to all the user of our website to have the pages in his favorite language

en/tutorials/plugins/coord/autolocale.txt · Last modified: 2012/01/31 11:34 by foxmask
Recent changes RSS feed Creative Commons License