users@jersey.java.net

Re: Jersey internationalization

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 22 Feb 2008 15:11:50 +0100

Hi Merry,

It is not possible to use annotations for negotiating language content.

Instead there is programmatic support using the javax.ws.rs.core.Variant
and the javax.ws.rs.core.Request#selectVariant method.

    @Path("/")
    public static class LanguageVariantResource {
        @GET
        public Response doGet(@Context Request r) {
            List<Variant> vs = Variant.VariantListBuilder.newInstance().
                    languages("zh").
                    languages("fr").
                    languages("en").add().
                    build();

            Variant v = r.selectVariant(vs);
            if (v == null)
                return Response.notAcceptable(vs).build();
            else {
                // do some work.
                // check value of v.getLanguage()
                Object c = ...
                return Response.ok(c, v).build();
            }
        }

Note that if you are using the per-request life-cycle you can choose to
do the variant selection and not acceptable response (as a
WebApplicationException) on the class constructor to avoid repeating for
multiple HTTP methods.

Hope this helps,
Paul.

wang merry wrote:
> Hi, Paul
>
> I means: support for returning representations in different languages
> and support for handling language negotiation with the Content-Language
> and Accept-Language HTTP headers
>
> For exemple: if Accept-Language=fr, it return the representations in french.
> if Accept-Language=en, it return the representations
> in english.
>
> Thanks.
>
> Merry
>
> ------------------------------------------------------------------------
> Date: Thu, 21 Feb 2008 08:33:52 +0100
> From: Paul.Sandoz_at_Sun.COM
> Subject: Re: Jersey internationalization
> To: users_at_jersey.dev.java.net
> CC: merrywang2001_at_hotmail.com
>
> Hi Merry,
>
> Can you provide more details on what you want to do, perhaps with an
> example.
>
> Do you mean support for returning representations in different
> languages and support for handling language negotiation with the
> Content-Language and Accept-Language HTTP headers?
>
> Or do you mean that the log and exception messages that Jersey
> prints out are in English only?
>
> Thanks,
> Paul.
>
> On Feb 20, 2008, at 4:41 PM, wang merry wrote:
>
> Hello
>
> If Jersey provide internationalized operation, if yes, how.
>
> Thanks
>
>
>
> Merry
>
> ------------------------------------------------------------------------
> Express yourself instantly with MSN Messenger! MSN Messenger
> <http://clk.atdmt.com/AVE/go/onm00200471ave/direct/01/>
>
>
>
> ------------------------------------------------------------------------
> Express yourself instantly with MSN Messenger! MSN Messenger
> <http://clk.atdmt.com/AVE/go/onm00200471ave/direct/01/>

-- 
| ? + ? = To question
----------------\
   Paul Sandoz
        x38109
+33-4-76188109