Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alaugks/spring-requesturi-locale-interceptor
Handling Locale as first part of RequestURI in Spring MVC
https://github.com/alaugks/spring-requesturi-locale-interceptor
locale localization spring spring-boot springmvc
Last synced: about 1 month ago
JSON representation
Handling Locale as first part of RequestURI in Spring MVC
- Host: GitHub
- URL: https://github.com/alaugks/spring-requesturi-locale-interceptor
- Owner: alaugks
- Created: 2024-04-28T15:01:49.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-07-07T17:07:31.000Z (4 months ago)
- Last Synced: 2024-09-29T08:22:10.186Z (about 2 months ago)
- Topics: locale, localization, spring, spring-boot, springmvc
- Language: Java
- Homepage:
- Size: 44.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RequestURI Locale Interceptor for Spring
Handling Locale as first part of [RequestURI](https://jakarta.ee/specifications/servlet/6.0/apidocs/jakarta.servlet/jakarta/servlet/http/httpservletrequest#getRequestURI()).
Example:
```
HTTP Request RequestURIhttps://foo.bar/{locale}/some/path.html -> /{locale}/some/path.html
https://foo.bar/{locale}/a.html -> /{locale}/a.html
https://foo.bar/{locale}/xyz?a=b -> /{locale}/xyz
```An example in action can be seen [here](https://spring-boot-xliff-example.alaugks.dev/).
## Versions
| Version | Description |
|:--------|:--------------------------------------------------------------------------------------------------------------------------|
| 0.3.0 | [Release notes](https://github.com/alaugks/spring-requesturi-locale-interceptor/releases/tag/0.3.0) |
| 0.2.0 | [Release notes](https://github.com/alaugks/spring-requesturi-locale-interceptor/releases/tag/0.2.0) / **Breaking Change** |
| 0.1.0 | [Release notes](https://github.com/alaugks/spring-requesturi-locale-interceptor/releases/tag/0.1.0) |[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=alaugks_spring-url-path-locale-interceptor&metric=alert_status)](https://sonarcloud.io/summary/overall?id=alaugks_spring-url-path-locale-interceptor) [![Maven Central](https://img.shields.io/maven-central/v/io.github.alaugks/spring-requesturi-locale-interceptor.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.alaugks/spring-requesturi-locale-interceptor/0.3.0)
## Dependency
### Maven
```xml
io.github.alaugks
spring-requesturi-locale-interceptor
0.3.0```
### Gradle
```
implementation group: 'io.github.alaugks', name: 'spring-requesturi-locale-interceptor', version: '0.3.0'
```## Configuration
### Options
Options
Description
Required
builder(Locale defaultLocale)
Default and fallback Locale.
Yes
supportedLocales(List<Locale> locales)
List all locales that are supported.
No
defaultRequestURI(String path)
If the RequestURI is empty, a redirect to the path is performed.
No (If not set the default RequestURI is /{defaultLocale}.)
### Spring Configuration
```java
import io.github.alaugks.spring.requesturilocaleinterceptor.RequestURILocaleInterceptor;
import io.github.alaugks.spring.requesturilocaleinterceptor.RequestURILocaleResolver;
import java.util.List;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebMvcConfigurerConfig implements WebMvcConfigurer {private final Locale defaultLocale = Locale.forLanguageTag("en");
private final List supportedLocales = List.of(
Locale.forLanguageTag("en"),
Locale.forLanguageTag("de"),
Locale.forLanguageTag("en-US")
);@Bean
public LocaleResolver localeResolver() {
RequestURILocaleResolver resolver = new RequestURILocaleResolver();
resolver.setDefaultLocale(this.defaultLocale);
return resolver;
}@Override
public void addInterceptors(InterceptorRegistry registry) {
RequestURILocaleInterceptor interceptor = RequestURILocaleInterceptor
.builder(this.defaultLocale)
.supportedLocales(this.supportedLocales)
.defaultRequestURI("/en/home")
.build();
registry.addInterceptor(urlInterceptor)
.addPathPatterns("/**")
// Exclude from Interceptor
.excludePathPatterns("/static/**", "/error");
}
}
```