restructured the duration formatting templates for easier translation

This commit is contained in:
Sheldan
2020-05-11 21:14:55 +02:00
parent 70428e6e03
commit c234266b7b
14 changed files with 15 additions and 29 deletions

View File

@@ -41,36 +41,17 @@ public class DurationMethod implements TemplateMethodModelEx {
Duration duration = (Duration) wrappedObject;
StringBuilder stringBuilder = new StringBuilder();
// upgrading to java 9 makes this nicer
// todo refactor to use a single template, instead of multiple ones
HashMap<String, Object> parameters = new HashMap<>();
long days = duration.toDays();
if(days > 0) {
stringBuilder.append(service.renderTemplate("day", getParam(days)));
}
parameters.put("days", days);
long hours = duration.toHours() % 24;
if(hours > 0) {
stringBuilder.append(service.renderTemplate("hour", getParam(hours)));
}
parameters.put("hours", hours);
long minutes = duration.toMinutes() % 60;
if(minutes > 0) {
stringBuilder.append(service.renderTemplate("minute", getParam(minutes)));
}
parameters.put("minutes", minutes);
long seconds = duration.get(ChronoUnit.SECONDS) % 60;
if(seconds > 0) {
stringBuilder.append(service.renderTemplate("second", getParam(seconds)));
}
parameters.put("seconds", seconds);
return stringBuilder.toString();
}
/**
* Creates the parameter passed to the template for rendering the single time unit template.
* @param value
* @return
*/
private HashMap<String, Object> getParam(Long value) {
HashMap<String, Object> params = new HashMap<>();
params.put("amount", value);
return params;
return service.renderTemplateWithMap("duration_formatting", parameters);
}
}

View File

@@ -1 +0,0 @@
<#if amount gt 1>${amount} days<#else>1 day</#if>

View File

@@ -0,0 +1 @@
<#if days gt 1><#include "duration_days"><#elseif days = 1><#include "duration_day"></#if> <#if hours gt 1><#include "duration_hours"><#elseif hours = 1><#include "duration_hour"></#if> <#if minutes gt 1><#include "duration_minutes"><#elseif minutes = 1><#include "duration_minute"></#if> <#if seconds gt 1><#include "duration_seconds"><#elseif seconds = 1><#include "duration_second"></#if>

View File

@@ -1 +0,0 @@
<#if amount gt 1>${amount} hours<#else>1 hour</#if>

View File

@@ -1 +0,0 @@
<#if amount gt 1>${amount} minutes<#else>1 minute</#if>

View File

@@ -1 +0,0 @@
<#if amount gt 1>${amount} seconds<#else>1 second</#if>