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

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

             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).

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
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.


Olivier Dupont said...

Haven't noticed that yet, thanks for pointing it out!

Wannderer said...

Do you still have the Demo App ? Would like to know how where you created that button. I am trying to add a language toggle to the navigation bar.

Francis said...

The demo application is not available anymore... didn't had time to recreate it. Sorry.

But, if you want to do a language switch, simply add a link in the navbar that branches to a "dummy" page with the REQUEST = BRANCH_TO_PAGE_ACCEPT
and in that page, just add a plsql process with the code mentioned above : 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;

and branch back to the calling page.



antoniosanchezca said...

How I can translate the file browser button and the legend that follows?
I used Apex 4.2

yk said...

Do we have any workaround for the first time application showing up in primary language? I would like to see application in language set at browser level.. Browser language is used by Apex 4.2..Is there any bug in 4.2? As workaround I set session language which works good but at first instance application appears in English( primary lang)

Francis said...

The "Browse" button cannot be translated. It will use the browser to define the language.

Francis said...

To see the first call to a page in a specific language other than the primary language, you have to use the &P_LANG= parameter in the URL as mentioned in this post.