Skip to Content
Author's profile photo Former Member

Let a user change their portal language graphically

We’re going to make a couple of portal components to allow for dynamic language selection of the portal language. Of course, this assumes you have translated your portal content into the appropriate languages! To keep it simple, this example will limit itself to French, English and German, but the logic can be extended.

Because it’s going to be graphic based, first find some suitable image files to display to the user. Using Google images and searching for flags gave me the ones I use here:

image

We need to put these into our developer studio project – I keep mine under dist/images/mimes.

Our first component, which I have called PickLanguage, is used to display the images as anchored links that will then invoke the second component.

Here’s the code:

The href string points to my portal component, so if I change the name of the PAR file or component, I need to change this to the appropriate value.

The second component, which I have called ChangeLanguage, sets the user’s language and then launches the portal again…

Make an iView that launches the first component and give it to a portal role, and you should be finished.

As a caveat, you must have an environment in which users can change their portal language. If they can’t do this, then you need to find another way to achieve the dynamic selection.

Good luck!

Assigned Tags

      21 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      An excellent example.
      Very good for a flexible user development to deal with loacales.
      Thanks Michael.
      Author's profile photo Former Member
      Former Member
      Hi,

      Is there a requirement for a portalapps.xml for this deployment.  If you have a copy of this deployment is is possible to send it to me.

      kai@kaiandrachel.co.uk

      Many thanks....

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Yep, you always need a portalapp.xml file for a portal component to work. NWDS will make one for you, and the default worked for me.

      The important bit is:

       
           

      Cheers

      Author's profile photo Former Member
      Former Member

      Hello Michael,How to solve the issue ...?

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Unfortunately the Java standard set of locales does not include constants SPANISH or POLISH (or SWEDISH or...) You might be able to use something like:

      private static Locale POLISH = new Locale("pl", "PL");

      etc

      Author's profile photo Former Member
      Former Member
      Hi, Thanks Michael, your blog really helped me a lot...I am able to change the language of the portal. The only problem now remains is , the language changes for all the areas except fot the content area. There the contes are still displayed in English. Whereas teh same thing when done through the SAP personalize->language modify, after relogging into the portal, the content in the content area ia also changed to the newly selected language. Is there any workaround for this?

      Thanks in advance.

      Author's profile photo vaishali dhayalan
      vaishali dhayalan
      Hi Michael,Sonali,

      please can you tell me how you set the locale to spanish... I am unable to set the locale.

      I created a new locale as follows:

      private static Locale SPANISH = new Locale("es", "ES");

      and then set this
      if (language.equalsIgnoreCase("spanish"))
      modUser.setLocale(SPANISH);

      but its not setting the locale to spanish.
      Please can you let me know how to check this. I have a requirement to change the locale to spanish.

      Require help here.

      Regards,
      Vaishali

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Make sure the ?language parameter is set to spanish when you click the Spanish flag...
      Author's profile photo vaishali dhayalan
      vaishali dhayalan
      Hi Micheal,

      I have added the following code in PickLanguage.java

      response.write(
      ""); and also added a es.gif in imagesmimes folder

      when i click on the image in the url is

      http://ud1sap01pd1.bhpb.internal:50000/irj/servlet/prt/portal/prtroot/dynamicrole.changelanguage?language=spanish

      But still my user language remains english. The language changes only for the ones mentioned in the log.

      Please can you let me know where i am going wrong.

      Regards,
      Vaishali

      Author's profile photo vaishali dhayalan
      vaishali dhayalan
      Hi Micheal,

      I have added the following code in PickLanguage.java

      response.write(
      ""); and also added a es.gif in imagesmimes folder

      when i click on the image in the url is

      http://ud1sap01pd1.bhpb.internal:50000/irj/servlet/prt/portal/prtroot/dynamicrole.changelanguage?language=spanish

      But still my user language remains english. The language changes only for the ones mentioned in the blog.

      Please can you let me know where i am going wrong.

      Regards,
      Vaishali

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      I would suggest putting in some extra response.write statements to make sure you are really going through the correct logic path in ChangeLanguage...
      Author's profile photo vaishali dhayalan
      vaishali dhayalan
      Hi Michael,Sonali,

      please can you tell me how you set the locale to spanish... I am unable to set the locale.

      I created a new locale as follows:

      private static Locale SPANISH = new Locale("es", "ES");

      and then set this
      if (language.equalsIgnoreCase("spanish"))
          modUser.setLocale(SPANISH);

      But unfortunately the portal language is not set to Spanish.
      Please can you let me know how to check this. I have a requirement to change the locale to spanish.

      Require help here.

      Regards,
      Vaishali

      Author's profile photo Former Member
      Former Member
      Excelent blog .If you have a copy of this deployment is it possible to send it to me
      nancylam01@yahoo.com
      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Hi Nancy

      I don't have a par file for this - it ios a cut and paste from a larger set of code...

      Cheers

      Author's profile photo Former Member
      Former Member
      Hi, I've got this error when I click on a link to change the language:

      com.sapportals.portal.prt.runtime.PortalRuntimeException: iView not found: DynamicRole.ChangeLanguage

      this is the url:
      /irj/servlet/prt/portal/prtroot/DynamicRole.ChooseLanguage?language=english

      or

      /irj/servlet/prt/portal/prtroot/DynamicRole.ChooseLanguage?language=french

      or

      /irj/servlet/prt/portal/prtroot/DynamicRole.ChooseLanguage?language=spanish

      I've got the ChangeLanguage.java class in the .par file

      Thanks.

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Hi Jorge

      I'm not sure why the component DynamicRole.ChooseLanguage is being invoked. It should be DynamicRole.ChangeLanguage, as this is the name of your Java file.

      Is it possible you mistyped the name of the href variable in Pick.java?

      Cheers

      Author's profile photo Former Member
      Former Member
      Use:
      /irj/portal/prtroot/DynamicRole.ChangeLanguage?language=english

      Instead:
      "/irj/servlet/prt/portal/prtroot/DynamicRole.ChangeLanguage?language=english

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      We were posting at the same time!
      Author's profile photo vaishali dhayalan
      vaishali dhayalan
      Hi Micheal,

      I am getting the follwing error:

      com.sapportals.portal.prt.runtime.PortalRuntimeException: iView not found: DynamicRole.PickLanguage

      Please can you guide me regarding this.

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      What did you call your NWDS project? I called mine DynamicRole, which is why the URL is DynamicRole.PickLanguage. Replace DynamicRole with your project nsme and see what happens.
      Author's profile photo Former Member
      Former Member

      Hi Michael,

      I have similar requirement of creating links for different languages in masthead area. Can you give detail code.