Grails: Change contents of message.properties (i18n) in production - grails

Is is possible to modify text in message.properties when in production without a redeploy/restart.

It's quite possible, but you need to replace the default messageSource bean with a ReloadableResourceBundleMessageSource. You can do this by configuring a new messageSource bean definition in your grails-app/conf/spring/resources.groovy as follows:
beans = {
messageSource(org.springframework.context.support.ReloadableResourceBundleMessageSource) {
basenames = ["classpath:grails-app/i18n/myApp", "file:grails-app/i18n/messages", "WEB-INF/grails-app/i18n/messages"]
}
}
The above will work in both development and production. You may also want to research other options available to you using the ReloadableResourceBundleMessageSource.

This is not possible if you use the default messageSource bean. If you want to do this, one option is to store the messages in the database rather than properties files. Here are a couple of plugins that support this (I've never used either of them myself)
http://grails.org/plugin/localizations
https://github.com/goeh/grails-i18n-db
Alternatively, replace the default messageSource bean with one that supports reloading, e.g. ReloadableResourceBundleMessageSource.

Related

Grails dynamic baseurl for multi-url webapp

Any one have an idea how to use a dynamic baseurl??
f.e: I have file.war and I want to deploy the same war for www.webapp1.com & www.webapp2.com
I tried to use alias for my server.xml host, and I tried to use a separated host in server.xml.. but every time I try webapp2.com url I get the webapp1.com.
Grails registers a bean named grailsLinkGenerator. I believe you can accomplish what you're trying to do by overriding this bean with your own custom logic. Your bean must implement the LinkGenerator interface. You might find it easiest to extend DefaultLinkGenerator or CachingLinkGenerator

Using resources.groovy to define services

I'm using the resources.groovy to declare a service e.g.
aService(com.foo.OrganizationService)
so that I can tie aService to my controllers instead of using organizationService which could change in the future.
I've noticed that the OrganizationService doesn't get treated special like other services "not" declared in the resources.groovy. For example it doesn't get injected with grailsApplication, and likely a hibernateSession etc and other things I've not hit yet....
Now, I know I can manually wire in stuff to my service but I'd rather not have to maintain that...
Is there a special way to declare a service in the resources.groovy so that gets treated like another service that grails loads up?
TIA
The short answer to your question is "no".
Under the covers, Grails services are driven by some intelligent code that is referencing a specific location and expecting certain properties.
Viewing the source code (especially around the ServicesGrailsPlugin.groovy) is a good way to see the "magic" in how these are wired together.
Is there a reason you wouldn't want to use a bonafide Grails service to solve your problem? If you are expecting things like a grailsApplication, it seems like that use is pretty specific to Grails and would be a good candidate for porting over to a "true" Grails service.
Good luck!
So I've come full circle on this. This is a timing problem. Where services haven't been grails initialized yet.
Basically when you use the resources.groovy to do service wiring you run the risk of using a Service that might initialize itself e.g. afterPropertiesSet method or static initializers that use grails services (log, hibernate session, ..) that haven't been injected yet.
So... What I've turned to instead is to create my own BeanBuilder in a BootStrap.groovy file.
BeanBuilder builder = new BeanBuilder(grailsApplication.parentContext)
def bb = builder.beans {
LoginListener(com.foo.LoginListener) {
springSecurityService = ref("springSecurityService")
userService = ref("userService")
}
}
bb.registerBeans(grailsApplication.mainContext)

Good way to introduce constant string to i18n internationalization file in grails

In grails, is there a way in the i18n file to reference a constant value. I'm trying to do something like this:
constant.sitename=Fancy SiteName
intro.headline.label=Welcome to {constant.sitename}
home.headline=You're at {constant.sitename}
The reason is I don't want to change the sitename in every single string if we decide to change the name, I only want to do it once. Is there a way to accomplish this requirement?
I realize I could also set a constant in the Config.groovy, but then that would require passing in the param on every single message that required it, which I'd rather not have to do this as it would make developers lives worse.
You might override Grails' standard messageSource bean by a custom implementation in resources.groovy. By default the Grails i18n plugin uses PluginAwareResourceBundleMessageSource for this.
So, subclass this and override the necessary code to add property replacement. Don't know by heart what exact methods these are, but that should be easy to figure out with a debugger.

Distinguishing between Grails domain-class fields and getBlah() methods via GrailsDomainClassProperty

I'm writing a Groovy script (as part of a Grails plugin) and I want to get a list of properties for a GrailsDomainClass that a user of my plugin might define. I can do this using domainClass.properties (where domainClass is a GrailsDomainClass).
However, suppose a user has the grails domain class:
class Example {
String name
static constraints = {
}
def getSomeNonExistingProperty(){
return "Not-a-real-property"
}
}
In this case, domainClass.properties returns a list with both name and someNoneExistingProperty
I understand that this is because of Grails is generating a read-only property on-the-fly for use where someone has a getBlah() method. That's great, but in my script I want to perform some actions with the "real" properties only (or at least non read-only properties).
That is, I would like some way of distinguishing or identifying someNonExistingProperty as a read-only property, or, alternatively, as a property generated by Grails and not entered explicitly as a field in the domainClass by the user of my plugin.
I've looked at the GrailsDomainClassProperty Class and it has a range of methods providing information about the property. However, none of them appear to tell me whether a property is read-only or not, or to allow me to distinguish between a field defined in the domainClass and a field created on-the-fly by Grails as a result of a "getSomeNonExistingProperty()" method.
Am I missing something obvious here? Is there a way of getting a list of just the explicitly user-defined fields (eg name, in the above example)?
I believe transient properties are what you are trying to exclude
I've run into this problem a few times, and instead of trying to work around it I typically just end up renaming my getX() method. It's probably the easiest option.
Edit:
Alternatively, I wonder if you could use reflection to see which methods are defined on the class, and while iterating over your properties see if the property has an explicit getter defined, and omit it. I'm not very familiar with reflection when it comes to Groovy and Grails, especially with the dynamic methods, but it's a possible route of investigation.

Grails URLMappings

I have a plugin that relies on a custom url mapping. When i install the plugin i need to copy the content of the UrlMappings.groovy of the plugin and merge it with the one in the application where the plugin is installed
I would like however to register these url mappings directly into the grails framework without copying the content into the application itself. I don't want the user to change these mappings as they are core to how the plugins works.
Anyone can give me hints where to venture to achieve that.
Thank you
-ken
Create another file in grails-app/config/ with a name ending in UrlMappings.groovy and put the customized mappings in there, for example myUrlMappings.groovy
Seems like i need to interface with UrlMappingsHolderFactoryBean directly to be able to do that. I was hoping that there might be an easier way to do that. The code below is taken from the UrlMappingPlugin itself, the only source that i found to help me solve my problem.
if (application.isUrlMappingsClass(event.source)) {
application.addArtefact(UrlMappingsArtefactHandler.TYPE, event.source)
BeanBuilder beans = beans {
grailsUrlMappingsHolderBean(UrlMappingsHolderFactoryBean) {
grailsApplication = application
}
}
ApplicationContext appCtx = event.ctx
beans.registerBeans(appCtx)
HotSwappableTargetSource ts = appCtx.getBean("urlMappingsTargetSource")
ts.swap appCtx.getBean("grailsUrlMappingsHolderBean")
}
Personally I use Java approach and inject mappings form plugin (I have only one plugin for that).
But generally my approach is following:
1.App
class UrlMappings {
static mappings = DimaURLMappings.getMappings()
}
2.Plugin (called "Dima")
class DimaURLMappings {
static def getMappings(){
return {
//Mappings here
}
}
}
Yes I need to add it manually, but on the other hand it's quite obvious where application gets that mappings, and there are no magic.
I use it in such way because I have few applications that use my plugin. If you have few plugins with mappings you will just need to merge them I think it is not hard. you could also return closure from plugin. But don't forget to change "delegate" in application.

Resources