getting a list of all i18n properties used in a Grails application

You might know this situation: in a project you start by hacking code that uses i18n properties instead of fixed strings in order to support multiple languages. The normal process in Grails is to use the g:message tag in controllers or gsp templates. Side by side you append the new introduced i18n property with some value in your messsages.properties file.

When support for a new language is requested, all you have to do is translating messages.properties. So far so good – this make i18n really easy.

But: when the project evolves, there’s a good chance that some of your i18n properties in messages.properties gets orphaned. Assume you remove a block of code from a gsp. It happens often that the i18n properties used in this block are not removed from messages*.properties because at some point you are not sure if it is referenced elsewhere. So what would be really useful here would be a list of all referenced i18n properties from your *.groovy/*.gsp files.

Doing so is pretty easy, just add a new gant script to your Grails application’s script folder, let’s name it i18nList.groovy. This script basically contains:

includeTargets << grailsScript("Init")
 
target(main: "create a list of all i18n properties used in groovy code and gsp templates") {
 
    def properties = []
 
    new File(".").eachFileRecurse {
        if (it.file) {
            switch (it) {
                case ~/.*\.groovy/:
                    def matcher = it.text =~ /code:\s*["'](.*?)["']/
                    matcher.each { properties << it[1] }
                    break
                case ~/.*\.gsp/:
                    def matcher = it.text =~ /code=["'](.*?)["']/
                    matcher.each { properties << it[1] }
                    break
            }
        }
    }
    println properties.sort().unique().join("\n")
 
}
 
setDefaultTarget(main)

(sorry the color coding seems to fail for some Groovy regexes) The script recursivly iterates over all *.groovy and *.gsp files in your project and extract the part after the ‘code’ attribute of the message tag using a regex. The regex result are collected into an array. This array is sorted, unique’d and printed to the console. That’s it.

One word of caution: this gant script ‘works for me’. So depending on your code, you might notice that the used regex are not sufficient or even fail. Feel free to modify them for your needs, even better send back your modifications.

5 thoughts on “getting a list of all i18n properties used in a Grails application

  1. Sebastian Kurt

    @Stefan Armbruster
    Solution found, as our app uses snippets like ${message(code: foo.create’, default: ‘Create New’)} in GSP-Files we have to add the matcher for this to GSP case.

    switch (it) {
    case ~/.*\.groovy/:
    def matcher = it.text =~ /code:\s*[“‘](.*?)[“‘].*default:\s*[“‘](.*?)[“‘]/
    matcher.each { properties << it[1]+"="+it[2] }
    break
    case ~/.*\.gsp/:
    def matcher = it.text =~ /code=["'](.*?)["'].*default=\s*["'](.*?)["']/
    matcher.each { properties << it[1]+"="+it[2] }
    def matcher2 = it.text =~ /code:\s*["'](.*?)["'].*default:\s*["'](.*?)["']/
    matcher2.each { properties << it[1]+"="+it[2] }
    break
    }

  2. Paul

    Hi, i updated your scripts to match more possibilities:

    includeTargets < println “${k}=${v}” }

    }

    setDefaultTarget(main)

  3. ataaaak

    Beware. Don’t use this script with the first line

    includeTargets << grailsScript("Init")

    This will reinitialize your application and rewrite changes you have already made

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>