-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from jamebal/i18n
feat: add i18n config
- Loading branch information
Showing
14 changed files
with
413 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/jmal/clouddisk/interceptor/HeaderLocaleChangeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.jmal.clouddisk.interceptor; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; | ||
|
||
import java.util.Locale; | ||
|
||
public class HeaderLocaleChangeInterceptor extends LocaleChangeInterceptor { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) { | ||
String lang = request.getHeader("lang"); | ||
if (StrUtil.isNotBlank(lang) && lang.indexOf("_") > 0) { | ||
Locale locale = Locale.forLanguageTag(lang.replace("_", "-")); | ||
LocaleContextHolder.setLocale(locale); | ||
} else { | ||
LocaleContextHolder.setLocale(Locale.US); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.jmal.clouddisk.util; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.NoSuchMessageException; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Locale; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MessageUtil { | ||
|
||
private final MessageSource messageSource; | ||
|
||
/** | ||
* 获取国际化消息 | ||
* @param messageKey 消息的键 | ||
* @return 国际化消息 | ||
*/ | ||
public String getMessage(String messageKey) { | ||
return getMessage(messageKey, LocaleContextHolder.getLocale()); | ||
} | ||
|
||
/** | ||
* 获取国际化消息 | ||
* @param messageKey 消息的键 | ||
* @param locale 语言 | ||
* @return 国际化消息 | ||
*/ | ||
public String getMessage(String messageKey, Locale locale) { | ||
String result = messageKey; | ||
try { | ||
result = messageSource.getMessage(messageKey, null, locale); | ||
} catch (NoSuchMessageException ignored) { | ||
} | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.