Skip to content

Commit

Permalink
[SHIBUI-894]
Browse files Browse the repository at this point in the history
Another fix attempt. This one should do it. "Should."
  • Loading branch information
Bill Smith committed Sep 27, 2018
1 parent b2c8e95 commit 19f2d0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class InternationalizationConfiguration {
@Bean
public LocaleResolver localeResolver() {
// TODO if we want to control the order, we can implement our own locale resolver instead of using the SessionLocaleResolver.
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();

// NOTE: If we set a default here, Locale.getDefault's behavior will be consistent, but then Accept-Language
// is not honored (only ?lang=). If we do not set a default, the default is determined at runtime by the JVM.
// This may break unit tests if the system does not determine the default to be English.
// sessionLocaleResolver.setDefaultLocale(new Locale("en"));

return sessionLocaleResolver;
}

@Bean
public MappedResourceBundleMessageSource messageSource() {
MappedResourceBundleMessageSource source = new MappedResourceBundleMessageSource();
source.setBasenames("i18n/messages");
source.setUseCodeAsDefaultMessage(true);
source.setFallbackToSystemLocale(false);
source.setUseCodeAsDefaultMessage(false); //TODO Why was this true?
source.setFallbackToSystemLocale(false); // allows us to return messages.properties instead of
// messages_en.properties for unsupported languages.
return source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,23 @@ class InternationalizationMessagesControllerTests extends Specification {
result.andExpect(content().json(expectedFrenchResult))
}

def "GET messages with an unsupported Accept-Language returns the default language"() {
def "GET messages with an unsupported Accept-Language returns the default (unspecified) language"() {
when:
def result = mockMvc.perform(
get(messagesUrl)
.header("Accept-Language", "ja_JP"))
.header("Accept-Language", "ja"))
then:
result.andExpect(content().json(expectedEnglishResult))
result.andExpect(content().json(expectedDefaultResult))
}
def "GET messages with a made-up Accept-Language returns the default (unspecified) language"() {
when:
def result = mockMvc.perform(
get(messagesUrl)
.header("Accept-Language", "foo"))
then:
result.andExpect(content().json(expectedDefaultResult))
}
}

0 comments on commit 19f2d0a

Please sign in to comment.