Can this be done using sessions?

I have yet to make another website with multiple languages, 6 this time! I’m a bit fed up by doing this using different folders for each language, in other words having the same page multiple times, I.E. folder greek index.cfm, folder english index.cfm, folder russian index.cfm, and so on for other languages and of course other pages. Since 80 to 90 % from the content will be dynamic and there will not be a members section or something similar I was just thinking that this maybe can be done using sessions, but I can’t get my head around it how this should work in such a manner that the the choice of the visitor will be remembered as well. Has someone experienced a same situation or ideas how something like this could work, or would you say this is not a good idea? All suggestions are more than welcome!

Okay. I’ve been testing this for a few hours now and so far so good. The language switch is working when one of the different language flags is pressed. I have set the session time out to 2 hours and used cookies to remember the choice. Can anyone advise me what the downfall could be of using sessions/cookies? And again, If there would be any other(better?) suggestions! I really would like to hear them

of course it’s a good idea

sessions will work fine, but you should also use cookies

and separate folders for separate languages? seems a bit excessive

if the content is being retrieved from a database, you simply pull out the rows of data in language that the user prefers (or else in the default language)

That was my thought as well Rudy. I have indeed set a default session.language and depending on which flag is pressed the session.language is set to that particular language. I indeed have set cookies to remember the chosen language. I just wanted to be sure! Sometimes you think you have a brilliant plan, which than turns out to have all kind of holes. Thanks for the advise :tup:

One last question about this. This is how I have structured it so far:


<cfif Not structKeyExists( Session, 'language' )>
	<cfset Session.language = "greek" >	
<cfelseif structKeyExists( Url, 'language' )>
	<cfset Session.language = Url.language >
  <cfcookie name="language" value="#Session.language#" expires="never">
</cfif>

  <cfif structKeyExists( Session, 'language' ) And Session.language Eq 'greek' OR structKeyExists( Cookie, 'language' ) AND Cookie.language EQ  'greek' >
		<cfmodule template="modules/site/greek/menu.cfm">  
  <cfelse>
 		<cfmodule template="modules/site/english/menu.cfm"> 
  </cfif>

This example is just for two languages and is about which menu to include, but would this be the right way to go?

This example is just for two languages and is about which menu to include, but would this be the right way to go?

There are thousands of languages in the world. Are you going to add every single one to your if statement? Ouch!!!

The way I’d approach this is to build templates and depending on the language of the user populate the template with their specific-content.

So for your menu I wouldn’t have greek/menu.cfm etc. … I’d simply have menu.cfm which would do something like :-


<cfquery datasource="#application.dsn#" name="getMenu">
select link,title
from menu
where lang = <cfqueryparam value="#session.language#" cfsqltype="CF_SQL_VARCHAR" />
</cfquery>

<ul>
<cfloop query="getMenu">
<li><a href="#link#">#title#</li>
</cfloop>
</ul>


Page wise I’d do something like :-


<cfquery datasource="#application.dsn#" name="getPageContent">
select title,intro,body
from content
where lang = <cfqueryparam value="#session.language#" cfsqltype="CF_SQL_VARCHAR" /> and pageID = <cfqueryparam value="#pageID#" cfsqltype="CF_SQL_VARCHAR" />
</cfquery>

<html>
<head><title>#getPageContent.title#</title>
<body>
<p>#intro#</p>
<p>#body#</p>
</body>
</html>


So no mention of the specific-language in the page, it’s all DB managed.

So if the client adds 100 more languages or takes anyway - no code change :slight_smile:

Thanks for that Clarkee21. Indeed way easier.

and you would write the query so that it looks for the user’s language, and returns the default language if the user didn’t pick one

When I came up with this question, I was in the middle of a project, which I finished yesterday. In the meantime I couldn’t stop thinking that I realy needed to find a more permanent solution for this, because more and more often do I need to make sites with at least 2 languages. Since I have some spare time for a few days this is the right moment I guess :slight_smile:

Like I said, I fully understand where you’re heading James with this example, I only try to figure what the best database setup would be in this case. Lets say I have about 10 pages that need to get their content from the database! What would be the best way? Have a seperate table languages and use the primary key of this table as a reference in the content table or should I store tha language value as used in the session.language as a field for each language. I.o.w.


<cfquery datasource="#application.dsn#" name="getPageContent">
select language,title,content
from content
where lang = <cfqueryparam value="#session.language#" cfsqltype="CF_SQL_VARCHAR" /> and pageID = <cfqueryparam value="#pageID#" cfsqltype="CF_SQL_VARCHAR" />
</cfquery>

I would appreciate your input on this

Have a seperate table languages and use the primary key of this table as a reference in the content table

Yeah a foreign key in the “content” table referencing the language type it belongs to seems reasonable enough to me. R397 might have other ideas ( he’s the DB expert around here :wink: ).

I’m not sure why you’ve got language in your select statement though. Is that some kind of label? You’ve got the language name in the session already.

In hindsight though maybe the session.language you mentioned in your last post should be a database record id instead for the word “Russian”. At least that keeps it completely unique and inline with your database.

So when the user selects a new language the session language id is changed. If they don’t select one then you could get ColdFusion to do a look up on the language table and get the default. That could be an extra column in the DB which has a flag set next to it ( 1: Default, 0 : Non ).

A few ideas for you.

I got that from the query example you mentioned in post #6


where lang = <cfqueryparam value="#session.language#" cfsqltype="CF_SQL_VARCHAR" /> and pageID = <cfqueryparam value="#pageID#" cfsqltype="CF_SQL_VARCHAR" />


I presumed that you mend, that there should be a field in the content table called language where the value should match Session.language.

If this is not what you mend, I am a bit confused, because I could get my head around that. Remember initially a language switch will find only place when one of the language flags is used. When this happens I use a Cookie so the choice will be remembered when the person revisit the page.