Search in sources :

Example 1 with StringModel

use of freemarker.ext.beans.StringModel in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatConstraintViolationWorksWithDefault.

@Test
public void testThatConstraintViolationWorksWithDefault() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    Mockito.when(messages.get(IsInteger.KEY, context, resultOptional)).thenReturn(Optional.empty());
    ConstraintViolation violation = new ConstraintViolation(IsInteger.KEY, "theField", IsInteger.MESSAGE);
    List args = new ArrayList();
    args.add(new StringModel(violation, new BeansWrapper()));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("theField must be an integer"));
    Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject());
}
Also used : ConstraintViolation(ninja.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringModel(freemarker.ext.beans.StringModel) BeansWrapper(freemarker.ext.beans.BeansWrapper) TemplateModel(freemarker.template.TemplateModel) Result(ninja.Result) Test(org.junit.Test)

Example 2 with StringModel

use of freemarker.ext.beans.StringModel in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatConstraintViolationWorks.

@Test
public void testThatConstraintViolationWorks() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    Mockito.when(messages.get(IsInteger.KEY, context, resultOptional)).thenReturn(Optional.of("This simulates the translated message!"));
    ConstraintViolation violation = new ConstraintViolation(IsInteger.KEY, "theField", IsInteger.MESSAGE);
    List args = new ArrayList();
    args.add(new StringModel(violation, new BeansWrapper()));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message!"));
    Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject());
}
Also used : ConstraintViolation(ninja.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringModel(freemarker.ext.beans.StringModel) BeansWrapper(freemarker.ext.beans.BeansWrapper) TemplateModel(freemarker.template.TemplateModel) Result(ninja.Result) Test(org.junit.Test)

Example 3 with StringModel

use of freemarker.ext.beans.StringModel in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethod method exec.

public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() == 1 && args.get(0) instanceof StringModel && ((StringModel) args.get(0)).getWrappedObject() instanceof ConstraintViolation) {
        ConstraintViolation violation = (ConstraintViolation) ((StringModel) args.get(0)).getWrappedObject();
        String messageValue = messages.get(violation.getMessageKey(), context, result, violation.getMessageParams()).orElse(violation.getDefaultMessage());
        return new SimpleScalar(messageValue);
    } else if (args.size() == 1) {
        String messageKey = ((SimpleScalar) args.get(0)).getAsString();
        String messageValue = messages.get(messageKey, context, result).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else if (args.size() > 1) {
        List<String> strings = Lists.newArrayList();
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        String messageKey = strings.get(0);
        String messageValue = messages.get(messageKey, context, result, strings.subList(1, strings.size()).toArray()).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else {
        throw new TemplateModelException("Using i18n without any key is not possible.");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ConstraintViolation(ninja.validation.ConstraintViolation) StringModel(freemarker.ext.beans.StringModel) SimpleScalar(freemarker.template.SimpleScalar)

Aggregations

StringModel (freemarker.ext.beans.StringModel)3 ConstraintViolation (ninja.validation.ConstraintViolation)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 TemplateModel (freemarker.template.TemplateModel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Result (ninja.Result)2 Test (org.junit.Test)2 SimpleNumber (freemarker.template.SimpleNumber)1 SimpleScalar (freemarker.template.SimpleScalar)1 TemplateModelException (freemarker.template.TemplateModelException)1