rbmobLocale

This commit is contained in:
devezhao 2021-06-03 16:15:07 +08:00
parent 7d2b1e57b6
commit 4a1b4c634e
3 changed files with 27 additions and 18 deletions

2
@rbv

@ -1 +1 @@
Subproject commit d5d3c9e9015198dd532329b7157112e292d2b9f7
Subproject commit 68a72229855e74f5c3b484aa7a37c70525d119e3

View file

@ -43,6 +43,10 @@ public class AppUtils {
public static final String SK_LOCALE = WebUtils.KEY_PREFIX + ".LOCALE";
public static final String CK_LOCALE = "rb.locale";
// RbMob
public static final String HF_CLIENT = "X-Client";
public static final String HF_LOCALE = "X-ClientLocale";
/**
* @return
* @see BootApplication#getContextPath()
@ -106,6 +110,9 @@ public class AppUtils {
*/
public static String getReuqestLocale(HttpServletRequest request) {
String locale = (String) ServletUtils.getSessionAttribute(request, SK_LOCALE);
if (locale == null) {
locale = StringUtils.defaultIfBlank(request.getHeader(HF_LOCALE), null);
}
if (locale == null) {
locale = RebuildConfiguration.get(ConfigurationItem.DefaultLanguage);
}
@ -171,7 +178,7 @@ public class AppUtils {
* @return
*/
public static boolean isRbMobile(HttpServletRequest request) {
String UA = request.getHeader("X-Client");
String UA = request.getHeader(HF_CLIENT);
return UA != null && UA.startsWith("RB/Mobile-");
}

View file

@ -189,43 +189,45 @@ public class RebuildWebInterceptor implements AsyncHandlerInterceptor, InstallSt
* @param request
* @param response
* @return
* @see AppUtils#getReuqestLocale(HttpServletRequest)
*/
private String detectLocale(HttpServletRequest request, HttpServletResponse response) {
String rbmobLocale = request.getHeader(AppUtils.HF_LOCALE);
if (rbmobLocale != null) return rbmobLocale;
// 0. Session
String locale = (String) ServletUtils.getSessionAttribute(request, AppUtils.SK_LOCALE);
String havingLocale = (String) ServletUtils.getSessionAttribute(request, AppUtils.SK_LOCALE);
String urlLocale = request.getParameter("locale");
if (StringUtils.isNotBlank(urlLocale) && !urlLocale.equals(locale)) {
if (StringUtils.isNotBlank(urlLocale) && !urlLocale.equals(havingLocale)) {
urlLocale = Application.getLanguage().available(urlLocale);
if (urlLocale != null) {
locale = urlLocale;
havingLocale = urlLocale;
ServletUtils.setSessionAttribute(request, AppUtils.SK_LOCALE, locale);
ServletUtils.addCookie(response, AppUtils.CK_LOCALE, locale,
ServletUtils.setSessionAttribute(request, AppUtils.SK_LOCALE, havingLocale);
ServletUtils.addCookie(response, AppUtils.CK_LOCALE, havingLocale,
CommonsCache.TS_DAY * 90, null, StringUtils.defaultIfBlank(AppUtils.getContextPath(), "/"));
if (Application.devMode()) {
Application.getLanguage().refresh();
}
if (Application.devMode()) Application.getLanguage().refresh();
}
}
if (locale != null) return locale;
if (havingLocale != null) return havingLocale;
// 1. Cookie
locale = ServletUtils.readCookie(request, AppUtils.CK_LOCALE);
if (locale == null) {
havingLocale = ServletUtils.readCookie(request, AppUtils.CK_LOCALE);
if (havingLocale == null) {
// 2. User-Local
locale = request.getLocale().toString();
havingLocale = request.getLocale().toString();
}
// 3. Default
if ((locale = Application.getLanguage().available(locale)) == null) {
locale = RebuildConfiguration.get(ConfigurationItem.DefaultLanguage);
if ((havingLocale = Application.getLanguage().available(havingLocale)) == null) {
havingLocale = RebuildConfiguration.get(ConfigurationItem.DefaultLanguage);
}
ServletUtils.setSessionAttribute(request, AppUtils.SK_LOCALE, locale);
return locale;
ServletUtils.setSessionAttribute(request, AppUtils.SK_LOCALE, havingLocale);
return havingLocale;
}
/**