Skip to content

Commit a7d9db7

Browse files
committed
feat(i18n): i18n 支持
1 parent 7f3f7d1 commit a7d9db7

File tree

8 files changed

+57
-21
lines changed

8 files changed

+57
-21
lines changed

easyfile-common/src/main/java/com/openquartz/easyfile/common/i18n/I18nTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static String translate(String template) {
4242
}
4343

4444
// 获取当前语言
45-
Locale locale = LocaleContextHolder.currentLocale();
45+
Locale locale = LocaleContext.currentLocale();
4646

4747
MessageSource messageSource = SpringContextUtil.getBean(MessageSource.class);
4848
return messageSource.getMessage(key, null, locale);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.openquartz.easyfile.common.i18n;
22

33
import com.openquartz.easyfile.common.util.StringUtils;
4-
import org.springframework.core.NamedThreadLocal;
4+
import org.springframework.context.i18n.LocaleContextHolder;
55
import org.springframework.lang.Nullable;
66

77
import java.util.Locale;
@@ -13,31 +13,29 @@
1313
*
1414
* @author svnee
1515
*/
16-
public class LocaleContextHolder {
16+
public class LocaleContext {
1717

18-
private static final ThreadLocal<Locale> currentLocaleContext = new NamedThreadLocal<>("Current Locale");
19-
20-
private LocaleContextHolder() {
18+
private LocaleContext() {
2119
}
2220

2321
public static Locale currentLocale() throws IllegalStateException {
24-
return currentLocaleContext.get();
22+
return LocaleContextHolder.getLocale();
2523
}
2624

2725
public static String currentLocaleLanguage() throws IllegalStateException {
28-
Locale locale = currentLocaleContext.get();
29-
return Optional.ofNullable(locale).map(Locale::getLanguage).orElse(StringUtils.EMPTY);
26+
Locale locale = currentLocale();
27+
return Optional.of(locale).map(Locale::getLanguage).orElse(StringUtils.EMPTY);
3028
}
3129

3230
public static void setCurrentLocale(@Nullable Locale locale) {
3331

3432
if (Objects.nonNull(locale)) {
35-
currentLocaleContext.set(locale);
33+
LocaleContextHolder.setLocale(locale);
3634
}
3735

3836
}
3937

4038
public static void clear() {
41-
currentLocaleContext.remove();
39+
LocaleContextHolder.resetLocaleContext();
4240
}
4341
}

easyfile-core/src/main/java/com/openquartz/easyfile/core/intercept/I18nDownloadExecutorInterceptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.openquartz.easyfile.core.intercept;
22

33
import com.openquartz.easyfile.common.bean.BaseDownloaderRequestContext;
4-
import com.openquartz.easyfile.common.i18n.LocaleContextHolder;
4+
import com.openquartz.easyfile.common.i18n.LocaleContext;
55
import com.openquartz.easyfile.common.response.ExportResult;
66
import com.openquartz.easyfile.core.executor.BaseDownloadExecutor;
77
import com.openquartz.easyfile.storage.download.DownloadStorageService;
@@ -30,12 +30,12 @@ public int order() {
3030
public void beforeExecute(BaseDownloadExecutor executor, BaseDownloaderRequestContext context, Long registerId, InterceptorContext interceptorContext) {
3131

3232
Locale currentLocale = downloadStorageService.getCurrentLocale(registerId);
33-
LocaleContextHolder.setCurrentLocale(currentLocale);
33+
LocaleContext.setCurrentLocale(currentLocale);
3434

3535
}
3636

3737
@Override
3838
public void afterExecute(BaseDownloadExecutor executor, BaseDownloaderRequestContext context, ExportResult result, InterceptorContext interceptorContext) {
39-
LocaleContextHolder.clear();
39+
LocaleContext.clear();
4040
}
4141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.openquartz.easyfile.example.config;
2+
3+
import org.springframework.context.MessageSource;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
7+
8+
@Configuration
9+
public class I18nConfig {
10+
11+
@Bean
12+
public MessageSource messageSource() {
13+
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
14+
messageSource.setBasename("classpath:i18n/messages"); // 设置基础名,指向资源文件的位置
15+
messageSource.setDefaultEncoding("UTF-8"); // 设置默认编码
16+
return messageSource;
17+
}
18+
}

easyfile-example/easyfile-example-local/src/main/java/com/openquartz/easyfile/example/config/WebMvcConfig.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,49 @@
55
import org.springframework.context.annotation.Configuration;
66
import org.springframework.web.multipart.MultipartResolver;
77
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
8+
import org.springframework.web.servlet.LocaleResolver;
89
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
910
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
1011
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
12+
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
13+
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
14+
15+
import java.util.Locale;
1116

1217
@Configuration
1318
public class WebMvcConfig implements WebMvcConfigurer {
1419

1520
@Override
1621
public void addInterceptors(InterceptorRegistry registry) {
17-
registry
18-
.addInterceptor(new WebRequestInterceptor())
19-
.addPathPatterns("/**");
22+
registry.addInterceptor(localeChangeInterceptor());
23+
registry.addInterceptor(new WebRequestInterceptor())
24+
.addPathPatterns("/**");
2025
}
2126

2227
@Override
2328
public void addResourceHandlers(ResourceHandlerRegistry registry) {
2429
registry
25-
.addResourceHandler("/**")
26-
.addResourceLocations("classpath:/static/", "classpath:/META-INF/resources/");
30+
.addResourceHandler("/**")
31+
.addResourceLocations("classpath:/static/", "classpath:/META-INF/resources/");
2732
}
2833

2934
@Bean("multipartResolver")
3035
public MultipartResolver mulitipartResovler() {
3136
return new StandardServletMultipartResolver();
3237
}
38+
39+
@Bean
40+
public LocaleResolver localeResolver() {
41+
SessionLocaleResolver slr = new SessionLocaleResolver();
42+
slr.setDefaultLocale(Locale.US); // 默认语言
43+
return slr;
44+
}
45+
46+
@Bean
47+
public LocaleChangeInterceptor localeChangeInterceptor() {
48+
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
49+
lci.setParamName("lang"); // URL参数名
50+
return lci;
51+
}
52+
3353
}

easyfile-example/easyfile-example-local/src/main/resources/i18n/messages/messages_en.properties

Whitespace-only changes.

easyfile-example/easyfile-example-local/src/main/resources/i18n/messages/messages_zh.properties

Whitespace-only changes.

easyfile-spring-boot-starter/easyfile-spring-boot-starter-parent/src/main/java/com/openquartz/easyfile/starter/aop/FileExportInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static com.openquartz.easyfile.core.exception.DownloadErrorCode.FILE_GENERATOR_MUST_SUPPORT_ANNOTATION;
44
import static com.openquartz.easyfile.core.exception.DownloadErrorCode.SYNC_DOWNLOAD_EXECUTE_ERROR;
55

6-
import com.openquartz.easyfile.common.i18n.LocaleContextHolder;
6+
import com.openquartz.easyfile.common.i18n.LocaleContext;
77
import com.openquartz.easyfile.common.util.StringUtils;
88
import com.openquartz.easyfile.core.executor.support.FileExportTriggerContext;
99
import com.openquartz.easyfile.starter.spring.boot.autoconfig.properties.EasyFileDownloadProperties;
@@ -240,7 +240,7 @@ private RegisterDownloadRequest buildDownloadRequest(FileExportExecutor executor
240240
registerRequest.setFileSuffix(requestContext.getFileSuffix());
241241
registerRequest.setOtherMap(requestContext.getOtherMap());
242242
registerRequest.setMaxServerRetry(executor.maxServerRetry());
243-
registerRequest.setLocale(LocaleContextHolder.currentLocaleLanguage());
243+
registerRequest.setLocale(LocaleContext.currentLocaleLanguage());
244244
return registerRequest;
245245
}
246246
}

0 commit comments

Comments
 (0)