Search in sources :

Example 1 with Language

use of org.restlet.data.Language in project qi4j-sdk by Qi4j.

the class ContextResourceClientFactory method setAcceptedLanguages.

public void setAcceptedLanguages(Language... acceptedLanguages) {
    List<Preference<Language>> languages = new ArrayList<Preference<Language>>();
    for (Language acceptedLanguage : acceptedLanguages) {
        languages.add(new Preference<Language>());
    }
    info.setAcceptedLanguages(languages);
}
Also used : Language(org.restlet.data.Language) Preference(org.restlet.data.Preference) ArrayList(java.util.ArrayList)

Example 2 with Language

use of org.restlet.data.Language in project qi4j-sdk by Qi4j.

the class AbstractResponseWriter method getVariant.

protected Variant getVariant(Request request, List<Language> possibleLanguages, List<MediaType> possibleMediaTypes) {
    Language language = request.getClientInfo().getPreferredLanguage(possibleLanguages);
    if (language == null) {
        language = possibleLanguages.get(0);
    }
    MediaType responseType = request.getClientInfo().getPreferredMediaType(possibleMediaTypes);
    if (responseType == null && request.getClientInfo().getPreferredMediaType(Collections.singletonList(MediaType.ALL)) == MediaType.ALL) {
        responseType = possibleMediaTypes.get(0);
    }
    Variant variant = new Variant(responseType, language);
    variant.setCharacterSet(CharacterSet.UTF_8);
    return variant;
}
Also used : Variant(org.restlet.representation.Variant) Language(org.restlet.data.Language) MediaType(org.restlet.data.MediaType)

Example 3 with Language

use of org.restlet.data.Language in project qi4j-sdk by Qi4j.

the class ContextResource method getLocale.

protected Locale getLocale() {
    Request request = Request.getCurrent();
    List<Preference<Language>> preferenceList = request.getClientInfo().getAcceptedLanguages();
    if (preferenceList.isEmpty()) {
        return Locale.getDefault();
    }
    Language language = preferenceList.get(0).getMetadata();
    String[] localeStr = language.getName().split("-");
    Locale locale;
    switch(localeStr.length) {
        case 1:
            locale = new Locale(localeStr[0]);
            break;
        case 2:
            locale = new Locale(localeStr[0], localeStr[1]);
            break;
        case 3:
            locale = new Locale(localeStr[0], localeStr[1], localeStr[2]);
            break;
        default:
            locale = Locale.getDefault();
    }
    return locale;
}
Also used : Locale(java.util.Locale) Language(org.restlet.data.Language) Preference(org.restlet.data.Preference) Request(org.restlet.Request)

Example 4 with Language

use of org.restlet.data.Language in project OpenAM by OpenRock.

the class DeviceCodeVerificationResource method getTemplateRepresentation.

private Representation getTemplateRepresentation(String template, OAuth2Request request, String errorCode) {
    TemplateRepresentation response = getTemplateFactory(getContext()).getTemplateRepresentation(template);
    Map<String, String> dataModel = new HashMap<>();
    dataModel.put("errorCode", errorCode);
    dataModel.put("baseUrl", baseURLProviderFactory.get(request.<String>getParameter("realm")).getRootURL(ServletUtils.getRequest(getRequest())));
    List<String> locale = new ArrayList<>();
    for (Preference<Language> language : getRequest().getClientInfo().getAcceptedLanguages()) {
        locale.add(language.getMetadata().getName());
    }
    dataModel.put("locale", oAuth2Utils.join(locale, " "));
    dataModel.put("realm", request.<String>getParameter(OAuth2Constants.Params.REALM));
    response.setDataModel(dataModel);
    return response;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) Language(org.restlet.data.Language) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

Language (org.restlet.data.Language)4 ArrayList (java.util.ArrayList)2 Preference (org.restlet.data.Preference)2 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Request (org.restlet.Request)1 MediaType (org.restlet.data.MediaType)1 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)1 Variant (org.restlet.representation.Variant)1