I'm assuming that by "language code" you mean the value of the
Accepted-Languages header from the request.

You can use Request.getCurrent() to get the, um, current request, from
which you can retrieve the language(s). This works as long as you're in the
same thread that is handling the request, or if you're in a task that was
submitted to the TaskService from such a thread. (Not sure what happens in
async handling, though.)

But relying on static methods to supply values is something to avoid if
possible. Why not pass contextual information (like the accepted languages)
to the Validator instance at construction time? It looks especially nice
with the builder pattern:

    Literal literal = Validator.builder()
        .withAcceptedLanguages(getClientInfo().getAcceptedLanguages())
        .build()
        .validate(jsonObj.getString("textKey"));


--tim

On Tue, Mar 27, 2012 at 6:19 AM, Mutaz Qasem <[email protected]> wrote:

> Hi
>
> I have a resource
>
> public class SomeResource extends ServerResource{
>    @Post("json")
>    public Representation create(JsonRepresentation entity){
>
>        JSONObject jsonObj = entity.getJsonObject();
>        Literal literal = new
> Vlidator().validate(jsonObj.getString("textKey"));
>    }
> }
>
> public class Vlidator{
>     public Literal validate(String someText){
>          String langCode = getLanguageCode();
>          LiteralManager lm = new LiteralManager();
>          return lm.createLiteral(someText, langCode);
>     }
>     public String getLanguageCode(){
>        // How to get the lang code??!!
>    }
> }
>
> How can I implement getLanguageCode() method to retrieve the language code
> of the thread of the current request. Considering that Vlidator class is
> defined somewhere else outside the SomeResource class.
> Is that possible?
>
> Thanks
> Mutaz
>
> --
> View this message in context:
> http://restlet-discuss.1400322.n2.nabble.com/Get-language-code-from-the-executing-thread-tp7409075p7409075.html
> Sent from the Restlet Discuss mailing list archive at Nabble.com.
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2941092
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2941152

Reply via email to