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
|||
||