2

I'm having trouble localizing a composite component (Mojarra 2.2, Glassfish 4.0).

I tried BalusC's solution here to put the properties file in the same directory as the composite component with the same file name as the composite component and a .properties extension -- that works to externalize the strings from the composite component's definition.

But when I want to localize the strings (by adding _en suffix to the property file's filename) I get this error (only thing changed is the name of the properties file):

[2013-08-08T13:49:04.234-0500] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=24 _ThreadName=http-listener-1(5)] [timeMillis: 1375987744234] [levelValue: 900] [[
  StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at javax.faces.component.UIComponent.findComponentResourceBundleLocaleMatch(UIComponent.java:1204)
at javax.faces.component.UIComponent.getResourceBundleMap(UIComponent.java:1007)
at sun.reflect.GeneratedMethodAccessor1226.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:115)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)

...

I read an article (post from 24/Aug/12 03:50 PM) that JSF localizes differently than standard Java and that the properties files would go in resources/en/<library>/<component>.properties, so I put mycomp.properties there and got the error again.

My default locale is actually "en_US" -- I tried that as well with the same results.

So, this works:
resources/lib/mycomp.xhtml, resources/lib/mycomp.properties

These don't work:
resources/lib/mycomp.xhtml, resources/lib/mycomp_en.properties
resources/lib/mycomp.xhtml, resources/en/lib/mycomp.properties

What am I doing wrong?

Community
  • 1
  • 1
CraftWeaver
  • 707
  • 1
  • 8
  • 21

1 Answers1

2

I can reproduce your problem. This is quite embarrassing. Apparently something changed related to this in a newer Mojarra version. I can't get both the old and new approach to work in 2.2.0.

I peeked in the Mojarra source code again, starting at UIComponent#getResourceBundleMap(), and I learned that the resource bundle is first searched based on the FQN of the component's class. We could easily make use of it.

If not already done, first create a backing component class:

package com.example.composite;

@FacesComponent("myComposite")
public class MyComposite extends UINamingContainer {
    // Can be kept empty.
}

And declare it in the composite component definition:

<cc:interface componentType="myComposite">

Then, you can simply put the MyComposite.properties, MyComposite_en.properties, etc in the very same com.example.composite package. It worked for me.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I tried that work-around, but get the same error. I verified the backing component class is being instantiated (so, presumably it's properly associated with the composite component), but still get the NullPointerException when trying to obtain the string with `#{cc.resourceBundleMap.title}`. Could there be something else I need to do, as well? I've defined a `` in `faces-config.xml` to use for app-wide localization -- could that be complicating things? if I reference that bundle in the composite component, it works, so that could suffice as a work-around, I suppose. – CraftWeaver Aug 09 '13 at 11:02