(beginner) Coldfusion: variable to change language

Discussion in 'Programming' started by pekkapekka, Jul 31, 2008.

  1. #1
    Heyya,

    Whilst working at other stuff this summer, my boss wanted me to fix some stuff on a certain (small)website associated with my work. Sure, I thought, not knowing the site was built pretty much entirely in CF. Anyways, I thought I'd give it a shot so I've been reading the code and googling/trying to figure out how the stuff works.

    The thing I'm trying to do is add a little function which switches the site's language back and forth with links on the main page. I began thinking that using a variable that could be changed could work. So i pretty much defined a variable in application.cfm (lets call it "language") and did cfif's which compares "language" to a string, around two versions of the header (just the header to begin with).

    So, that works fine while manually changing the "language" variable in the code of application.cfm. What I just can't figure out is how I can make a link on the site, when clicked, change that variable to a specified string. Because it feels like, if I could do that, then it would work. When I've attempted to write code for this, it seems as you can't directly change a variable in application.cfm from the browser-side, true?

    I then thought, "ok, then I make a 'switchlang.cfm' and write some code changing the variable". Here is pretty much where I got stuck, as my half-assed attempts didn't get me anywhere.

    So please, please help me with this. Any pointers, solutions is most welcome. Am I going about this in a totally wrong way? I'm just looking for an easy solution that works.

    Please go easy on me, I just saw CF code a couple of weeks ago, so I may have written some major "wrongs", i know... :)
     
    pekkapekka, Jul 31, 2008 IP
  2. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #2
    This works if you want to change the application variable "language" from one language to another...

    application.cfm
    
    <cfapplication name="language" 
    			   clientmanagement="Yes" 
    			   sessionmanagement="Yes" 
    			   setclientcookies="Yes">
    
    
    
    <cfset language = 'english'>
    
    Code (markup):
    Page you want to change with links.
    change the parts in red to work on your site..
    
    <cfif IsDefined('url.language')>
    
    <cfset application.language = #URL.LANGUAGE#>
    
    </cfif>
    
    <a href="[COLOR="Red"]NameOfPage[/COLOR].cfm?language=english">English</a> | <a href="[COLOR="red"]NameOfPage[/COLOR].cfm?language=spanish">Spanish</a><br/><br/>
    
    <cfoutput>#application.language#</cfoutput><br/><br/>
    
    Code (markup):
    Good luck... if you got question please ask them :)
     
    unitedlocalbands, Jul 31, 2008 IP
  3. pekkapekka

    pekkapekka Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help! Hm...

    Thing is, the html for the header&footer (of which I have two versions, with different language) is in the application.cfm file. And it seems I cant write <a href="application.cfm?language=english">..
     
    pekkapekka, Aug 1, 2008 IP
  4. dshuck

    dshuck Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Do a little googling for "Paul Hastings" and "i18n". He has written some great stuff on internationalization in ColdFusion and has been involved in a number of community projects. If you pull down the example Mach-II application "MachBlog", there is a great example of how to do this using a ResourceBundle component that pulls back the appropriate language as you need it. Basically in your request, you set some variable or event arg indicating the language you wish to display within that request. Then whenever you are ready to write output, you pull the appropriate text from the i18n resource bundle rather than keeping two individual views. It is *very* flexible and very easy to use. We took this approach when we wrote www.instantspot.com. Although we are only actually enabling English at the moment, anywhere that you see text, it could alternatively be displayed in German or Spanish. For instance.... in a place where you might have just put:

    <a href="/index.cfm/event/Login">Log in here</a>

    we do:

    <a href="/index.cfm/event/Login><cfoutput>#event.getArg("ResourceBundle").getResource("LogInHere")#</cfoutput></a>

    ...and based on whatever language is currently established within that event, the appropriate text is displayed.

    They took a slightly different approach with MachBlog where the language is defined at the application level, but it is easy to change that concept to set it per request for each visitor.
     
    dshuck, Aug 1, 2008 IP
  5. dshuck

    dshuck Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    One other point that should probably be made... if you take the suggestion given by unitedlocalbands, you will be changing it for the entire application anytime that you change it, and not just for a particular user and particular request. If you are planning on delivering different languages to different people, that is definitely not the approach that you want to take.

    If you just want to always have your language be in English, or always have it be in Spanish, that would be fine, but even then I think it is a tightly coupled approach that I would likely try to avoid.
     
    dshuck, Aug 1, 2008 IP
  6. pekkapekka

    pekkapekka Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hey! I will check out that stuff and see if I can get it to work. (just quitting work now so prob. on Monday). I understand what you're saying about changing it for the application - I didn't know it worked like that. Gotta avoid that then, definately.

    When you're saying "very easy", I wonder how the level of difficulty is for me, though. Do you think I could fix my problem with this approach in ~2w, being a total cf noob (I'm a quick learner, but not superman)?

    Thanks for taking the time to help!
     
    pekkapekka, Aug 1, 2008 IP
  7. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #7
    If you dont want the language to change for the whole application then change the variable to a session scope variable instead of an application variable. Then the language will only change for the user viewing the site and not everyone else to.

    so instead of

    
    <cfset language = 'english'>
    
    Code (markup):
    you would use
    
    <cfset session.language = 'english'>
    
    Code (markup):
    So use the session scope instead.

    Still sounds like dshuck's suggestion is more efficient, I would not want to have to keep separate files for every language either.

    But now you have a way of allowing users to set variables if the need arises...

    Take care.
     
    unitedlocalbands, Aug 1, 2008 IP
  8. pekkapekka

    pekkapekka Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well I continued trying with this.. (unitedlocalbands code)..

    First i put the header and footer in separate templates (header.cfm and footer.cfm). Then I was trying with your code, and I get an error:

    "Routines cannot be declared more than once.
    The routine header has been declared twice in different templates. "

    This appears when trying to change the language with
    <a href="header.cfm?language=swe">Swedish</a>
     
    pekkapekka, Aug 4, 2008 IP
  9. phydiux

    phydiux Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You should start reading up on form, session and URL scopes - you can have the browser set variables through all of these scopes. Adobe's Using ColdFusion Variables should help you out.

    Can you copy the code from your header.cfm and footer.cfm here? It sounds like you're trying to declare something more than once. Maybe you're trying to declare the cfapplication more than once in the application.cfm file?

    dshuck's example is something commonly used in the open source world with people trying to create applications that're meant for various language transisions (check out open source stuff like PHPbb or Wordpress - yes, they're written in a different language, but have similar ideas behind them.) While it works and is a good idea for a new application, you'll find it's more challenging to implement on an existing application. I think it's in your best interest to figure out the ColdFusion first, before trying to implement calling component and integrating language templates into your site.
     
    phydiux, Aug 4, 2008 IP
  10. pekkapekka

    pekkapekka Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hey,

    First off: I appreciate that you took the time to help me. I've solved the problem now, so thank you all and goodbye for now! :)
     
    pekkapekka, Aug 6, 2008 IP