[CF](CFWheels 1.0.4) [pl] Strona wielojęzykowa [en] Multilanguage site
by jmail on Jun.04, 2010, under PHP / CF / .NET / Java
ColdFusion on Wheels got one big problem. They do not have support for multi language. I have made some researches and found out one plugin for multi language site. It call Localizer but it wasn’t that what I was looking for.
So I’ve decide to made this by my own and here it is! Multi language support for site
1. Add folder langs in main CFWheels dir
2. Add folder default in langs dir (this will hold default language files)
3. Add file globals.cfm into langs/default dir (this will hold all stuff you need in many controllers)
4. Insert into globals.cfm file all langs you need (for every Controller) for example:
Key:Value|
login:LOG IN|
signin:SIGN IN|
logout:LOGOUT|
Note:
- if you need to use : in your lang string use HTML CODE 58
- if you need to use | in your lang string use HTML CODE 124
5. Add function readLangs to your’s Controller.cfc file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <cffunction name="readLangs"> <cfset langs = StructNew()> <cfif isDefined("session.language")> <cfset langToRead="#session.language#"> <cfelse> <cfset langToRead="default"> </cfif> <cfif DirectoryExists("#application.wheels.rootPath#langs/#langToRead#")> <cfif FileExists("#application.wheels.rootPath#langs/#langToRead#/globals.mo")> <cffile action="read" file="#application.wheels.rootPath#langs/#langToRead#/globals.mo" variable="globalLang"> <cfset langsArray = ListToArray(trim(globalLang), "|")> <cfloop index="i" from="1" to="#arrayLen(langsArray)#"> <cfset langsPos = ListToArray(trim(langsArray[i]), ":")> <cfset "langs.#langsPos[1]#" = langsPos[2]> </cfloop> </cfif> </cfif> <cfif DirectoryExists("#application.wheels.rootPath#langs/#langToRead#/#Controller#")> <cfif FileExists("#application.wheels.rootPath#langs/#langToRead#/#Controller#/#Action#.mo")> <cffile action="read" file="#application.wheels.rootPath#langs/#langToRead#/#Controller#/#Action#.mo" variable="actionLang"> <cfset langsArray = ListToArray(trim(actionLang), "|")> <cfloop index="i" from="1" to="#arrayLen(langsArray)#"> <cfset langsPos = ListToArray(trim(langsArray[i]), ":")> <cfset "langs.#langsPos[1]#" = langsPos[2]> </cfloop> </cfif> </cfif> <cfset application.langs = langs> </cffunction> |
6. Add function getLang to your’s Controller.cfc file
1 2 3 4 5 6 7 8 9 | <cffunction name="getLang"> <cfargument name="key" required="true" hint="This argument provides a key from a structure `langs` which value will be returned"> <cfif isDefined("application.langs.#key#")> <cfset lang = #Evaluate("application.langs.#key#")#> <cfelse> <cfset lang = ""> </cfif> <cfreturn lang> </cffunction> |
7. Use it in your view file:
1 2 3 | <cfoutput> #getLang("Key")# </cfoutput> |
8. To create new language simple copy dirs and files from langs/default dir into langs/langNameOrCode dir
langNameOrCode – that is your own choice
9. Set the session.lang variable to change the language
1 | <cfset session.language = "pl"> |
in my example
10. In default and langNameOrCode dirs create dirs for each controller (just like you are doing in the view dir) and create file for each action with extension mo
For example I’ve got controller Main and action index so I will create:
/langs/default/Main/index.mo
/langs/pl/Main/index.mo
/langs/en/Main/index.mo
and the last thing I need to do is to change the session.lang name to whatever I want
That’s it