-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in SHIBUI-635 (pull request #116)
[SHIBUI-635] Approved-by: Dmitriy Kopylenko <dkopylenko@unicon.net> Approved-by: Shibui Jenkins <shibui.jenkins@gmail.com> Approved-by: Ryan Mathis <rmathis@unicon.net>
- Loading branch information
Showing
23 changed files
with
251 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...u/internet2/tier/shibboleth/admin/ui/configuration/InternationalizationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.configuration; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.i18n.MappedResourceBundleMessageSource; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.LocaleResolver; | ||
import org.springframework.web.servlet.i18n.SessionLocaleResolver; | ||
|
||
@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(); | ||
return sessionLocaleResolver; | ||
} | ||
|
||
@Bean | ||
public MappedResourceBundleMessageSource messageSource() { | ||
MappedResourceBundleMessageSource source = new MappedResourceBundleMessageSource(); | ||
source.setBasenames("i18n/messages"); | ||
source.setUseCodeAsDefaultMessage(true); | ||
return source; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...internet2/tier/shibboleth/admin/ui/controller/InternationalizationMessagesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.controller; | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.i18n.MappedResourceBundleMessageSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* @author Bill Smith (wsmith@unicon.net) | ||
*/ | ||
@Controller | ||
@RequestMapping(value = "/api/messages") | ||
public class InternationalizationMessagesController { | ||
@Autowired | ||
MappedResourceBundleMessageSource messageSource; | ||
|
||
@GetMapping | ||
public ResponseEntity<?> getAll(Locale locale) { | ||
return ResponseEntity.ok(messageSource.getMessagesMap(locale)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...n/java/edu/internet2/tier/shibboleth/admin/ui/i18n/MappedResourceBundleMessageSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.i18n; | ||
|
||
import org.springframework.context.support.ResourceBundleMessageSource; | ||
|
||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* @author Bill Smith (wsmith@unicon.net) | ||
*/ | ||
public class MappedResourceBundleMessageSource extends ResourceBundleMessageSource { | ||
public Map<String, String> getMessagesMap(Locale locale) { | ||
ResourceBundle resourceBundle = this.doGetBundle("i18n/messages", locale); | ||
Map<String, String> messagesMap = new HashMap<>(); | ||
Enumeration bundleKeys = resourceBundle.getKeys(); | ||
|
||
while (bundleKeys.hasMoreElements()) { | ||
String key = (String)bundleKeys.nextElement(); | ||
String value = resourceBundle.getString(key); | ||
messagesMap.put(key, value); | ||
} | ||
|
||
return messagesMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Fill this file with key/value pairs, as follows: | ||
# | ||
# some.test.message=This is a test message. | ||
# | ||
# Then, create a copy using the name of the language code: | ||
# | ||
# messages_<code>.properties | ||
# | ||
# Do this for each language we want to support. | ||
# Ideally, all messages should exist for each language. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a.sample.message=This is a sample message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a.sample.message=Le francais est tres difficile. |
113 changes: 113 additions & 0 deletions
113
...t2/tier/shibboleth/admin/ui/controller/InternationalizationMessagesControllerTests.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.controller | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.configuration.CoreShibUiConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.InternationalizationConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.SearchConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.TestConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.i18n.MappedResourceBundleMessageSource | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.domain.EntityScan | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories | ||
import org.springframework.test.context.ContextConfiguration | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders | ||
import org.springframework.web.servlet.LocaleResolver | ||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor | ||
import spock.lang.Specification | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | ||
|
||
/** | ||
* @author Bill Smith (wsmith@unicon.net) | ||
*/ | ||
@DataJpaTest | ||
@ContextConfiguration(classes=[CoreShibUiConfiguration, SearchConfiguration, TestConfiguration, InternationalizationConfiguration]) | ||
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"]) | ||
@EntityScan("edu.internet2.tier.shibboleth.admin.ui") | ||
class InternationalizationMessagesControllerTests extends Specification { | ||
@Autowired | ||
MappedResourceBundleMessageSource messageSource | ||
|
||
@Autowired | ||
LocaleChangeInterceptor localeChangeInterceptor | ||
|
||
@Autowired | ||
LocaleResolver localResolver | ||
|
||
def controller | ||
def mockMvc | ||
|
||
def setup() { | ||
controller = new InternationalizationMessagesController( | ||
messageSource: messageSource | ||
) | ||
|
||
mockMvc = MockMvcBuilders.standaloneSetup(controller) | ||
.setLocaleResolver(localResolver) | ||
.addInterceptors(localeChangeInterceptor) | ||
.build() | ||
} | ||
|
||
def messagesUrl = "/api/messages" | ||
|
||
def expectedEnglishResult = | ||
'{' + | ||
' "some.test.message": "This is the English test message."' + | ||
'}' | ||
|
||
def expectedFrenchResult = | ||
'{' + | ||
' "some.test.message": "Je ne sais pas Francais."' + | ||
'}' | ||
|
||
def "GET messages with no header or \"lang\" param defaults to returning english messages"() { | ||
when: | ||
def result = mockMvc.perform( | ||
get(messagesUrl)) | ||
|
||
then: | ||
result.andExpect(content().json(expectedEnglishResult)) | ||
} | ||
|
||
def "GET messages with Accept-Language returns messages in that language"() { | ||
when: | ||
def result = mockMvc.perform( | ||
get(messagesUrl) | ||
.header("Accept-Language", "fr")) | ||
|
||
then: | ||
result.andExpect(content().json(expectedFrenchResult)) | ||
} | ||
|
||
def "GET messages with \"lang\" request param returns messages in that language"() { | ||
when: | ||
def result = mockMvc.perform( | ||
get(messagesUrl) | ||
.param("lang", "fr")) | ||
|
||
then: | ||
result.andExpect(content().json(expectedFrenchResult)) | ||
} | ||
|
||
def "GET messages with both Accept-Language header and \"lang\" request param returns messages in the language specified by the \"lang\" parameter"() { | ||
when: | ||
def result = mockMvc.perform( | ||
get(messagesUrl) | ||
.header("Accept-Language", "en") | ||
.param("lang", "fr")) | ||
|
||
then: | ||
result.andExpect(content().json(expectedFrenchResult)) | ||
} | ||
|
||
def "GET messages with an unsupported Accept-Language returns the default language"() { | ||
when: | ||
def result = mockMvc.perform( | ||
get(messagesUrl) | ||
.header("Accept-Language", "es")) | ||
|
||
then: | ||
result.andExpect(content().json(expectedEnglishResult)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.