Oracle APEX – How to remember user’s selected language on Login page

Purpose of this blog is to learn and find a way on how to remember user’s last selected language and render Login page on a user’s last selected language. Generally, we know the user’s selected language post login screen as we store it in database. So, generally we load login page with application default primary language.

Trick here is to use JavaScript Cookies to store last user’s last selected language. We will use apex.storage namespace. Following is the two JavaScript/PL SQL function which can be used to achieve this.

JavaScript Function
===================
function setLanguage(pLang){
  apex.storage.setCookie("USER_LANG", pLang);
}

PL/SQL Function
===============
FUNCTION getCookieValue(
  p_name IN VARCHAR2
) RETURN VARCHAR2 AS

l_names owa_cookie.vc_arr;
l_vals owa_cookie.vc_arr;
l_num_vals INTEGER;
l_return_value varchar2(4000);

BEGIN

owa_cookie.get_all(
	names => l_names,
	vals => l_vals,
	num_vals => l_num_vals
);

FOR r IN 1 .. l_names.COUNT LOOP
	if l_names(r) = p_name THEN
		l_return_value := l_vals(r);
		exit;
	end if;
END LOOP;

RETURN l_return_value;

END getCookieValue;

On change/switch language page, use dynamic action to set values into cookies. We can use setLanguage function to achieve this.

setLanguage(apex.item("P11_LANGUAGE").getValue());

This will show you USER_LANG cookie value inside Storage section of your browser Developer Toolbar.

Next step is to load login page on Cookie language value. For this, we need to create ON LOAD before header branch on login page (9999) as below.

Do not forget to create new Application Item = APP_LANG_REQUEST

Name = Set Language
Point = Before Header
Type = Function Returning URL (Redirect)
Language = PL/SQL
**************************************************************************
DECLARE
    l_redirect_url VARCHAR2(100);
    l_browser_lang varchar2(2) := getCookieValue(p_name => 'USER_LANG');    
BEGIN

  IF APEX_UTIL.GET_SESSION_LANG is null then
    if l_browser_lang is null then
        select substr(owa_util.get_cgi_env('HTTP_ACCEPT_LANGUAGE'), 1, 2)
          into l_browser_lang
          from dual;
    end if;
    
    if l_browser_lang IS NOT NULL then

        :APP_LANG := l_browser_lang;
        APEX_UTIL.SET_SESSION_LANG(P_LANG => l_browser_lang);

        l_redirect_url := APEX_PAGE.GET_URL(p_page => 9999);

        :APP_LANG_REQUEST := 'LANG_SET';
       
        return l_redirect_url;
    end if;
  end if;  
end;
**************************************************************************

Condition Type = Item is NULL
Item = APP_LANG_REQUEST

Your implementation might be different based on your use case and implementations. Purpose here is to highlight some way/thoughts to achieve this.

Hope that helps!

Regards,
Jaydip Bosamiya
jbosamiya@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *