Skip to Content

Setting/Getting Available Languages

The portal includes a portal service and API for obtaining the available portal languages:
source languages, target languages and personalization languages.
The portal’s Locale service controls the available languages. The available languages are determined
by the service’s configuration.
The service provides methods for obtaining the available languages. Obtain a reference to the service as follows:

ILocaleListService localeService = (ILocaleListService)PortalRuntime .getRuntimeResources().getService(ILocaleListService.KEY);

The Locale service is part of the wizard framework. Use the sharing reference:
com.sap.portal.admin.wizardframework and the JAR file
com.sap.portal.admin.wizardframework_api.jar.

Getting the Technical Names for Personalization Languages

The following gets the list of technical names of the languages that an anonymous user can select for a portal session:

private List getTechLanguagesName(IPortalComponentRequest request) {

// Get Locale service
ILocaleListService localeService = (ILocaleListService)PortalRuntime.getRuntimeResources().getService(ILocaleListService.KEY);

// Get current locale
Locale globalLocale = request.getLocale();

// Get technical names of locales
List allLocales = localeService.getListOfPersLocalesOrderedByDisplayName(globalLocale);
List allLocaleTechNames = localeService.getTechnicalNamesForLocales(allLocales);

returnallLocaleTechNames;
}

Getting the Display Names for Personalization Languages

The following gets the list of display names of the languages that an anonymous user can select for a portal session:

private List getDisplayLanguagesName(IPortalComponentRequest request) {
// Get Locale service ILocaleListService localeService = (ILocaleListService)PortalRuntime.getRuntimeResources().getService(ILocaleListService.KEY);

// Get current locale
Locale globalLocale = request.getLocale();

// Get display names of locales
List allLocales = localeService.getListOfPersLocalesOrderedByDisplayName(globalLocale); List allLocaleDisplayNames = localeService.getDisplayNamesForLocales(allLocales, globalLocale);

returnallLocaleDisplayNames;
}

Setting the Language for Anonymous Users

You can create a dropdown list to enable an anonymous user to select a language for the current session.
Create the list by getting the technical names and display names of the languages, as shown above.
Assuming that your user interface passes the technical name of the selected language in the parameter lang, the following code sets the language:

ILocaleListService localeService = (ILocaleListService)PortalRuntime.getRuntimeResources().getService(ILocaleListService.KEY);

// Get the technical name of the selected language
newLocale = localeService.getLocaleForTechnicalName(request.getParameter(“lang”));

// Set the language
HttpSession session = request.getServletRequest().getSession(); session.setAttribute(“sessionLocale”, newLocale);

This only sets the language for anonymous users. For authenticated users, the language is set by the portal personalization.

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. R. Kleijn
    On which ep version did you test this? I tried this functionality in EP7 without succes. There is going to be an alternative available in SP12 but for now we are on SP9 and i would like to add you code for language changes.
    (0) 
    1. Anonymous
      Hi,
      I am sorry but this is only available for SP10.
      Down porting the change is not so trivial and might cause problems, so this is not an option.

      Please try to upgrade to higher portal SP.

      Thanks and Regards,
      Erez

      (0) 
      1. Tandlum Suhash
        Hello Erez,
        The Blog was helpful.But I have a requirement to display english language as a default language for all users that includes authenticated users as well as anonymous users.How do I do it? and Where should I place the code?

        Regards,
        Suhash.

        (0) 
  2. Chris Hesse
    Thanks for the informative article – my question is, where do I insert this code?  For example, lets say I want to force the language choice for a particular user to only English or their local language based on the country they are in?  Is there a place where code like this would be executed?

    Thanks

    (0) 
    1. Anonymous
      Hi,
      Basically there are two ways to set this languages, if its something predefined for all users, there is no need for using this code and you can set the list by using the service manager.
      If you want to set the list dynamically for certain users, then using this code make sense, in this case you will have to put this code in a very early stage, for example in the heard iView, or any other custom iView that loads at start.

      Thanks and Regards,
      Erez

      (0) 

Leave a Reply