Multi-lingual site with Model-Glue and Coldspring
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:- 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
1 <cfset thisRBfile=arguments.rbFile&".properties">and change it to1 <cfset thisRBfile = expandPath(arguments.rbFile&".properties")>
- Next open up your ColdSpring.xml file and add the following:
1 <bean id="resourcebundle" class="model.resourcebundle">What we are doing here is initiated the resourcebundle.cfc and passing two arguments to the init method. These arguments consist of the:
2 <constructor-arg name="rbFile"><value>/model/locale_data/en_US</value></constructor-arg>
3 <constructor-arg name="rbLocale"><value>en_US</value></constructor-arg>
4 </bean>
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:
1 <bean id="controller" class="controller.controller">and in your Model Glue controller add
2 <property name="resourcebundle">
3 <ref bean="resourcebundle" />
4 </property>
5 </bean>1 <cffunction name="setResourcebundle" returntype="void" access="public" output="false">
2 <cfargument name="Resourcebundle" required="true" type="any" />
3 <cfset variables.Resourcebundle = arguments.Resourcebundle />
4 </cffunction>
5
6 <cffunction name="getResourcebundle" returntype="any" access="public" output="false">
7 <cfreturn variables.Resourcebundle />
8 </cffunction> - 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:
1 <cfset arguments.event.setValue('rb', variables.Resourcebundle) />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:
1 name=NameIf 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:
2 dob=Date Of birth
3 elevator=elevator
4 cigarette=cigarette
5 sidewalk=sidewalk1 name=Name
2 dob=Date Of birth
3 elevator=lift
4 cigarette=fag
5 sidewalk=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
1 <cfset rb = viewState.getValue('rb') />
-
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
1 <cfset rb = viewState.getValue('rb') />
2 <cfoutput>
3 #rg.getResource('name')#: #users_name#<br/>
4 #rg.getResource('dob')#: #dateFormat(dob, 'dd-mm-yyyy')#<br/>
5 </cfoutput>
Plase help me out
Plase help me out