Partial support for locale in spanish.

This commit is contained in:
Paulo Gustavo Veiga
2012-06-30 02:26:21 -03:00
parent 36c34b6962
commit dd74a7a63d
46 changed files with 394 additions and 270 deletions

View File

@@ -0,0 +1,60 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.filter;
import com.wisemapping.model.User;
import com.wisemapping.security.Utils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;
public class UserLocaleInterceptor extends HandlerInterceptorAdapter {
public static final String USER_LOCALE = "user.locale";
public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, Object object) throws Exception {
final HttpSession session = request.getSession(false);
User user = Utils.getUser(false);
if (user != null && session != null) {
String userLocale = user.getLocale();
final String sessionLocale = (String) session.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
if (userLocale != null && !userLocale.equals(sessionLocale)) {
// LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
// if (localeResolver == null) {
// throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
// }
// LocaleEditor localeEditor = new LocaleEditor();
// localeEditor.setAsText(userLocale);
// localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, userLocale);
}
}
return true;
}
}

View File

@@ -18,6 +18,8 @@
package com.wisemapping.model;
import org.jetbrains.annotations.NotNull;
import java.io.Serializable;
import java.util.*;
@@ -33,6 +35,8 @@ public class User
private String username;
private Set<String> tags = new HashSet<String>();
private boolean allowSendEmail = false;
private String locale;
public User() {
}
@@ -119,6 +123,7 @@ public class User
}
public int hashCode() {
int result;
result = (firstname != null ? firstname.hashCode() : 0);
@@ -136,4 +141,11 @@ public class User
this.username = username;
}
public String getLocale() {
return locale;
}
public void setLocale(@NotNull String locale) {
this.locale = locale;
}
}

View File

@@ -80,6 +80,21 @@ public class AccountController extends BaseController {
userService.updateUser(user);
}
@RequestMapping(method = RequestMethod.PUT, value = "account/locale", consumes = {"text/plain"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changeLanguage(@RequestBody String language) {
if (language == null) {
throw new IllegalArgumentException("language can not be null");
} if (!language.equals("en") && !language.equals("es") ){
throw new IllegalArgumentException("language not supported yet");
}
final User user = Utils.getUser();
user.setLocale(language);
userService.updateUser(user);
}
@RequestMapping(method = RequestMethod.POST, value = "logger/editor", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changePassword(@RequestBody RestLogItem item) {