Skip to content

Commit

Permalink
#213 handle parameters in i18n mustache templates
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Oct 18, 2016
1 parent 7b9962e commit 0dcecc0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import alfio.model.UploadedResource;
import alfio.model.modification.UploadBase64FileModification;
import alfio.repository.EventRepository;
import alfio.util.TemplateManager;
import alfio.util.TemplateManager.TemplateResource;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -95,11 +96,8 @@ public void getTemplate(@PathVariable("name") TemplateResource name, @PathVariab
Locale loc = Locale.forLanguageTag(locale);
String template = new String(os.toByteArray(), StandardCharsets.UTF_8);




response.setContentType("text/plain");
response.getWriter().print("");
response.getWriter().print(TemplateManager.translate(template, loc, messageSource));
}


Expand Down
11 changes: 7 additions & 4 deletions src/main/java/alfio/util/TemplateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ public Pair<ParserState, Integer> next(String template, int idx, AST ast) {
public Pair<ParserState, Integer> next(String template, int idx, AST ast) {

//
I18NNode currentNode = (I18NNode)ast.currentLevel;
currentNode.text = template.substring(currentNode.startIdx, idx);
ast.focusToParent();
//
return Pair.of(OPEN_TAG, idx + END_TAG.length());
Expand Down Expand Up @@ -339,7 +337,6 @@ public void visit(StringBuilder sb, Locale locale, MessageSource messageSource)

static class I18NNode extends Node {
int startIdx;
String text;

I18NNode(int startIdx) {
this.startIdx = startIdx;
Expand All @@ -351,9 +348,15 @@ public void visit(StringBuilder sb, Locale locale, MessageSource messageSource)
for(Node node : children) {
node.visit(internal, locale, messageSource);
}

String childTemplate = internal.toString();
String key = extractKey(childTemplate);
List<String> args = extractParameters(childTemplate);
String text = messageSource.getMessage(key, args.toArray(), locale);

//
//FIXME add support for parameters :)
sb.append(messageSource.getMessage(internal.toString(), null, locale));
sb.append(text);
//
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/alfio/util/TemplateManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class TemplateManagerTest {
messageSource.addMessage("a", Locale.ENGLISH, "a-resolved");
messageSource.addMessage("b", Locale.ENGLISH, "b-resolved");
messageSource.addMessage("1a-resolved-middle-b-resolved2", Locale.ENGLISH, "complete-resolved");

messageSource.addMessage("parameter", Locale.ENGLISH, "{2}-{1}-{0}");
}

@Test
Expand Down Expand Up @@ -74,4 +76,9 @@ public void parseNestedI18N() {
public void parseNested2I18N() {
Assert.assertEquals("0complete-resolved3", TemplateManager.translate("0{{#i18n}}1{{#i18n}}a{{/i18n}}-middle-{{#i18n}}b{{/i18n}}2{{/i18n}}3", Locale.ENGLISH, messageSource));
}

@Test
public void simpleParams() {
Assert.assertEquals("3-2-1", TemplateManager.translate("{{#i18n}}parameter [1] [2] [3]{{/i18n}}", Locale.ENGLISH, messageSource));
}
}

0 comments on commit 0dcecc0

Please sign in to comment.