Skip to content

Commit

Permalink
perf($Starter): support internationalization (i18n)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 19, 2021
1 parent e07452b commit 1c8b38b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import lombok.val;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
Expand All @@ -40,16 +42,17 @@
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPersistence> implements UserService {
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final JwtService jwtService;
private final MessageSource messageSource;

@Override
@Cacheable
public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginToken) {
LambdaQueryWrapper<UserPersistence> wrapper = Wrappers.lambdaQuery();
wrapper.and(queryWrapper -> queryWrapper.eq(UserPersistence::getUsername, loginToken)
.or()
.eq(UserPersistence::getEmail, loginToken)
.or()
.eq(UserPersistence::getCellphone, loginToken));
.or()
.eq(UserPersistence::getEmail, loginToken)
.or()
.eq(UserPersistence::getCellphone, loginToken));
val userPersistence = this.getBaseMapper().selectOne(wrapper);
if (ObjectUtil.isNull(userPersistence)) {
return null;
Expand Down Expand Up @@ -87,6 +90,7 @@ public LoginResponse login(@Valid LoginPayload payload) throws SecurityException
if (matched) {
String jwt = jwtService.createJwt(payload.getRememberMe(), user.getId(), user.getUsername(), null, null);
val response = new LoginResponse();
response.setGreeting(messageSource.getMessage(("greeting"), null, LocaleContextHolder.getLocale()));
response.setJwt(jwt);
return response;
}
Expand Down
1 change: 1 addition & 0 deletions auth-center/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greeting=Hello!
1 change: 1 addition & 0 deletions auth-center/src/main/resources/messages_zh_CN.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
greeting=你好!
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
**/
@Data
public class LoginResponse {
private String greeting;
private String jwt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
@ConditionalOnWebApplication
@EnableConfigurationProperties({MafConfiguration.class, MafProjectProperty.class})
@Import({
WebMvcConfiguration.class,
MyBatisPlusConfiguration.class,
RedisConfiguration.class,
Swagger2Configuration.class,
Expand Down Expand Up @@ -104,13 +105,6 @@ public SpringBootStartupHelper springBootStartupHelper(MafProjectProperty mafPro
return new SpringBootStartupHelper(mafProjectProperty, ipHelper);
}

@Bean
@ConditionalOnMissingBean
public WebMvcConfiguration webMvcConfiguration() {
log.warn("Initial bean: '{}'", WebMvcConfiguration.class.getSimpleName());
return new WebMvcConfiguration();
}

@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public GlobalErrorController globalErrorController(ErrorAttributes errorAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* <p>
* Ignored request configuration.
*
* TODO: make prefix under "maf."
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com
* @date 5/2/20 11:41 PM
**/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.jmsoftware.maf.springcloudstarter.configuration;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
* <h1>WebMvcConfiguration</h1>
Expand All @@ -14,12 +16,15 @@
* @date 1/23/20 9:02 AM
**/
@Configuration
@RequiredArgsConstructor
public class WebMvcConfiguration implements WebMvcConfigurer {
/**
* Max age: 3600 seconds (1 hour)
*/
private static final long MAX_AGE_SECS = 3600;
/**
* Default name of the locale specification parameter: "lang".
*/
private static final String DEFAULT_PARAM_NAME = "lang";

/**
* Configure cross origin requests processing.
Expand All @@ -33,4 +38,25 @@ public void addCorsMappings(CorsRegistry registry) {
.allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
.maxAge(MAX_AGE_SECS);
}

/**
* An interceptor bean that will switch to a new locale based on the value of the lang parameter appended to a
* request.
*
* @return the locale change interceptor
* @see
* <a href='https://www.baeldung.com/spring-boot-internationalization#localechangeinterceptor'>LocaleChangeInterceptor</a>
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName(DEFAULT_PARAM_NAME);
return localeChangeInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
// In order to take effect, this bean needs to be added to the application's interceptor registry.
registry.addInterceptor(localeChangeInterceptor());
}
}

0 comments on commit 1c8b38b

Please sign in to comment.