Skip to content

Commit 0493ec8

Browse files
committed
删除localecontext
1 parent dac1a24 commit 0493ec8

File tree

5 files changed

+25
-151
lines changed

5 files changed

+25
-151
lines changed

framework/fit/java/fit-builtin/services/fit-http-classic/definition/src/main/java/modelengine/fit/http/util/i18n/LocaleResolveFilter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import modelengine.fit.http.server.*;
1010
import modelengine.fitframework.annotation.Scope;
11-
import modelengine.fitframework.util.i18n.LocaleContext;
1211
import modelengine.fitframework.util.i18n.LocaleContextHolder;
1312

1413
import java.util.List;
@@ -74,12 +73,12 @@ public void doFilter(HttpClassicServerRequest request, HttpClassicServerResponse
7473
Locale responseLocale = null;
7574
if (paramLocale != null && !paramLocale.trim().isEmpty()) {
7675
responseLocale = Locale.forLanguageTag(paramLocale);
77-
LocaleContextHolder.setLocaleContext(new LocaleContext(responseLocale));
76+
LocaleContextHolder.setLocale(responseLocale);
7877
}
7978
// 如果参数中不包含地区,则解析请求所带的地区参数。
8079
else {
8180
Locale locale = this.localeResolver.resolveLocale(request);
82-
LocaleContextHolder.setLocaleContext(new LocaleContext(locale));
81+
LocaleContextHolder.setLocale(locale);
8382
}
8483

8584
// 继续执行后续过滤器。

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/util/i18n/LocaleContext.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/util/i18n/LocaleContextHolder.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,32 @@
99
import java.util.Locale;
1010

1111
/**
12-
* 表示存储地区上下文的线程上下文
12+
* 表示存储地区的线程上下文
1313
*
1414
* @author 阮睿
1515
* @since 2025-08-01
1616
*/
1717
public class LocaleContextHolder {
18-
private static final ThreadLocal<LocaleContext> LOCALE_CONTEXT_HOLDER = new ThreadLocal<>();
18+
private static final ThreadLocal<Locale> LOCALE_CONTEXT_HOLDER = new ThreadLocal<>();
1919

2020
/**
2121
* 设置当前线程的地区上下文。
2222
*
23-
* @param localeContext 表示将要存储在当前线程地区上下文的 {@link LocaleContext}。
23+
* @param locale 表示将要存储在当前线程地区上下文的 {@link Locale}。
2424
*/
25-
public static void setLocaleContext(LocaleContext localeContext) {
26-
if (localeContext != null) {
27-
LOCALE_CONTEXT_HOLDER.set(localeContext);
25+
public static void setLocale(Locale locale) {
26+
if (locale != null) {
27+
LOCALE_CONTEXT_HOLDER.set(locale);
2828
}
2929
}
3030

31-
/**
32-
* 获取当前线程的地区上下文。
33-
*
34-
* @return 表示当前线程地区上下文的 {@link LocaleContext}。
35-
*/
36-
public static LocaleContext getLocaleContext() {
37-
return LOCALE_CONTEXT_HOLDER.get();
38-
}
39-
4031
/**
4132
* 获取当前线程的地区。
4233
*
4334
* @return 表示当前线程上下文存储地区信息的 {@link Locale}。
4435
*/
4536
public static Locale getLocale() {
46-
LocaleContext context = getLocaleContext();
47-
return context != null ? context.getLocale() : null;
37+
return LOCALE_CONTEXT_HOLDER.get();
4838
}
4939

5040
/**

framework/fit/java/fit-util/src/test/java/modelengine/fitframework/util/i18n/LocaleContextHolderTest.java

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,55 +29,28 @@ void tearDown() {
2929
}
3030

3131
@Nested
32-
@DisplayName("Test method: setLocaleContext and getLocaleContext")
32+
@DisplayName("Test method: setLocale and getLocale")
3333
class TestSetAndGetLocaleContext {
3434
@Test
35-
@DisplayName("Given locale context with zh_CN then return the same locale context")
35+
@DisplayName("Given locale with zh_CN then return the same locale")
3636
void givenLocaleContextWithZhCNThenReturnSameLocaleContext() {
37-
LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE);
38-
LocaleContextHolder.setLocaleContext(localeContext);
39-
assertThat(LocaleContextHolder.getLocaleContext()).isEqualTo(localeContext);
37+
Locale locale = Locale.SIMPLIFIED_CHINESE;
38+
LocaleContextHolder.setLocale(locale);
39+
assertThat(LocaleContextHolder.getLocale()).isEqualTo(locale);
4040
}
4141

4242
@Test
43-
@DisplayName("Given locale context with en_US then return the same locale context")
43+
@DisplayName("Given locale with en_US then return the same locale")
4444
void givenLocaleContextWithEnUSThenReturnSameLocaleContext() {
45-
LocaleContext localeContext = new LocaleContext(Locale.US);
46-
LocaleContextHolder.setLocaleContext(localeContext);
47-
assertThat(LocaleContextHolder.getLocaleContext()).isEqualTo(localeContext);
45+
Locale locale = Locale.US;
46+
LocaleContextHolder.setLocale(locale);
47+
assertThat(LocaleContextHolder.getLocale()).isEqualTo(locale);
4848
}
4949

5050
@Test
51-
@DisplayName("Given null locale context then not set and return null")
51+
@DisplayName("Given null locale then not set and return null")
5252
void givenNullLocaleContextThenReturnNull() {
53-
LocaleContextHolder.setLocaleContext(null);
54-
assertThat(LocaleContextHolder.getLocaleContext()).isNull();
55-
}
56-
}
57-
58-
@Nested
59-
@DisplayName("Test method: getLocale")
60-
class TestGetLocale {
61-
@Test
62-
@DisplayName("Given locale context with zh_CN then return zh_CN locale")
63-
void givenLocaleContextWithZhCNThenReturnZhCNLocale() {
64-
LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE);
65-
LocaleContextHolder.setLocaleContext(localeContext);
66-
assertThat(LocaleContextHolder.getLocale()).isEqualTo(Locale.SIMPLIFIED_CHINESE);
67-
}
68-
69-
@Test
70-
@DisplayName("Given locale context with en_US then return en_US locale")
71-
void givenLocaleContextWithEnUSThenReturnEnUSLocale() {
72-
LocaleContext localeContext = new LocaleContext(Locale.US);
73-
LocaleContextHolder.setLocaleContext(localeContext);
74-
assertThat(LocaleContextHolder.getLocale()).isEqualTo(Locale.US);
75-
}
76-
77-
@Test
78-
@DisplayName("Given no locale context then return null")
79-
void givenNoLocaleContextThenReturnNull() {
80-
LocaleContextHolder.clear();
53+
LocaleContextHolder.setLocale(null);
8154
assertThat(LocaleContextHolder.getLocale()).isNull();
8255
}
8356
}
@@ -86,14 +59,14 @@ void givenNoLocaleContextThenReturnNull() {
8659
@DisplayName("Test method: clear")
8760
class TestClear {
8861
@Test
89-
@DisplayName("Given existing locale context then clear it")
62+
@DisplayName("Given existing locale then clear it")
9063
void givenExistingLocaleContextThenClearIt() {
91-
LocaleContext localeContext = new LocaleContext(Locale.SIMPLIFIED_CHINESE);
92-
LocaleContextHolder.setLocaleContext(localeContext);
93-
assertThat(LocaleContextHolder.getLocaleContext()).isNotNull();
64+
Locale locale = Locale.SIMPLIFIED_CHINESE;
65+
LocaleContextHolder.setLocale(locale);
66+
assertThat(LocaleContextHolder.getLocale()).isNotNull();
9467

9568
LocaleContextHolder.clear();
96-
assertThat(LocaleContextHolder.getLocaleContext()).isNull();
69+
assertThat(LocaleContextHolder.getLocale()).isNull();
9770
}
9871
}
9972
}

framework/fit/java/fit-util/src/test/java/modelengine/fitframework/util/i18n/LocaleContextTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)