terminado

This commit is contained in:
Jaime Jiménez
2025-09-11 12:15:56 +02:00
parent 6a9c197a02
commit 67b5f9457e
15 changed files with 311 additions and 91 deletions

View File

@ -22,15 +22,20 @@ public class InternationalizationConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.of("es"));
slr.setDefaultLocale(Locale.forLanguageTag("es")); // idioma por defecto
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang"); // parámetro ?lang=en, ?lang=es
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
registry.addInterceptor(interceptor);
registry.addInterceptor(localeChangeInterceptor());
}
@Bean
@ -40,22 +45,22 @@ public class InternationalizationConfig implements WebMvcConfigurer {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:i18n/*.properties");
// Extraer los nombres base sin extensión ni sufijos (_en, _es, etc.)
// Extraer nombres base sin sufijos de idioma
Set<String> basenames = Arrays.stream(resources)
.map(res -> {
try {
String uri = Objects.requireNonNull(res.getURI()).toString();
// Ej: file:/.../i18n/login_en.properties
String path = uri.substring(uri.indexOf("/i18n/") + 1); // i18n/login_en.properties
String base = path.replaceAll("_[a-z]{2}\\.properties$", "") // login.properties
.replaceAll("\\.properties$", "");
return "classpath:" + base;
} catch (IOException e) {
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
.map(res -> {
try {
String uri = Objects.requireNonNull(res.getURI()).toString();
// Ejemplo: file:/.../i18n/login_en.properties
String path = uri.substring(uri.indexOf("/i18n/") + 1); // i18n/login_en.properties
String base = path.replaceAll("_[a-z]{2}\\.properties$", "") // login.properties
.replaceAll("\\.properties$", "");
return "classpath:" + base;
} catch (IOException e) {
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
source.setBasenames(basenames.toArray(new String[0]));
source.setDefaultEncoding("UTF-8");