Search code examples
grailsconfigurationlocalizationlocale

Stripe Grails configuration per locale


Is there a way in Grails to stripe the config by locale?

eg like this:

locale {
    fr-FR {
        grails.serverURL = "http://www.mysite.fr"
    }
    en-GB {
        grails.serverURL = "http://www.mysite.co.uk"
    }
}

We are starting to internationalise our site for multiple country/language and some configuration will have to be country specific.

Thanks


Solution

  • If the configuration values are strings (as above) you could put them in the message*.properties files instead of Config.groovy. You can then use either the messageSource Spring bean or the message tag to retrieve the values for the current Locale.

    Update

    Further to the comment below about mixing config information in resource bundles, an alternative is to do it programatically - remember Config.groovy is a .groovy file, so you can mix code with your configuration data. Something like the following should work:

    locale {    
      def serverUrls = [Locale.FRANCE: "http://www.mysite.fr", 
                Locale.UK: "http://www.mysite.co.uk"]
    
      def currentLocale = org.springframework.context.i18n.LocaleContextHolder.locale
      def serverlUrl = serverUrls[currentLocale]
      assert serverUrl, "no serverUrl found for Locale $currentLocale"    
    
      grails.serverURL = serverUrl
    }
    

    If you have several config parameters that you want to vary by Locale, something like the following would be neater

    def currentLocale = org.springframework.context.i18n.LocaleContextHolder.locale
    
    switch (currentLocale) {
      case Locale.FRANCE:
        // config params for France
      break;
      case Locale.UK:
        // config params for UK
      break;        
    }