Skip to content

Commit 06785ce

Browse files
committed
Reduce example
1 parent 82d8b00 commit 06785ce

17 files changed

Lines changed: 235 additions & 667 deletions

File tree

Readme.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# Example: Spring MessageSource for XLIFF files
1+
# Example: XLIFF MessageSource for Spring
22

3-
* alaugks/spring-messagesource-xliff ([GitHub](https://github.com/alaugks/spring-messagesource-xliff))
4-
* Docker Image: alaugks/spring-boot-xliff-example:
5-
2.0.0.3 ([Docker Hub](https://hub.docker.com/r/alaugks/spring-messagesource-xliff-example/tags))
6-
* [Example Website](https://spring-boot-xliff-example.alaugks.dev)
3+
This example shows how to use [spring-messagesource-xliff](https://github.com/alaugks/spring-messagesource-xliff).
74

85
## Build and Run Docker Image
96

@@ -23,3 +20,10 @@ docker compose restart
2320

2421
> [!NOTE]
2522
> When you restart, the JAR file is rebuilt and the Tomcat is started. This can take a few seconds.
23+
24+
## Related MessageSources and Examples
25+
26+
* [XLIFF MessageSource for Spring](https://github.com/alaugks/spring-messagesource-xliff)
27+
* [JSON MessageSource for Spring](https://github.com/alaugks/spring-messagesource-json)
28+
* [Example: JSON MessageSource for Spring](https://github.com/alaugks/spring-messagesource-json-example)
29+
* [Example: Custom Database Spring MessageSource](https://github.com/alaugks/spring-messagesource-db-example)

pom.xml

Lines changed: 16 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,18 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>io.github.alaugks</groupId>
76
<artifactId>spring-messagesource-xliff-example</artifactId>
8-
97
<version>2.0.0.3</version>
10-
118
<packaging>jar</packaging>
129

1310
<name>${project.groupId}:${project.artifactId}</name>
1411
<description>Example: Spring MessageSource for XLIFF files</description>
1512
<url>https://github.com/alaugks/spring-xliff-translation-example</url>
1613

17-
<properties>
18-
<java.version>17</java.version>
19-
</properties>
20-
2114
<parent>
22-
<groupId>org.springframework.boot</groupId>
15+
<groupId>io.github.alaugks</groupId>
2316
<artifactId>spring-boot-starter-parent</artifactId>
24-
<version>3.3.4</version>
25-
<relativePath/>
17+
<version>3.2.12</version>
2618
</parent>
2719

2820
<dependencies>
@@ -42,7 +34,7 @@
4234
<dependency>
4335
<groupId>io.github.alaugks</groupId>
4436
<artifactId>spring-messagesource-xliff</artifactId>
45-
<version>2.0.0</version>
37+
<version>2.0.1-SNAPSHOT</version>
4638
</dependency>
4739
<dependency>
4840
<groupId>io.github.alaugks</groupId>
@@ -58,157 +50,19 @@
5850
</dependency>
5951
</dependencies>
6052

61-
<profiles>
62-
63-
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
64-
<!-- TEST COVERAGE -->
65-
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
66-
<!-- @see https://howtodoinjava.com/junit5/jacoco-test-coverage/ -->
67-
<!-- mvn test -P test-coverage -->
68-
<profile>
69-
<id>test-coverage</id>
70-
<build>
71-
<plugins>
72-
<plugin>
73-
<groupId>org.jacoco</groupId>
74-
<artifactId>jacoco-maven-plugin</artifactId>
75-
<version>0.8.11</version>
76-
<executions>
77-
<execution>
78-
<id>before-unit-test-execution</id>
79-
<goals>
80-
<goal>prepare-agent</goal>
81-
</goals>
82-
<configuration>
83-
<destFile>${project.build.directory}/jacoco-output/jacoco-unit-tests.exec</destFile>
84-
<propertyName>surefire.jacoco.args</propertyName>
85-
</configuration>
86-
</execution>
87-
<execution>
88-
<id>after-unit-test-execution</id>
89-
<phase>test</phase>
90-
<goals>
91-
<goal>report</goal>
92-
</goals>
93-
<configuration>
94-
<dataFile>${project.build.directory}/jacoco-output/jacoco-unit-tests.exec</dataFile>
95-
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
96-
</configuration>
97-
</execution>
98-
<execution>
99-
<id>before-integration-test-execution</id>
100-
<phase>pre-integration-test</phase>
101-
<goals>
102-
<goal>prepare-agent</goal>
103-
</goals>
104-
<configuration>
105-
<destFile>${project.build.directory}/jacoco-output/jacoco-integration-tests.exec
106-
</destFile>
107-
<propertyName>failsafe.jacoco.args</propertyName>
108-
</configuration>
109-
</execution>
110-
<execution>
111-
<id>after-integration-test-execution</id>
112-
<phase>post-integration-test</phase>
113-
<goals>
114-
<goal>report</goal>
115-
</goals>
116-
<configuration>
117-
<dataFile>${project.build.directory}/jacoco-output/jacoco-integration-tests.exec
118-
</dataFile>
119-
<outputDirectory>
120-
${project.reporting.outputDirectory}/jacoco-integration-test-coverage-report
121-
</outputDirectory>
122-
</configuration>
123-
</execution>
124-
<execution>
125-
<id>merge-unit-and-integration</id>
126-
<phase>post-integration-test</phase>
127-
<goals>
128-
<goal>merge</goal>
129-
</goals>
130-
<configuration>
131-
<fileSets>
132-
<fileSet>
133-
<directory>${project.build.directory}/jacoco-output/</directory>
134-
<includes>
135-
<include>*.exec</include>
136-
</includes>
137-
</fileSet>
138-
</fileSets>
139-
<destFile>${project.build.directory}/jacoco-output/merged.exec</destFile>
140-
</configuration>
141-
</execution>
142-
<execution>
143-
<id>create-merged-report</id>
144-
<phase>post-integration-test</phase>
145-
<goals>
146-
<goal>report</goal>
147-
</goals>
148-
<configuration>
149-
<dataFile>${project.build.directory}/jacoco-output/merged.exec</dataFile>
150-
<outputDirectory>
151-
${project.reporting.outputDirectory}/jacoco-merged-test-coverage-report
152-
</outputDirectory>
153-
</configuration>
154-
</execution>
155-
<execution>
156-
<id>check</id>
157-
<phase>verify</phase>
158-
<goals>
159-
<goal>check</goal>
160-
</goals>
161-
<configuration>
162-
<rules>
163-
<rule>
164-
<element>CLASS</element>
165-
<excludes>
166-
<exclude>*Test</exclude>
167-
<exclude>*IT</exclude>
168-
</excludes>
169-
<limits>
170-
<limit>
171-
<counter>LINE</counter>
172-
<value>COVEREDRATIO</value>
173-
<minimum>100%</minimum>
174-
</limit>
175-
</limits>
176-
</rule>
177-
</rules>
178-
<dataFile>${project.build.directory}/jacoco-output/merged.exec</dataFile>
179-
</configuration>
180-
</execution>
181-
</executions>
182-
</plugin>
183-
<plugin>
184-
<groupId>org.apache.maven.plugins</groupId>
185-
<artifactId>maven-surefire-plugin</artifactId>
186-
<version>2.22.2</version>
187-
<configuration>
188-
<argLine>${surefire.jacoco.args}</argLine>
189-
</configuration>
190-
</plugin>
191-
<plugin>
192-
<groupId>org.apache.maven.plugins</groupId>
193-
<artifactId>maven-failsafe-plugin</artifactId>
194-
<version>2.22.2</version>
195-
<configuration>
196-
<argLine>${failsafe.jacoco.args}</argLine>
197-
</configuration>
198-
<executions>
199-
<execution>
200-
<goals>
201-
<goal>integration-test</goal>
202-
<goal>verify</goal>
203-
</goals>
204-
</execution>
205-
</executions>
206-
</plugin>
207-
</plugins>
208-
</build>
209-
</profile>
210-
211-
</profiles>
53+
<repositories>
54+
<repository>
55+
<id>maven-snapshots</id>
56+
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
57+
<layout>default</layout>
58+
<releases>
59+
<enabled>false</enabled>
60+
</releases>
61+
<snapshots>
62+
<enabled>true</enabled>
63+
</snapshots>
64+
</repository>
65+
</repositories>
21266

21367
<build>
21468
<finalName>application</finalName>

src/main/java/io/github/alaugks/config/MessageSourceConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.alaugks.config;
22

3+
import io.github.alaugks.spring.messagesource.catalog.resources.LocationPattern;
34
import java.util.Locale;
45

56
import io.github.alaugks.spring.messagesource.xliff.XliffResourceMessageSource;
@@ -14,7 +15,10 @@ public class MessageSourceConfig {
1415
@Bean
1516
public MessageSource messageSource() {
1617
return XliffResourceMessageSource
17-
.builder(Locale.forLanguageTag("en"), "translations/*")
18+
.builder(
19+
Locale.forLanguageTag("en"),
20+
new LocationPattern("translations/*")
21+
)
1822
.build();
1923
}
2024
}

src/main/java/io/github/alaugks/controller/HomeController.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/main/java/io/github/alaugks/controller/IndexController.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@
22

33
import jakarta.servlet.http.HttpServletRequest;
44

5+
import java.util.Locale;
6+
import org.springframework.context.MessageSource;
7+
import org.springframework.context.NoSuchMessageException;
58
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
610
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
712
import org.springframework.web.servlet.ModelAndView;
813

914
@Controller
1015
public class IndexController {
1116

17+
private final MessageSource messageSource;
18+
19+
public IndexController(MessageSource messageSource) {
20+
this.messageSource = messageSource;
21+
}
22+
1223
@GetMapping("/")
1324
public ModelAndView index(HttpServletRequest request) {
1425
// Fallback. Redirect is by RequestURILocaleInterceptor.
1526
String langCode = request.getLocale().toString();
1627
return new ModelAndView("redirect:" + String.format("/%s/home", langCode));
1728
}
29+
30+
@GetMapping(value = "/{lang}/home", name = "home_index")
31+
public String index(Model model, Locale locale, @PathVariable String lang) {
32+
33+
try {
34+
model.addAttribute("headline", this.messageSource.getMessage("headline", null, locale));
35+
model.addAttribute("messages_headline", this.messageSource.getMessage("messages.headline", null, locale));
36+
model.addAttribute("postcode", this.messageSource.getMessage("postcode", null, locale));
37+
model.addAttribute("messages_postcode", this.messageSource.getMessage("messages.postcode", null, locale));
38+
model.addAttribute("payment_headline", this.messageSource.getMessage("payment.headline", null, locale));
39+
model.addAttribute("payment_expiry_date", this.messageSource.getMessage("payment.expiry_date", null, locale));
40+
}
41+
catch (NoSuchMessageException ignored) {}
42+
43+
return "home/index";
44+
}
1845
}

0 commit comments

Comments
 (0)