Thanks for the reply Thierry.
Ah I see, you can only have one language per variant.
I was in a different mindset adding multiple languages per variant.
So, with one language per variant am I correct in thinking that if I
wished to support 6 languages and 3 mediatypes (json,xml,html) that I
would have to add 18 variants?
If so, this approach does not seem to scale well.
I initially thought it would work like:
A match would occur for the mediatype and if the language was in the
variants language list.
Then provide a method like getPreferredLanguage().
Variant variant = new Variant(MediaType.TEXT_PLAIN);
variant.getLanguages().add(Language.ENGLISH);
variant.getLanguages().add(Language.FRENCH);
...
(or more likely have a single List<Language> and variant.setLanguages(list))
getVariants().add(variant);
if (MediaType.TEXT_PLAIN.equals(variant.getMediaType())) {
if (Language.FRENCH.equals(variant.getPreferredLanguage())) {
rep = new StringRepresentation("Je parle français.");
} else {
...
}
For my real usage to grab a ResourceBundle, I would just use
variant.getPreferredLanguage() and push it to the freemarker template.
What do you think about this approach?
jon
Thierry Boileau wrote:
Hi Jonathan,
I'm not sure to clearly understand what you say.
The framework actually includes a content negotiation algorithm which
is in charge to compare the clients preferences (media-type and
languages only) on one side with the list of supported variants
declared by the resource on the other side and to determine the
preferred variant [1]. Once the choice is done, the framework calls
the Resource#represent(Variant) method (instead of deprecated
"getRepresentation(Variant)"...) which is the place where you decide
to generate the representation according to the variant passed as
parameter.
In this case, the variant contains only one Language metadata
(Language.FRENCH or Language.ENGLISH) which allows to make the tests
with the "contains" method.
I hope I answer your question.
best regards,
Thierry Boileau
[1] see "com.noelios.restlet.Engine#getPreferredVariant(ClientInfo,
List<Variant>, Language)"
On Wed, Mar 12, 2008 at 2:28 PM, Jonathan Hall <[EMAIL PROTECTED]> wrote:
Hi Thierry,
Say I had the client send multiple languages:
" Accept-Language: en-gb,fr;q=0.7,en;q=0.3"
If using a variant with multiple languages, the code given wouldn't
satisfy the clients preference, since it is using ".contains(...)". It
would return the language depending on the control code.
To return the correct preference, the client language preference/weights
should be compared to the variants languages and a best match found.
I can't find a method like this, I think it sounds like a useful
addition to the framework, do you?
(btw I'm testing with 1.1-SNAPSHOT and 1.1-M2)
jon
Thierry Boileau wrote:
> Hello Jon,
>
> here is the code of a sample resource declaring 2 variants with the
> same media-type and distinct languages.
> I've tested it against release 1.1m2.
>
> best regards,
> Thierry Boileau
>
> *****
> import org.restlet.Context;
> import org.restlet.data.Language;
> import org.restlet.data.MediaType;
> import org.restlet.data.Request;
> import org.restlet.data.Response;
> import org.restlet.resource.Representation;
> import org.restlet.resource.Resource;
> import org.restlet.resource.StringRepresentation;
> import org.restlet.resource.Variant;
>
> public class MyResource extends Resource {
> public MyResource(Context context, Request request, Response response) {
> super(context, request, response);
> // Defines two text_plain variants
> Variant variant = new Variant(MediaType.TEXT_PLAIN);
> variant.getLanguages().add(Language.ENGLISH);
> getVariants().add(variant);
> variant = new Variant(MediaType.TEXT_PLAIN);
> variant.getLanguages().add(Language.FRENCH);
> getVariants().add(variant);
> }
>
> @Override
> public Representation getRepresentation(Variant variant) {
> Representation rep = null;
> if (MediaType.TEXT_PLAIN.equals(variant.getMediaType())) {
> if (variant.getLanguages().contains(Language.FRENCH)) {
> rep = new StringRepresentation("Je parle français.");
> } else {
> rep = new StringRepresentation("I speak English.");
> }
> // Update the representation metadata
> rep.setMediaType(variant.getMediaType());
> rep.getLanguages().addAll(variant.getLanguages());
> }
>
> return rep;
> }
> }
>
> *****
>
>
> On Tue, Mar 11, 2008 at 7:11 PM, Jonathan Hall <[EMAIL PROTECTED]> wrote:
>
>> Hi,
>>
>> I've been looking at getting the preferred locale and I have a couple of
>> questions.
>>
>> I see that getPreferredVariant() uses apaches content negotiation
>> algorithm
>> (http://httpd.apache.org/docs/2.2/en/content-negotiation.html#algorithm)
>> which includes using the Accept-Language header:
>>
>> "|Accept-Language: en-gb,en;q=0.5"
>>
>> I'm unsure how (if) this comes into play or how to use it to get the
>> preferred language.
>>
>> I added Language.FRENCH only to a variant and added it to
>> Resource.getVariants() thinking it may use it in content negotiation, it
>> seems not.
>>
>> If this worked as expected I propose a method like
>> Resource.||getPreferredVariant().getPreferredLanguage()|| to get the
>> preferred locale.|||
>> |
>> || If the client's language does not match then 406 or use a default
>> language. Make it backwards compatible by only using that algorithm when
>> languages are set?
>>
>> Any help, thoughts appreciated
>>
>> jon
>>
>> |||
>> ||
>>
>>
>
>