Search in sources :

Example 1 with I18n

use of com.liferay.faces.util.i18n.I18n in project liferay-faces-alloy by liferay.

the class AlloyValidatorHelper method getMessage.

public static String getMessage(FacesContext facesContext, InputDate inputDate, String messageId) {
    ExternalContext externalContext = facesContext.getExternalContext();
    I18n i18n = I18nFactory.getI18nInstance(externalContext);
    Object localeObject = inputDate.getLocale(facesContext);
    Locale locale = getObjectAsLocale(localeObject);
    return i18n.getMessage(facesContext, locale, messageId);
}
Also used : Locale(java.util.Locale) ExternalContext(javax.faces.context.ExternalContext) I18n(com.liferay.faces.util.i18n.I18n)

Example 2 with I18n

use of com.liferay.faces.util.i18n.I18n in project liferay-faces-alloy by liferay.

the class AlloyValidatorHelper method getMessage.

public static String getMessage(FacesContext facesContext, InputTime inputTime, String messageId) {
    ExternalContext externalContext = facesContext.getExternalContext();
    I18n i18n = I18nFactory.getI18nInstance(externalContext);
    Object localeObject = inputTime.getLocale(facesContext);
    Locale locale = getObjectAsLocale(localeObject);
    return i18n.getMessage(facesContext, locale, messageId);
}
Also used : Locale(java.util.Locale) ExternalContext(javax.faces.context.ExternalContext) I18n(com.liferay.faces.util.i18n.I18n)

Example 3 with I18n

use of com.liferay.faces.util.i18n.I18n in project liferay-faces-alloy by liferay.

the class InputDateTime method validateValue.

protected final void validateValue(FacesContext facesContext, Object newValue, Date minDate, Date maxDate, TimeZone timeZone) {
    String pattern = getPattern();
    Date submittedDate = getObjectAsDate(newValue, pattern, timeZone);
    try {
        // the maximum date.
        if (submittedDate.before(minDate) || submittedDate.after(maxDate)) {
            setValid(false);
            String validatorMessage = getValidatorMessage();
            FacesMessage facesMessage;
            if (validatorMessage != null) {
                facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, validatorMessage, validatorMessage);
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
                simpleDateFormat.setTimeZone(timeZone);
                String minDateString = simpleDateFormat.format(minDate);
                String maxDateString = simpleDateFormat.format(maxDate);
                Locale locale = getObjectAsLocale(getLocale(facesContext));
                ExternalContext externalContext = facesContext.getExternalContext();
                I18n i18n = I18nFactory.getI18nInstance(externalContext);
                String message = i18n.getMessage(facesContext, locale, "please-enter-a-value-between-x-and-x", minDateString, maxDateString);
                facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
            }
            String clientId = getClientId(facesContext);
            facesContext.addMessage(clientId, facesMessage);
        } else {
            setValid(true);
        }
    } catch (FacesException e) {
        setValid(false);
        String message = e.getMessage();
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
        String clientId = getClientId(facesContext);
        facesContext.addMessage(clientId, facesMessage);
    }
}
Also used : Locale(java.util.Locale) ExternalContext(javax.faces.context.ExternalContext) FacesMessage(javax.faces.application.FacesMessage) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) FacesException(javax.faces.FacesException) I18n(com.liferay.faces.util.i18n.I18n)

Example 4 with I18n

use of com.liferay.faces.util.i18n.I18n in project liferay-faces-alloy by liferay.

the class InputFile method validateValue.

@Override
protected void validateValue(FacesContext facesContext, Object value) {
    super.validateValue(facesContext, value);
    if (isValid()) {
        Long maxFileSize = getMaxFileSize();
        String contentTypeSet = getContentTypes();
        if ((maxFileSize != null) || (contentTypeSet != null)) {
            Locale locale = facesContext.getViewRoot().getLocale();
            ExternalContext externalContext = facesContext.getExternalContext();
            I18n i18n = I18nFactory.getI18nInstance(externalContext);
            String clientId = getClientId(facesContext);
            @SuppressWarnings("unchecked") List<UploadedFile> uploadedFiles = (List<UploadedFile>) value;
            for (UploadedFile uploadedFile : uploadedFiles) {
                if ((maxFileSize != null) && (maxFileSize >= 0) && (uploadedFile.getSize() > maxFileSize)) {
                    String errorMessage = i18n.getMessage(facesContext, locale, "file-x-is-y-bytes-but-may-not-exceed-z-bytes", uploadedFile.getName(), uploadedFile.getSize(), maxFileSize);
                    handleInvalidFile(facesContext, clientId, uploadedFile, errorMessage);
                }
                String contentType = uploadedFile.getContentType();
                if ((contentType == null) || ((contentTypeSet != null) && !contentTypeSet.contains(contentType))) {
                    String errorMessage = i18n.getMessage(facesContext, locale, "file-x-has-an-invalid-content-type-y", uploadedFile.getName(), contentType);
                    handleInvalidFile(facesContext, clientId, uploadedFile, errorMessage);
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) UploadedFile(com.liferay.faces.util.model.UploadedFile) ExternalContext(javax.faces.context.ExternalContext) List(java.util.List) I18n(com.liferay.faces.util.i18n.I18n)

Example 5 with I18n

use of com.liferay.faces.util.i18n.I18n in project liferay-faces-alloy by liferay.

the class PaginatorRenderer method encodeSummary.

protected void encodeSummary(FacesContext facesContext, ResponseWriter responseWriter, Paginator paginator, String summaryPostion, Locale locale, int first, int curPageNumber, int rows, int rowCount, int pageCount) throws IOException {
    // If the paginator is to appear above or below the pagination controls, then
    boolean encodePaginationDiv = ("top".equals(summaryPostion) || "bottom".equals(summaryPostion));
    if (encodePaginationDiv) {
        // Encode the starting <div> element that represents the Bootstrap pagination component.
        responseWriter.startElement("div", paginator);
        responseWriter.startElement("ul", paginator);
        responseWriter.writeAttribute("class", "pagination", null);
    }
    // Determine the first page number and the last page number.
    int paginatorFirst = first + 1;
    int paginatorLast = Math.min(first + rows, rowCount);
    // Get an internationalized message that contains the pagination summary.
    ExternalContext externalContext = facesContext.getExternalContext();
    I18n i18n = I18nFactory.getI18nInstance(externalContext);
    String message = i18n.getMessage(facesContext, locale, "results-x-x-of-x-page-x-of-x", paginatorFirst, paginatorLast, rowCount, curPageNumber, pageCount);
    // Encode a list item inside the Bootstrap pagination component that contains the pagination summary message.
    responseWriter.startElement("li", paginator);
    responseWriter.startElement("span", paginator);
    responseWriter.writeAttribute("class", "alloy-paginator-summary disabled", null);
    responseWriter.writeText(message, paginator, null);
    responseWriter.endElement("span");
    responseWriter.endElement("li");
    // If the paginator is to appear above or below the pagination controls, then
    if (encodePaginationDiv) {
        // Encode the closing </div> element that represents the Bootstrap pagination component.
        responseWriter.endElement("ul");
        responseWriter.endElement("div");
    }
}
Also used : ExternalContext(javax.faces.context.ExternalContext) I18n(com.liferay.faces.util.i18n.I18n)

Aggregations

I18n (com.liferay.faces.util.i18n.I18n)10 ExternalContext (javax.faces.context.ExternalContext)10 Locale (java.util.Locale)9 I18nFactory (com.liferay.faces.util.i18n.I18nFactory)2 FacesMessage (javax.faces.application.FacesMessage)2 UIViewRoot (javax.faces.component.UIViewRoot)2 ResponseWriter (javax.faces.context.ResponseWriter)2 InputFile (com.liferay.faces.alloy.component.inputfile.InputFile)1 UploadedFile (com.liferay.faces.util.model.UploadedFile)1 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 FacesException (javax.faces.FacesException)1 Application (javax.faces.application.Application)1 ViewHandler (javax.faces.application.ViewHandler)1 NamingContainer (javax.faces.component.NamingContainer)1