Question

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?

Was it helpful?

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]#
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top