Add localization option for template engines

This commit is contained in:
René Pfeuffer
2019-04-03 11:52:01 +02:00
parent 751196c870
commit 08260fd7be
5 changed files with 81 additions and 5 deletions

View File

@@ -97,6 +97,11 @@ public class MustacheTemplateEngineTest extends TemplateEngineTestBase
return "sonia/scm/template/001.mustache";
}
@Override
public String getTemplateResourceWithGermanTranslation() {
return "sonia/scm/template/loc.mustache";
}
/**
* Method description
*

View File

@@ -37,6 +37,7 @@ package sonia.scm.template;
import com.google.common.collect.Maps;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -52,6 +53,7 @@ import java.io.StringWriter;
import java.net.URL;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
@@ -92,6 +94,8 @@ public abstract class TemplateEngineTestBase
*/
public abstract String getTemplateResource();
public abstract String getTemplateResourceWithGermanTranslation();
/**
* Method description
*
@@ -126,7 +130,7 @@ public abstract class TemplateEngineTestBase
* @throws IOException
*/
@Test
public void testGetTemlateNotFound() throws IOException
public void testGetTemplateNotFound() throws IOException
{
ServletContext context = mock(ServletContext.class);
TemplateEngine engine = createEngine(context);
@@ -151,6 +155,32 @@ public abstract class TemplateEngineTestBase
assertNotNull(engine.getTemplate(getTemplateResource()));
}
@Test
public void testGetTemplateFromClasspathWithExistingLocale() throws IOException
{
ServletContext context = mock(ServletContext.class);
TemplateEngine engine = createEngine(context);
Template template = engine.getTemplate(getTemplateResourceWithGermanTranslation(), Locale.GERMAN);
StringWriter writer = new StringWriter();
template.execute(writer, new Object());
Assertions.assertThat(writer.toString()).contains("German");
}
@Test
public void testGetTemplateFromClasspathWithNotExistingLocale() throws IOException
{
ServletContext context = mock(ServletContext.class);
TemplateEngine engine = createEngine(context);
Template template = engine.getTemplate(getTemplateResourceWithGermanTranslation(), Locale.CHINESE);
StringWriter writer = new StringWriter();
template.execute(writer, new Object());
Assertions.assertThat(writer.toString()).contains("English");
}
/**
* Method description
*