This discussion is archived
7 Replies Latest reply: Jan 30, 2013 7:23 AM by 793718 RSS

WebCenter Portal programmatic localization

793718 Newbie
Currently Being Moderated
Greetings!

I am trying to programmatically implement localization in an ADF application that uses WebCenter Portal.
I am using an <af:selectOneRadio> component to change the languages. I store the selected language in a session scope manage bean and then use it on every page - <f:view locale="localeManager.locale">
This does not translate WebCenter Portal components however. In particularly, the navigation model. The model uses the language of the browser.
Is there a way to tell WebCenter to use the locale from my LocaleManager manage bean?
Here is my changeLocale() method:
  private void changeLocale(String language) {
    this.locale = language;
    Locale newLocale = new Locale(this.locale);
    FacesContext context = FacesContext.getCurrentInstance();
    context.getViewRoot().setLocale(newLocale);
  }
Again, this successfully translates all the labels from resource bundles.

Any help is appreciated.
Respectfully,
Andrew
  • 1. Re: WebCenter Portal programmatic localization
    Blueberry Coder Journeyer
    Currently Being Moderated
    Hi.

    What you need to do is to create a custom JSF view handler (extending <tt>javax.faces.application.ViewHandler</tt>), and override this method:
    Locale calculateLocate(FacesContext context)
    . There, you can retrieve the value for the selected locale and force it on the whole application.

    The Javadocs for http://docs.oracle.com/cd/E17802_01/j2ee/j2ee/javaserverfaces/1.2/docs/api/javax/faces/application/ViewHandler.html

    Once you have coded your handler, you plug it in <tt>faces-config.xml</tt>.

    Best Regards,

    Frédéric.
  • 2. Re: WebCenter Portal programmatic localization
    Dimitar Dimitrov Expert
    Currently Being Moderated
    Assuming that your bean is session scoped, it's property "locale" has been properly initialized (e.g. it contains a java.util.Locale object, but not a string signature of a locale) and the bean is exposed via the name "localeManager", then the correct syntax is
    <f:view locale="#{localeManager.locale}">
    instead of
    <f:view locale="localeManager.locale">
    I have implemented a WebCenter Portal application one year ago (WebCenter 11.1.1.5) and I utilized the same approach for session localization. I can confirm that everything worked fine by setting the locale in the <f:view> tag in the same way.

    Dimitar
  • 3. Re: WebCenter Portal programmatic localization
    793718 Newbie
    Currently Being Moderated
    >
    Hi.

    What you need to do is to create a custom JSF view handler (extending javax.faces.application.ViewHandler), and override this method:
    Locale calculateLocate(FacesContext context)
    . There, you can retrieve the value for the selected locale and force it on the whole application.

    The Javadocs for http://docs.oracle.com/cd/E17802_01/j2ee/j2ee/javaserverfaces/1.2/docs/api/javax/faces/application/ViewHandler.html

    Once you have coded your handler, you plug it in faces-config.xml.

    Best Regards,

    Frédéric.
    >

    Thank you for your prompt reply!

    Unfortunately, I was unable to make your example work.
    Could you please tell me how you define the ViewHandler in the faces-config.xml ? I already have a line that defines a view handler, and it looks like this:
    <view-handler>oracle.webcenter.portalframework.sitestructure.handler.CustomViewHandler</view-handler>
    this is a mock I believe, since it does not override a single method. I tried replacing it with my own class, or keeping both lines, but that did not seem to work. The application can not be deployed with either of the options. Here is the deployment log:
    [05:28:48 PM] Deploying Application...
    [05:29:31 PM] Weblogic Server Exception: weblogic.application.ModuleException: 
    [05:29:31 PM] Caused by: java.lang.InstantiationException: 
    [05:29:31 PM]   See server logs or server console for more details.
    [05:29:31 PM] weblogic.application.ModuleException:
    [05:29:31 PM] ####  Deployment incomplete.  ####
    [05:29:31 PM] Remote deployment failed (oracle.jdevimpl.deploy.common.Jsr88RemoteDeployer)
    Am I correct in understanding that if a CustomViewHandler is defined, there is no more need to use this?
    <f:view locale="#{LocaleManager.locale}">
    This is my custom view handler class
    package portal.fo.shared.util;
    
    import portal.fo.shared.bean.LocaleManager;
    import portal.fo.shared.logger.FOLogger;
    
    import java.util.Locale;
    
    import javax.faces.application.ViewHandler;
    import javax.faces.context.FacesContext;
    
    import oracle.adf.share.ADFContext;
    
    
    public abstract class FOCustomViewHandler extends ViewHandler {
    
      private static FOLogger logger = FOLogger.createFOLogger(FOCustomViewHandler.class);
    
      public FOCustomViewHandler() {
        super();
      }
    
      public Locale calculateLocale(FacesContext facesContext) {
        LocaleManager localeManager = (LocaleManager)ADFContext.getCurrent().getSessionScope().get("LocaleManager");
        Locale locale = null;
        if (localeManager != null) {
          String sLocale = localeManager.getLanguage();
          if (sLocale != null) {
            logger.debug("Locale from session bean used");
            locale = localeManager.getLocale();
          } else {
            logger.debug("Default locale used");
            locale = new Locale("fi");
          }
        }
        return locale;
      }
    }
    Just to make sure the problem is clear: the way I am using right now works great except for the WebCenter Portal navigation. My application has several horizontal tabs. These tabs are defined in default-navigation-model.xml. In URL Attributes I set the Title attribute to use a value from a Resource Bundle, and when I change the language in the browser, the navigation tabs are translated. It does not take the application locale into account though.

    Edited by: Andrejs Dasko on Jan 28, 2013 7:36 AM

    Edited by: Andrejs Dasko on Jan 30, 2013 7:26 AM
  • 4. Re: WebCenter Portal programmatic localization
    Blueberry Coder Journeyer
    Currently Being Moderated
    Hi.

    You are right; with a custom handler, your pages don't need to assign a value to the locale property of the view tag.

    To ensure you are not breaking anything provided by the WebCenter framework, you view handler class should extend oracle.webcenter.portalframework.sitestructure.handler.CustomViewHandler. There can be only one handler declaration in <tt>faces-config.xml</tt> at a time. Yours should replace the WebCenter provided one.

    Why your application doesn't deploy when you do this, I don't know. Can you provide the stack trace foung in the server logs?

    Best Regards,

    Frédéric.
  • 5. Re: WebCenter Portal programmatic localization
    793718 Newbie
    Currently Being Moderated
    Hello Frederic!

    >
    To ensure you are not breaking anything provided by the WebCenter framework, you view handler class should extend oracle.webcenter.portalframework.sitestructure.handler.CustomViewHandler. There can be only one handler declaration in faces-config.xml at a time. Yours should replace the WebCenter provided one.
    >

    Thank you, I believe that extending oracle.webcenter.portalframework.sitestructure.handler.CustomViewHandler will allow the application to deploy. I was trying to extend javax.faces.application.ViewHandler, and JDeveloper did not seem to like that.
    I can not find which Technology this package (oracle.webecnter.portalframework) belongs to however. I would assume it would be WebCenter Portal - Framework, but even after clean and rebuild of the project, JDeveloper is still unable to import this class. Maybe it is just JDeveloper not reacting to new classes being added to the classpath. Do you know where this package is located and how I can import it.

    Respectfully
    Andrejs
  • 6. Re: WebCenter Portal programmatic localization
    Blueberry Coder Journeyer
    Currently Being Moderated
    It is contained in the WebCenter Portal Framework library. You can check such things by opening the Manage Libraries window (in the Tools menu) and typing the fully qualified class name in the search box. This is supposed to be part of the dependencies for your project if you selected the WebCenter application template.... Please ensure it is still referenced.

    Frédéric.
  • 7. Re: WebCenter Portal programmatic localization
    793718 Newbie
    Currently Being Moderated
    >
    It is contained in the WebCenter Portal Framework library. You can check such things by opening the Manage Libraries window (in the Tools menu) and typing the fully qualified class name in the search box. This is supposed to be part of the dependencies for your project if you selected the WebCenter application template.... Please ensure it is still referenced.
    >

    Thank you so much for the Tools -> Manage Libraries tip! I could have used that on several occasions in the past.

    I was able to get the view handler to work, once I removed all the <af:view> locale attributes. The navigation model is still not translated however. If the browser language is changed, navigation model is translated. This is very strange, because even the Loading message is now translated to the language specified in the CustomViewHandler. But the navigation model itself keeps using the browser language...

    Navigation model is described in default-navigation-model.xml

    Respectfully,
    Andrejs

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points