Now and then through my work I get asked to create a site with multi-lingual support. In the past I have usually just created a custom tag or UDF. This time, now that I am on A href="http://model-glue.com/">Model Glue: Unity I wanted something that was a little more integrated so I decided to use Ray Camden modified ResourceBundle.cfc. This cfc was actually created by Paul Hastings and you can find more information here.There are two reasons I am posting this mini guide, first to share and the second is that knowing me I am probably doing something inefficient and by letting the community see it hopefully someone will point out any errors or an easier way to do things :o) I've zipped up Pauls/Rays ResourceBundle.cfc that comes with BlogCFC and you can download it by clicking here. Lets's begin:
Thats pretty much covered the basics of setting this up. You'll notice in my last code snippet I use the UK date format style, this won't do if you are creating a multilingual site but there is a utilities package that goes with the resource bundle for this and I hope to cover that in another post. At the moment I just wait feedback/comment with this approach before I continue.
- Download the file and put it in your projects model directory, so the url to this CFC would be http://yourlocalproject/model/resourcebundle.cfc.
- Also in your model directory create new folder called "locale_data" and in there add a file called en_US.properties.
- Open up resourceBundle.cfc and change the method(function) "loadResourceBundle" to "init". The reason for this is that we are going to get ColdSpring to inject our required variables into the resource bundle.
- Then around line 53 look for the following bit of code
and change it to
- Next open up your ColdSpring.xml file and add the following:
/model/locale_data/en_US en_US What we are doing here is initiated the resourcebundle.cfc and passing two arguments to the init method. These arguments consist of the:
rbFile: This must be the path + filename UP to but NOT including the locale. We auto-add .properties to the end. This location is the file we created in point 2.
rbLocale: The locale.
- Then we are going to inject this into our controller and to do this add the following code snippet to your ColdSpring.xml File:
and in your Model Glue controller add
- At this point a lot of the hard work is done. Point 1-3 were just about setting up our environment and 4-6 were about some modification to get everything to play together nicely.
- We'll carry on with the controller. Because we have injected the resourcebundle.cfc in the Controller we can access it via the variable "variables.resourcebundle" and assign it to the viewState with the following code in the onRequestStart function:
By doing this we have the bundle in the application scope and ready. For our view files we'll just need to accees the viewState variable but before we do that.....
- Lets populate the en_US.properties file with the following before we go any further:
name=Namedob=Date Of birthelevator=elevatorcigarette=cigarettesidewalk=sidewalkIf you haven't used blogCFC or the resoucebundle then you probably should know that the for each language/region you need to create a new .properties file with the relevant prefix(from the CFMX's locales) and values. So for France the prefix would be fr_FR and UK(English) file would be called en_GB.properties, and the content would be something like:
name=Namedob=Date Of birthelevator=liftcigarette=fagsidewalk=pavement
- All we need to do now is get the variable we set earlier in OnRequestStart at point xxxxxxxxxxx. Anyone familiar with MG should know this already
- Now that the resourcebundle is referenced to the "rb" variable we use the method "getResource" to access our .properties file and retrieve the translations. So in your view file the final code to display the users name and dob will then look something like
#rg.getResource('dob')#: #dateFormat(dob, 'dd-mm-yyyy')#