🎨 支持后台切换语言

pull/33/merge
ruibaby 2018-09-08 13:26:52 +08:00
parent 55d3f20e4c
commit a95321a809
12 changed files with 159 additions and 36 deletions

View File

@ -2,13 +2,19 @@ package cc.ryanc.halo.config;
import cc.ryanc.halo.web.interceptor.ApiInterceptor;
import cc.ryanc.halo.web.interceptor.InstallInterceptor;
import cc.ryanc.halo.web.interceptor.LocaleInterceptor;
import cc.ryanc.halo.web.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
/**
* <pre>
@ -34,6 +40,9 @@ public class MvcConfig implements WebMvcConfigurer {
@Autowired
private ApiInterceptor apiInterceptor;
@Autowired
private LocaleInterceptor localeInterceptor;
/**
*
*
@ -54,6 +63,9 @@ public class MvcConfig implements WebMvcConfigurer {
.excludePathPatterns("/static/**");
registry.addInterceptor(apiInterceptor)
.addPathPatterns("/api/**");
registry.addInterceptor(localeInterceptor)
.addPathPatterns("/admin/**")
.addPathPatterns("/install");
}
/**
@ -84,4 +96,11 @@ public class MvcConfig implements WebMvcConfigurer {
.allowedOrigins("*")
.allowedMethods("*");
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.CHINA);
return slr;
}
}

View File

@ -10,6 +10,11 @@ package cc.ryanc.halo.model.enums;
*/
public enum BlogPropertiesEnum {
/**
*
*/
BLOG_LOCALE("blog_locale"),
/**
*
*/

View File

@ -0,0 +1,29 @@
package cc.ryanc.halo.model.enums;
/**
* @author : wangry
* @version : 1.0
* @date : 20180908
*/
public enum LocaleEnum {
/**
*
*/
ZH_CN("zh_CN"),
/**
*
*/
EN_US("en_US");
private String value;
LocaleEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}

View File

@ -83,6 +83,7 @@ public class InstallController {
/**
*
*
* @param blogLocale
* @param siteTitle
* @param siteUrl
* @param userName
@ -94,7 +95,8 @@ public class InstallController {
*/
@PostMapping(value = "/do")
@ResponseBody
public boolean doInstall(@RequestParam("blogTitle") String blogTitle,
public boolean doInstall(@RequestParam("blogLocale") String blogLocale,
@RequestParam("blogTitle") String blogTitle,
@RequestParam("blogUrl") String blogUrl,
@RequestParam("userName") String userName,
@RequestParam("userDisplayName") String userDisplayName,
@ -157,6 +159,8 @@ public class InstallController {
optionsService.saveOption(BlogPropertiesEnum.IS_INSTALL.getProp(), TrueFalseEnum.TRUE.getDesc());
//语言设置
optionsService.saveOption(BlogPropertiesEnum.BLOG_LOCALE.getProp(),blogLocale);
//保存博客标题和博客地址设置
optionsService.saveOption(BlogPropertiesEnum.BLOG_TITLE.getProp(), blogTitle);
optionsService.saveOption(BlogPropertiesEnum.BLOG_URL.getProp(), blogUrl);

View File

@ -21,6 +21,7 @@ import javax.servlet.http.HttpServletResponse;
*/
@Component
public class ApiInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (StringUtils.equals(TrueFalseEnum.TRUE.getDesc(), HaloConst.OPTIONS.get(BlogPropertiesEnum.API_STATUS.getProp()))) {

View File

@ -0,0 +1,43 @@
package cc.ryanc.halo.web.interceptor;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.model.enums.LocaleEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* @author : wangry
* @version : 1.0
* @date : 20180908
*/
@Component
public class LocaleInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (StringUtils.equals(LocaleEnum.EN_US.getValue(), HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_LOCALE.getProp()))) {
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
} else {
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}

View File

@ -18,6 +18,7 @@ import javax.servlet.http.HttpServletResponse;
*/
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object obj = request.getSession().getAttribute(HaloConst.USER_SESSION_KEY);

View File

@ -1,12 +1,13 @@
# 安装页面
install.page.title = 安装向导
install.form.blogTitle = 博客标题
install.form.blogUrl = 博客地址
install.form.userEmail = 电子邮箱
install.form.userName = 用户名
install.form.userDisplayName = 显示昵称
install.form.userPwd = 登录密码
install.form.userRePwd = 确认密码
install.form.language = 系统语言:
install.form.blogTitle = 博客标题:
install.form.blogUrl = 博客地址:
install.form.userEmail = 电子邮箱:
install.form.userName = 用户名:
install.form.userDisplayName = 显示昵称:
install.form.userPwd = 登录密码:
install.form.userRePwd = 确认密码:
install.btn.submit = 安装Halo
install.success.title = 安装成功!
install.success.message = 你可以选择进入前台,或者登陆后台!

View File

@ -1,12 +1,13 @@
# install page
install.page.title = Installation Guide
install.form.blogTitle = Blog title
install.form.blogUrl = Blog address
install.form.userEmail = Email
install.form.userName = Username
install.form.userDisplayName = Display nickname
install.form.userPwd = Password
install.form.userRePwd = Confirm password
install.form.language = Language:
install.form.blogTitle = Blog title:
install.form.blogUrl = Blog address:
install.form.userEmail = Email:
install.form.userName = Username:
install.form.userDisplayName = Display nickname:
install.form.userPwd = Password:
install.form.userRePwd = Confirm password:
install.btn.submit = Install
install.success.title = Installation successful!
install.success.message = You can choose to go to the homepage or log in to the dashboard!

View File

@ -1,12 +1,13 @@
# 安装页面
install.page.title = 安装向导
install.form.blogTitle = 博客标题
install.form.blogUrl = 博客地址
install.form.userEmail = 电子邮箱
install.form.userName = 用户名
install.form.userDisplayName = 显示昵称
install.form.userPwd = 登录密码
install.form.userRePwd = 确认密码
install.form.language = 系统语言:
install.form.blogTitle = 博客标题:
install.form.blogUrl = 博客地址:
install.form.userEmail = 电子邮箱:
install.form.userName = 用户名:
install.form.userDisplayName = 显示昵称:
install.form.userPwd = 登录密码:
install.form.userRePwd = 确认密码:
install.btn.submit = 安装Halo
install.success.title = 安装成功!
install.success.message = 你可以选择进入前台,或者登陆后台!

View File

@ -65,6 +65,15 @@
<div class="tab-pane active" id="general">
<form method="post" class="form-horizontal" id="commonOptions">
<div class="box-body">
<div class="form-group">
<label for="blogLocale" class="col-lg-2 col-sm-4 control-label">系统语言:</label>
<div class="col-lg-4 col-sm-8">
<select class="form-control" id="blogLocale" name="blog_locale">
<option value="zh_CN" ${((options.blog_locale?default('zh_CN'))=='zh_CN')?string('selected','')}>简体中文</option>
<option value="en_US" ${((options.blog_locale?if_exists)=='en_US')?string('selected','')}>English</option>
</select>
</div>
</div>
<div class="form-group">
<label for="blogTitle" class="col-lg-2 col-sm-4 control-label">博客标题:</label>
<div class="col-lg-4 col-sm-8">

View File

@ -39,50 +39,59 @@
<div class="box box-solid animated" id="installFirst">
<div class="box-body" style="padding: 30px;">
<div class="form-group animated fadeInUp" style="animation-delay: 0.1s">
<label for="blogTitle" class="col-sm-4 control-label"><@spring.message code='install.form.blogTitle'/></label>
<label for="blogLocale" class="col-sm-4 control-label"><@spring.message code='install.form.language'/></label>
<div class="col-sm-8">
<select class="form-control" id="blogLocale" name="blogLocale">
<option value="zh_CN" ${((options.blog_locale?default('zh_CN'))=='zh_CN')?string('selected','')}>简体中文</option>
<option value="en_US" ${((options.blog_locale?if_exists)=='en_US')?string('selected','')}>English</option>
</select>
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.2s">
<label for="blogTitle" class="col-sm-4 control-label"><@spring.message code='install.form.blogTitle'/></label>
<div class="col-sm-8">
<input type="text" class="form-control" id="blogTitle" name="blogTitle">
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.1s">
<label for="blogUrl" class="col-sm-4 control-label"><@spring.message code='install.form.blogUrl'/></label>
<div class="form-group animated fadeInUp" style="animation-delay: 0.3s">
<label for="blogUrl" class="col-sm-4 control-label"><@spring.message code='install.form.blogUrl'/></label>
<div class="col-sm-8">
<input type="text" class="form-control" id="blogUrl" name="blogUrl">
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.2s">
<label for="userEmail" class="col-sm-4 control-label"><@spring.message code='install.form.userEmail'/></label>
<div class="form-group animated fadeInUp" style="animation-delay: 0.4s">
<label for="userEmail" class="col-sm-4 control-label"><@spring.message code='install.form.userEmail'/></label>
<div class="col-sm-8">
<input type="text" class="form-control" id="userEmail" name="userEmail">
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.3s">
<label for="userName" class="col-sm-4 control-label"><@spring.message code='install.form.userName'/></label>
<div class="form-group animated fadeInUp" style="animation-delay: 0.5s">
<label for="userName" class="col-sm-4 control-label"><@spring.message code='install.form.userName'/></label>
<div class="col-sm-8">
<input type="text" class="form-control" id="userName" name="userName">
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.4s">
<label for="userDisplayName" class="col-sm-4 control-label"><@spring.message code='install.form.userDisplayName'/></label>
<div class="form-group animated fadeInUp" style="animation-delay: 0.6s">
<label for="userDisplayName" class="col-sm-4 control-label"><@spring.message code='install.form.userDisplayName'/></label>
<div class="col-sm-8">
<input type="text" class="form-control" id="userDisplayName" name="userDisplayName">
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.5s">
<label for="userPwd" class="col-sm-4 control-label"><@spring.message code='install.form.userPwd'/></label>
<div class="form-group animated fadeInUp" style="animation-delay: 0.7s">
<label for="userPwd" class="col-sm-4 control-label"><@spring.message code='install.form.userPwd'/></label>
<div class="col-sm-8">
<input type="password" class="form-control" id="userPwd" name="userPwd">
</div>
</div>
<div class="form-group animated fadeInUp" style="animation-delay: 0.6s">
<label for="userRePwd" class="col-sm-4 control-label"><@spring.message code='install.form.userRePwd'/></label>
<div class="form-group animated fadeInUp" style="animation-delay: 0.8s">
<label for="userRePwd" class="col-sm-4 control-label"><@spring.message code='install.form.userRePwd'/></label>
<div class="col-sm-8">
<input type="password" class="form-control" id="userRePwd" name="userRePwd">
</div>
</div>
</div>
<div class="box-footer" style="padding-right: 30px;">
<button type="submit" class="btn btn-primary btn-sm btn-flat pull-right animated fadeInUp" style="animation-delay: 0.9s"><@spring.message code='install.btn.submit'/></button>
<button type="submit" class="btn btn-primary btn-sm btn-flat pull-right animated fadeInUp" style="animation-delay: 1s"><@spring.message code='install.btn.submit'/></button>
</div>
</div>
<div class="box box-solid animated fadeInUp" style="display: none" id="installSuccess">