Provide Server-side service to support UI localization #265
- Now also enabled for adoc
This commit is contained in:
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This file is part of WebGoat, an Open Web Application Security Project utility. For details,
|
||||
* please see http://www.owasp.org/
|
||||
* <p>
|
||||
* Copyright (c) 2002 - 2017 Bruce Mayhew
|
||||
* <p>
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
* <p>
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
||||
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU General Public License along with this program; if
|
||||
* not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
||||
* 02111-1307, USA.
|
||||
* <p>
|
||||
* Getting Source ==============
|
||||
* <p>
|
||||
* Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software
|
||||
* projects.
|
||||
* <p>
|
||||
*/
|
||||
|
||||
package org.owasp.webgoat.i18n;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Wrapper around the LocaleResolver from Spring so we do not need to bother with passing the HttpRequest object
|
||||
* when asking for a Locale.
|
||||
*
|
||||
* @author nbaars
|
||||
* @date 2/7/17
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class Language {
|
||||
|
||||
private final LocaleResolver localeResolver;
|
||||
|
||||
public Locale getLocale() {
|
||||
return localeResolver.resolveLocale(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest());
|
||||
}
|
||||
|
||||
}
|
@ -26,11 +26,7 @@ package org.owasp.webgoat.i18n;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@ -42,7 +38,7 @@ import java.util.Properties;
|
||||
@AllArgsConstructor
|
||||
public class Messages extends ReloadableResourceBundleMessageSource {
|
||||
|
||||
private final LocaleResolver localeResolver;
|
||||
private final Language language;
|
||||
|
||||
/**
|
||||
* Gets all messages for presented Locale.
|
||||
@ -50,21 +46,14 @@ public class Messages extends ReloadableResourceBundleMessageSource {
|
||||
* @return all messages
|
||||
*/
|
||||
public Properties getMessages() {
|
||||
return getMergedProperties(resolveLocale()).getProperties();
|
||||
return getMergedProperties(language.getLocale()).getProperties();
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object... args) {
|
||||
return getMessage(code, args, resolveLocale());
|
||||
return getMessage(code, args, language.getLocale());
|
||||
}
|
||||
|
||||
public String getMessage(String code, String defaultValue, Object... args) {
|
||||
return super.getMessage(code, args, defaultValue, resolveLocale());
|
||||
return super.getMessage(code, args, defaultValue, language.getLocale());
|
||||
}
|
||||
|
||||
protected Locale resolveLocale() {
|
||||
return localeResolver.resolveLocale(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -43,23 +43,23 @@ import java.util.Properties;
|
||||
*/
|
||||
public class PluginMessages extends ReloadableResourceBundleMessageSource {
|
||||
|
||||
private Messages messages;
|
||||
private Language language;
|
||||
|
||||
public PluginMessages(Messages messages) {
|
||||
this.messages = messages;
|
||||
public PluginMessages(Messages messages, Language language) {
|
||||
this.language = language;
|
||||
this.setParentMessageSource(messages);
|
||||
}
|
||||
|
||||
public Properties getMessages() {
|
||||
return getMergedProperties(messages.resolveLocale()).getProperties();
|
||||
return getMergedProperties(language.getLocale()).getProperties();
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object... args) {
|
||||
return getMessage(code, args, messages.resolveLocale());
|
||||
return getMessage(code, args, language.getLocale());
|
||||
}
|
||||
|
||||
public String getMessage(String code, String defaultValue, Object... args) {
|
||||
return super.getMessage(code, args, defaultValue, messages.resolveLocale());
|
||||
return super.getMessage(code, args, defaultValue, language.getLocale());
|
||||
}
|
||||
|
||||
public void addPluginMessageBundles(final File i18nPluginDirectory) {
|
||||
|
Reference in New Issue
Block a user