Skip to content

Commit 6a361e0

Browse files
committed
fix: properly render strings using icu4j
1 parent ed69e46 commit 6a361e0

File tree

22 files changed

+212
-78
lines changed

22 files changed

+212
-78
lines changed

backend/api/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ apply plugin: 'io.spring.dependency-management'
2727

2828
repositories {
2929
mavenCentral()
30+
maven { url 'https://jitpack.io' }
3031
}
3132

3233
kotlin {

backend/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ apply plugin: "org.gradle.test-retry"
4646

4747
repositories {
4848
mavenCentral()
49+
maven { url 'https://jitpack.io' }
4950
}
5051

5152
dependencyManagement {

backend/data/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ apply plugin: 'org.hibernate.orm'
5252

5353
repositories {
5454
mavenCentral()
55+
maven { url 'https://jitpack.io' }
5556
}
5657

5758
idea {
@@ -99,6 +100,9 @@ dependencies {
99100
implementation "org.springframework.boot:spring-boot-starter-batch"
100101
implementation "org.springframework.boot:spring-boot-starter-websocket"
101102
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
103+
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
104+
105+
implementation 'com.github.transferwise:spring-icu:0.3.0'
102106

103107
/**
104108
* DB

backend/data/src/main/kotlin/io/tolgee/email/EmailTemplateConfig.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package io.tolgee.email
1818

19+
import com.transferwise.icu.ICUMessageSource
20+
import com.transferwise.icu.ICUReloadableResourceBundleMessageSource
1921
import org.springframework.beans.factory.annotation.Qualifier
2022
import org.springframework.context.MessageSource
2123
import org.springframework.context.annotation.Bean
2224
import org.springframework.context.annotation.Configuration
23-
import org.springframework.context.support.ResourceBundleMessageSource
2425
import org.thymeleaf.TemplateEngine
2526
import org.thymeleaf.spring6.SpringTemplateEngine
2627
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
@@ -39,13 +40,11 @@ class EmailTemplateConfig {
3940
}
4041

4142
@Bean("emailMessageSource")
42-
fun messageSource(): MessageSource {
43-
val messageSource = ResourceBundleMessageSource()
44-
messageSource.setBasename("email-i18n.messages")
43+
fun messageSource(): ICUMessageSource {
44+
val messageSource = ICUReloadableResourceBundleMessageSource()
45+
messageSource.setBasenames("email-i18n/messages", "email-i18n-test/messages")
4546
messageSource.setDefaultEncoding("UTF-8")
4647
messageSource.setDefaultLocale(Locale.ENGLISH)
47-
println(messageSource.getMessage("powered-by", null, Locale.ENGLISH))
48-
println(messageSource.getMessage("powered-by", null, Locale.FRENCH))
4948
return messageSource
5049
}
5150

@@ -55,6 +54,7 @@ class EmailTemplateConfig {
5554
@Qualifier("emailMessageSource") messageSource: MessageSource,
5655
): TemplateEngine {
5756
val templateEngine = SpringTemplateEngine()
57+
templateEngine.enableSpringELCompiler = true
5858
templateEngine.templateResolvers = setOf(templateResolver)
5959
templateEngine.setTemplateEngineMessageSource(messageSource)
6060
return templateEngine
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../../../../email/out
1+
../../../../../email/dist/email-templates

backend/data/src/test/kotlin/io/tolgee/email/EmailServiceTest.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ class EmailServiceTest {
7676
.let { it.getBodyPart(0).content as MimeMultipart }
7777
.let { it.getBodyPart(0).content as String }
7878
.assert
79-
.contains("Value of `testVar` : <span>test!!</span>")
79+
.contains("Testing ICU strings -- test!!")
80+
.contains("Value of `testVar`: <span>test!!</span>")
8081
.contains("<span>Was `testVar` equal to &quot;meow&quot; : </span><span>no</span>")
81-
.contains("Powered by")
82+
// Makes sure tag handling works as expected
83+
.contains(
84+
"Powered by <a href=\"https://tolgee.io\" style=\"color:inherit;text-decoration-line:underline\" " +
85+
"target=\"_blank\">Tolgee</a>",
86+
)
87+
// Might be a bit brittle but does the trick for now.
8288
.doesNotContain(" th:")
8389
.doesNotContain(" data-th")
8490
}
@@ -95,10 +101,31 @@ class EmailServiceTest {
95101
.let { it.getBodyPart(0).content as MimeMultipart }
96102
.let { it.getBodyPart(0).content as String }
97103
.assert
98-
.contains("Value of `testVar` : <span>meow</span>")
104+
.contains("Testing ICU strings -- meow")
105+
.contains("Value of `testVar`: <span>meow</span>")
99106
.contains("<span>Was `testVar` equal to &quot;meow&quot; : </span><span>yes</span>")
100107
}
101108

109+
@Test
110+
fun `it correctly processes foreach blocks`() {
111+
// FWIW this is very close to just testing Thymeleaf itself, but it serves as a sanity check for the template itself
112+
emailService.sendEmailTemplate("[email protected]", "zz-test-email", Locale.ENGLISH, TEST_PROPERTIES_MEOW)
113+
verify(mailSender).send(emailCaptor.capture())
114+
115+
val email = emailCaptor.value
116+
email.content
117+
.let { it as MimeMultipart }
118+
.let { it.getBodyPart(0).content as MimeMultipart }
119+
.let { it.getBodyPart(0).content as String }
120+
.assert
121+
.contains("Plain test: <span>Name #1</span>")
122+
.contains("<span>ICU test: Name #1</span>")
123+
.contains("Plain test: <span>Name #2</span>")
124+
.contains("<span>ICU test: Name #2</span>")
125+
.contains("Plain test: <span>Name #3</span>")
126+
.contains("<span>ICU test: Name #3</span>")
127+
}
128+
102129
companion object {
103130
private val TEST_PROPERTIES =
104131
mapOf(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
email-test-string = Testing ICU strings -- {testVar}
2+
email-test-it = ICU test: {item__name}

backend/development/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ apply plugin: "kotlin-kapt"
3737

3838
repositories {
3939
mavenCentral()
40+
maven { url 'https://jitpack.io' }
4041
}
4142

4243
kotlin {

backend/security/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ apply plugin: 'io.spring.dependency-management'
2323

2424
repositories {
2525
mavenCentral()
26+
maven { url 'https://jitpack.io' }
2627
}
2728

2829
kotlin {

backend/testing/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ apply plugin: "kotlin-kapt"
4343

4444
repositories {
4545
mavenCentral()
46+
maven { url 'https://jitpack.io' }
4647
}
4748

4849
kotlin {

0 commit comments

Comments
 (0)