Skip to content

Commit fea1798

Browse files
i18n review (#60)
* update version * feat: add translation of templates with defaultLang env variable * tests * remove logs * update changelog
1 parent 4efac62 commit fea1798

19 files changed

+863
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [v2.0.0](https://github.com/in2workspace/in2-verifier-api/releases/tag/v2.0.0)
8+
### Added
9+
- Get default language from configuration, use it to translate HTML templates.
10+
711
## [v2.0.0](https://github.com/in2workspace/in2-verifier-api/releases/tag/v2.0.0)
812
- New major version to align with the new major version of EUDIStack project.
913

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group = 'es.in2'
13-
version = '2.0.1'
13+
version = '2.0.2'
1414

1515
java {
1616
toolchain {

src/main/java/es/in2/vcverifier/config/FrontendConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public String getFaviconSrc() {
4646
return defaultIfBlank(properties.faviconSrc(), "dome_logo_favicon.png");
4747
}
4848

49+
public String getDefaultLang() { return defaultIfBlank(properties.defaultLang(), "en"); }
50+
4951
private String defaultIfBlank(String value, String defaultValue) {
5052
return (value == null || value.trim().isEmpty()) ? defaultValue : value;
5153
}

src/main/java/es/in2/vcverifier/config/properties/FrontendProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public record FrontendProperties(
1313
@NotNull @NestedConfigurationProperty Urls urls,
1414
@NestedConfigurationProperty Colors colors,
1515
@NotBlank String logoSrc,
16-
String faviconSrc) {
16+
String faviconSrc,
17+
String defaultLang) {
1718

1819
public record Urls(
1920
@NotBlank @URL String onboarding,

src/main/java/es/in2/vcverifier/controller/ClientErrorController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public String showErrorPage(@RequestParam("errorCode") String errorCode,
3535
model.addAttribute("secondaryContrast", frontendConfig.getSecondaryContrastColor());
3636
model.addAttribute("faviconSrc", frontendConfig.getFaviconSrc());
3737
// Return the view name
38-
return "client-authentication-error";
38+
return "client-authentication-error-" + frontendConfig.getDefaultLang();
3939
}
4040

4141
}

src/main/java/es/in2/vcverifier/controller/LoginQrController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public String showQrLogin(@RequestParam("authRequest") String authRequest, @Requ
4949
} catch (Exception e) {
5050
throw new QRCodeGenerationException(e.getMessage());
5151
}
52-
return "login";
52+
return "login-" + frontendConfig.getDefaultLang();
5353
}
5454

5555
private String generateQRCodeImageBase64(String barcodeText) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package es.in2.vcverifier.service;
2+
3+
public interface TranslationService {
4+
public String getLocale();
5+
public String translate(String code, Object... args);
6+
}
7+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package es.in2.vcverifier.service.impl;
2+
3+
import es.in2.vcverifier.config.FrontendConfig;
4+
import es.in2.vcverifier.service.TranslationService;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.context.MessageSource;
8+
import org.springframework.context.NoSuchMessageException;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.util.List;
12+
import java.util.Locale;
13+
14+
@Slf4j
15+
@Service
16+
@RequiredArgsConstructor
17+
public class TranslationServiceImpl implements TranslationService {
18+
19+
private final FrontendConfig frontendConfig;
20+
private final MessageSource messageSource;
21+
22+
private static final List<String> SUPPORTED_LANGS = List.of("en", "es");
23+
24+
@Override
25+
public String getLocale() {
26+
String locale = frontendConfig.getDefaultLang();
27+
28+
if (locale == null || locale.isBlank()) {
29+
log.warn("No default language configured. Using fallback: 'en'");
30+
return "en";
31+
}
32+
33+
locale = locale.trim().toLowerCase();
34+
35+
if (!SUPPORTED_LANGS.contains(locale)) {
36+
log.warn("Unsupported language '{}'. Falling back to 'en'", locale);
37+
return "en";
38+
}
39+
return locale;
40+
}
41+
42+
@Override
43+
public String translate(String code, Object... args) {
44+
var locale = Locale.forLanguageTag(getLocale());
45+
try {
46+
return messageSource.getMessage(code, args, locale);
47+
} catch (NoSuchMessageException e) {
48+
log.warn("Message code '{}' not found for locale {}. Falling back to code.", code, locale);
49+
return code;
50+
}
51+
}
52+
}
53+

src/main/resources/application.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ verifier:
7474
logoSrc:
7575
# Placeholder for favicon source path (OPTIONAL)
7676
faviconSrc:
77+
# Default language for visible texts (OPTIONAL)
78+
defaultLang: en
7779
backend:
7880
# Placeholder for backend URL (REQUIRED)
7981
url:

0 commit comments

Comments
 (0)