Apr 1, 2010

APEX 4.0 - Language Derived from session

In APEX we have different ways of setting the language of our applications.  :

  • Browser language
             Use the language of the browser to determine the language of the application

  • FSP_LANGUAGE_PREFERENCE preference
              Use the APEX preference FSP_LANGUAGE_PREFERENCE to set the language of the application

  • FSP_LANGUAGE_PREFERENCE item
             Create an application item called FSP_LANGUAGE_PREFERENCE and set the language of the application by assigning the language value to this item.

To select the Application Language Derived From , go in the Shared Components - Edit Globalization Attributes.




FSP_LANGUAGE_PREFERENCE item is the option that we use the most. With that option you can create a button with a process to set the value of FSP_LANGUAGE_PREFERENCE to the language of your choice. We also use it to display the data in the correct language using v('FSP_LANGUAGE_PREFERENCE') in views.

When you use this method, there is a small problem. If you want to access a page directly via an URL (or create a bookmark) you end up in the application in the primary language on the first page view.

ex: http://host/apex/f?p=12870:1:0::::FSP_LANGUAGE_PREFERENCE:fr-ca
Doing so, brings you to the application and sets FSP_LANGUAGE_PREFERENCE but the page displays in the primary language (en-us in this case). That is because APEX sets the language of the application before it assign the value to the item FSP_LANGUAGE_PREFERENCE.
After the first page load, if the user navigates or submit the page, the application gets displayed in the language of the item FSP_LANGUAGE_PREFERENCE.


In APEX 4.0, there is a new way of setting the language of your application : SESSION




Using this option in "Application Language Derived From" , will let you specify the language of your application directly in the URL using &P_LANG  (Same principle as turning the Trace On by adding &P_TRACE=YES at the end of the URL).

Ex:
To access the application in french : http://tryapexnow.com/apex/f?p=12870:1:0&P_LANG=fr-ca
To access the application in english : http://tryapexnow.com/apex/f?p=12870:1:0&P_LANG=en-us 



There are also three new procedures  :
  • APEX_UTIL.SET_SESSION_LANG( p_lang in varchar2 )
  • APEX_UTIL.GET_SESSION_LANG return varchar2
  • APEX_UTIL.RESET_SESSION_LANG
Note that those procedures will work only when you use "SESSION" in the Application Language Derived From. APEX_UTIL.GET_SESSION_LANG won't return the language value of other language derivation methods.

If you want to get the session lang, you can also use the v('BROWSER_LANGUAGE') variable. It will show the APEX application language (Not the browser language). 


In my demo application, to switch from one language to another, I created a button "Switch Lang" that calls a PL/SQL process that does :

if nvl(apex_util.get_session_lang,'en-us') = 'en-us'
    then apex_util.set_session_lang('fr-ca');
    else apex_util.set_session_lang('en-us');
end if;

This is a great new feature in APEX 4.0 if you use the globalization a lot like we do. Thanks to Joel for helping me out on this blog post.