Skip to content

Commit

Permalink
Support multiple messageSource beans (#13640)
Browse files Browse the repository at this point in the history
* Support multiple messageSource beans and default to grails or spring instance
  • Loading branch information
jamesfredley authored Sep 12, 2024
1 parent 9ea9c60 commit 438d2e0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 grails.util

import groovy.transform.CompileStatic
import org.springframework.context.MessageSource


/**
* A simple class that selects a single {@link org.springframework.context.MessageSource MessageSource}
* when two or more are present in the ApplicationContext.
* It defaults to the Grails or Spring {@link org.springframework.context.MessageSource MessageSource}, if present.
*
* @author James Fredley
* @since 7.0
*/

@CompileStatic
class GrailsMessageSourceUtils {
static MessageSource findPreferredMessageSource(List<MessageSource> messageSources){
if(!messageSources) {
return null
}

if(messageSources.size() == 1) {
return messageSources.get(0)
}

MessageSource firstGrailsSpring = messageSources.find {messageSource ->
String className = messageSource.class.name
// use the first Grails or Spring MessageSource
className.startsWith("org.grails") || className.startsWith("grails") || className.startsWith("org.springframework")
}

// return the first Grails or Spring MessageSource
if(firstGrailsSpring) {
return firstGrailsSpring
}

// return the first MessageSource from the list
return messageSources.get(0)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.grails.plugins.domain.support

import grails.core.GrailsApplication
import grails.util.GrailsMessageSourceUtils
import org.grails.datastore.gorm.validation.constraints.eval.ConstraintsEvaluator
import org.grails.datastore.gorm.validation.constraints.eval.DefaultConstraintEvaluator
import org.grails.datastore.gorm.validation.constraints.registry.ConstraintRegistry
Expand All @@ -14,10 +15,17 @@ import org.springframework.context.MessageSource

class DefaultConstraintEvaluatorFactoryBean implements FactoryBean<ConstraintsEvaluator> {

@Autowired
@Qualifier("pluginAwareResourceBundleMessageSource")
MessageSource messageSource

@Autowired
setMessageSource(List<MessageSource> messageSources) {
setMessageSource(GrailsMessageSourceUtils.findPreferredMessageSource(messageSources))
}

void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource
}

@Autowired
@Qualifier('grailsDomainClassMappingContext')
MappingContext grailsDomainClassMappingContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package grails.rest.render.errors

import grails.rest.render.ContainerRenderer
import grails.util.Environment
import grails.util.GrailsMessageSourceUtils
import grails.util.GrailsNameUtils
import grails.util.GrailsWebUtil
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import grails.web.mapping.LinkGenerator
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.MessageSource
import org.springframework.validation.Errors
import org.springframework.validation.ObjectError
Expand All @@ -47,10 +47,17 @@ abstract class AbstractVndErrorRenderer implements ContainerRenderer<Errors, Ob
boolean absoluteLinks = true
boolean prettyPrint = Environment.isDevelopmentMode()

@Autowired
@Qualifier("pluginAwareResourceBundleMessageSource")
MessageSource messageSource

@Autowired
setMessageSource(List<MessageSource> messageSources) {
setMessageSource(GrailsMessageSourceUtils.findPreferredMessageSource(messageSources))
}

void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource
}

@Autowired
LinkGenerator linkGenerator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import grails.rest.render.AbstractIncludeExcludeRenderer
import grails.rest.render.RenderContext
import grails.rest.render.RendererRegistry
import grails.util.Environment
import grails.util.GrailsMessageSourceUtils
import grails.util.GrailsWebUtil
import grails.web.mapping.LinkGenerator
import grails.web.mime.MimeType
Expand Down Expand Up @@ -61,10 +62,17 @@ abstract class AbstractLinkingRenderer<T> extends AbstractIncludeExcludeRenderer
public static final String TEMPLATED_ATTRIBUTE = 'templated'
public static final String DEPRECATED_ATTRIBUTE = 'deprecated'

@Autowired
@Qualifier("pluginAwareResourceBundleMessageSource")
MessageSource messageSource

@Autowired
setMessageSource(List<MessageSource> messageSources) {
setMessageSource(GrailsMessageSourceUtils.findPreferredMessageSource(messageSources))
}

void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource
}

@Autowired
LinkGenerator linkGenerator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import grails.databinding.*
import grails.databinding.converters.FormattedValueConverter
import grails.databinding.converters.ValueConverter
import grails.databinding.events.DataBindingListener
import grails.util.Environment
import grails.util.GrailsClassUtils
import grails.util.GrailsMessageSourceUtils
import grails.util.GrailsMetaClassUtils
import grails.util.GrailsNameUtils
import grails.validation.DeferredBindingActions
Expand All @@ -46,22 +46,18 @@ import org.grails.datastore.mapping.model.types.OneToMany
import org.grails.datastore.mapping.model.types.OneToOne
import org.grails.datastore.mapping.model.types.Simple
import org.grails.web.databinding.DataBindingEventMulticastListener
import org.grails.web.databinding.DefaultASTDatabindingHelper
import org.grails.web.databinding.GrailsWebDataBindingListener
import org.grails.web.databinding.SpringConversionServiceAdapter
import org.grails.web.databinding.converters.ByteArrayMultipartFileValueConverter
import org.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.MessageSource
import org.springframework.validation.BeanPropertyBindingResult
import org.springframework.validation.BindingResult
import org.springframework.validation.FieldError
import org.springframework.validation.ObjectError

import java.lang.annotation.Annotation
import java.lang.reflect.Modifier
import java.util.concurrent.ConcurrentHashMap

import static grails.web.databinding.DataBindingUtils.*

Expand Down Expand Up @@ -646,7 +642,10 @@ class GrailsWebDataBinder extends SimpleDataBinder {
}

@Autowired
@Qualifier("pluginAwareResourceBundleMessageSource")
setMessageSource(List<MessageSource> messageSources) {
setMessageSource(GrailsMessageSourceUtils.findPreferredMessageSource(messageSources))
}

void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource
}
Expand Down

0 comments on commit 438d2e0

Please sign in to comment.