Search code examples
coldfusionmultilingualcfquery

cfml, database and a multilingual website


I have a database that has four columns id,languageid,name,text

Depending on the users' default language, I create a query that has all the texts for the set language (where languageid=#user.defaultlanguageid#)

What's the easiest way of retrieving these when it comes to displaying the required string.

Seems like creating a subquery every time is a bit much work.

Is creating a function the best way to go?


Solution

  • You could just have a single query that populates a struct (perhaps an application-level struct) - something like this:

    <cfif not IsDefined("application.langMap")>
    
    <cfquery name="langNames" datasource="...">SELECT * from langTable</cfquery>
    
    <cfset application.langMap = {}>
    <cfloop query="langNames">
       <cfif not StructKeyExists(application.langMap, languageid)>
           <cfset application.langMap[languageid] = {}>
       </cfif>
       <cfset application.langMap[languageid][name] = text>
    </cfloop>
    
    </cfif>
    

    And then as you need the particular string within the display:

    #application.langMap[mylanguageid][name]#