Add i18n for rec. trx. reminder email
This commit is contained in:
@@ -6,9 +6,11 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.management.ReflectionException;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
@Configuration
|
||||
@@ -21,6 +23,7 @@ public class FinancerConfig {
|
||||
private String dateFormat;
|
||||
private Collection<String> mailRecipients;
|
||||
private String fromAddress;
|
||||
private Locale emailLocale;
|
||||
|
||||
/**
|
||||
* @return the raw country code, mostly an uppercase ISO 3166 2-letter code
|
||||
@@ -109,4 +112,19 @@ public class FinancerConfig {
|
||||
public void setFromAddress(String fromAddress) {
|
||||
this.fromAddress = fromAddress;
|
||||
}
|
||||
|
||||
public void setEmailLocale(String emailLocale) {
|
||||
try {
|
||||
this.emailLocale = (Locale) Locale.class.getDeclaredField(emailLocale).get(null);
|
||||
} catch (ReflectiveOperationException | NullPointerException roe) {
|
||||
LOGGER.warn(String
|
||||
.format("Could not get Locale for emailLocale '%s' - use fallback ENGLISH instead.", emailLocale), roe);
|
||||
|
||||
this.emailLocale = Locale.ENGLISH;
|
||||
}
|
||||
}
|
||||
|
||||
public Locale getEmailLocale() {
|
||||
return emailLocale;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,14 @@ import org.apache.commons.collections4.IterableUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@@ -29,6 +31,9 @@ public class SendRecurringTransactionReminderTask {
|
||||
@Autowired
|
||||
private JavaMailSender mailSender;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
@Scheduled(cron = "0 30 0 * * *")
|
||||
public void sendReminder() {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
@@ -56,7 +61,7 @@ public class SendRecurringTransactionReminderTask {
|
||||
|
||||
final StringBuilder reminderBuilder = new StringBuilder();
|
||||
|
||||
reminderBuilder.append("The following recurring transactions are due today:")
|
||||
reminderBuilder.append(this.getMessage("financer.recurring-transaction.email.reminder.intro"))
|
||||
.append(System.lineSeparator())
|
||||
.append(System.lineSeparator());
|
||||
|
||||
@@ -65,9 +70,9 @@ public class SendRecurringTransactionReminderTask {
|
||||
.append("|")
|
||||
.append(rt.getDescription())
|
||||
.append(System.lineSeparator())
|
||||
.append("From ")
|
||||
.append(this.getMessage("financer.recurring-transaction.email.reminder.from"))
|
||||
.append(rt.getFromAccount().getKey())
|
||||
.append(" to ")
|
||||
.append(this.getMessage("financer.recurring-transaction.email.reminder.to"))
|
||||
.append(rt.getToAccount().getKey())
|
||||
.append(": ")
|
||||
.append(rt.getAmount().toString())
|
||||
@@ -79,7 +84,7 @@ public class SendRecurringTransactionReminderTask {
|
||||
|
||||
msg.setTo(this.financerConfig.getMailRecipients().toArray(new String[]{}));
|
||||
msg.setFrom(this.financerConfig.getFromAddress());
|
||||
msg.setSubject("[Financer] Recurring transactions reminder");
|
||||
msg.setSubject(this.getMessage("financer.recurring-transaction.email.reminder.subject"));
|
||||
msg.setText(reminderBuilder.toString());
|
||||
|
||||
try {
|
||||
@@ -87,8 +92,12 @@ public class SendRecurringTransactionReminderTask {
|
||||
} catch (MailException e) {
|
||||
LOGGER.error("Could not send recurring transaction email reminder!", e);
|
||||
|
||||
LOGGER.info("Dumb email reminder content because the sending failed");
|
||||
LOGGER.info("Dump email reminder content because the sending failed");
|
||||
LOGGER.info(reminderBuilder.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private String getMessage(String key) {
|
||||
return this.messageSource.getMessage(key, null, this.financerConfig.getEmailLocale());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,6 @@ spring.datasource.url=jdbc:postgresql://localhost/financer_nm
|
||||
spring.datasource.username=financer_nm
|
||||
spring.datasource.password=financer_nm
|
||||
|
||||
financer.mailRecipients[0]=niklasmueller91@gmx.net
|
||||
financer.mailRecipients[0]=niklasmueller91@gmx.net
|
||||
|
||||
financer.email.language=GERMANY
|
||||
@@ -47,4 +47,10 @@ spring.mail.host=localhost
|
||||
|
||||
# Disable JMX as we don't need it and it blocks parallel deployment on Tomcat
|
||||
# because the connection pool cannot shutdown properly
|
||||
spring.jmx.enabled=false
|
||||
spring.jmx.enabled=false
|
||||
|
||||
spring.messages.basename=i18n/message
|
||||
|
||||
# Language to use for the email reminders
|
||||
# Possible values are: ENGLISH, GERMANY
|
||||
financer.emailLocale=ENGLISH
|
||||
@@ -0,0 +1,4 @@
|
||||
financer.recurring-transaction.email.reminder.intro=The following recurring transactions are due today\:
|
||||
financer.recurring-transaction.email.reminder.from=From\:
|
||||
financer.recurring-transaction.email.reminder.to= To\:
|
||||
financer.recurring-transaction.email.reminder.subject=[Financer] Recurring transactions reminder
|
||||
@@ -0,0 +1,4 @@
|
||||
financer.recurring-transaction.email.reminder.intro=Die folgenden wiederkehrenden Buchungen sind heute f\u00E4llig\:
|
||||
financer.recurring-transaction.email.reminder.from=Von\:
|
||||
financer.recurring-transaction.email.reminder.to= Nach\:
|
||||
financer.recurring-transaction.email.reminder.subject=[Financer] Wiederkehrende Buchungen - Erinnerung
|
||||
@@ -11,6 +11,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
||||
@@ -32,6 +33,9 @@ public class SendRecurringTransactionReminderTaskTest {
|
||||
@Mock
|
||||
private FinancerConfig financerConfig;
|
||||
|
||||
@Mock
|
||||
private MessageSource messageSource;
|
||||
|
||||
@Test
|
||||
public void test_sendReminder() {
|
||||
// Arrange
|
||||
|
||||
Reference in New Issue
Block a user