Last active
August 11, 2016 19:25
-
-
Save dnando/89130034858d417ebe2b51bc4772f195 to your computer and use it in GitHub Desktop.
Component core of multilingual system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent accessors="true"> | |
<cfproperty name="uiDataSource" default="uiDataSource" /> | |
<cfscript> | |
public any function rt( required string label, string queryString, string frameworkAction, string lan = session.lan ) { | |
/* | |
this function takes the label passed into it, hashes it, and looks it up in the structure variables.t ( generated by loadTerms() below ) | |
if the term is not found in the user's language, arguments.label is rendered instead | |
note that labels are case sensitive because a unique hash is generated for every case variation of a given string | |
if the user has permission to edit the UI terms and arguments.queryString exists | |
the label is rendered as a link to usr.editTerm&th=#termHash#&or=#origin# ( if the hash of arguments.label exists ) | |
or the label is rendered as a link to usr.editTerm&newTerm=#URLEncodedFormat(arguments.label, 'utf-8')#&or=#origin#' ( if the hash of arguments.label does not exist ) | |
and the user is returned to where they clicked on the link | |
example usage: | |
#rt('Invoices', cgi.query_string)# for cases where a link can be rendered | |
or #rt('Invoices')# for cases where a link cannot be rendered ( such as within another link, within a dropdown, or for messages set in the controller ) | |
links are rendered solely as a convenience - to enable the translation of terms in place and in context wherever possible | |
calls directly to rt('Some Term') can be made from view or layout files | |
calls from controllers are made with the fw prefix > fw.rt('Some Term') | |
*/ | |
var termExists = false; | |
var hashExists = false; | |
var termHash = hash( trim(arguments.label) ); | |
var renderedTerm = trim(arguments.label); | |
if ( structKeyExists( variables.t, termHash ) ) { | |
hashExists = true; | |
if ( structKeyExists( variables.t[termhash], arguments.lan ) ) { | |
renderedTerm = variables.t[termHash][arguments.lan]; | |
termExists = true; | |
} | |
} | |
if ( ListContains(session.permission,"usr.terms") and structKeyExists( arguments, 'queryString' ) ) { | |
// remove the framework.action portion of the query string so that fw.redirect can be used to bring edits in place back to the page of origin | |
var origin = ReReplace( arguments.queryString, '^#arguments.frameworkAction#=', '' ); | |
if ( hashExists ) { | |
renderedTerm = "<a class='rt' href='?#arguments.frameworkAction#=usr.editTerm&th=#termHash#&or=#URLEncodedFormat(origin)#'>#renderedTerm#</a>"; | |
} else { | |
renderedTerm = "<a class='rt' href='?#arguments.frameworkAction#=usr.editTerm&newTerm=#URLEncodedFormat(arguments.label, 'utf-8')#&or=#URLEncodedFormat(origin)#'>#renderedTerm#`</a>"; | |
} | |
} | |
return renderedTerm; | |
} | |
public void function loadTerms() { | |
/* | |
called by onApplicationStart() or by process that saves terms to the database after save is complete | |
queries for all UI terms in the database and builds a structure, t, populated according to the following pattern | |
t.hash.lan.term, with hash being the hash of the english label passed into the rt( ) function, | |
and lan being the 2 letter abbreviation of the language of the term | |
*/ | |
var qUserInterfaceTerms = findAllTerms(); | |
variables.t = {}; | |
var x = 1; | |
for (x = 1; x <= qUserInterfaceTerms.RecordCount; x=x+1) { | |
variables.t[qUserInterfaceTerms.termHash[x]][qUserInterfaceTerms.lan[x]] = qUserInterfaceTerms.term[x]; | |
} | |
} | |
</cfscript> | |
<cffunction name="findActiveLanguages" returntype="query" access="public" output="false"> | |
<cfset var qActiveLangages = '' /> | |
<cfquery name="qActiveLangages" datasource="#getUiDataSource()#"> | |
select languageName, lan | |
from Language | |
where isActive = 1 | |
order by sortOrder | |
</cfquery> | |
<cfreturn qActiveLangages /> | |
</cffunction> | |
<cffunction name="findLanguageFromLan" returntype="string" access="public" output="false"> | |
<cfargument name="lan" type="string" required="true" /> | |
<cfset var qLanguage = '' /> | |
<cfset var languageName = '' /> | |
<cfquery name="qLanguage" datasource="#getUiDataSource()#"> | |
select languageName from Language | |
where lan = <cfqueryparam value = "#arguments.lan#" cfsqltype="cf_sql_varchar" /> | |
</cfquery> | |
<cfif qLanguage.RecordCount> | |
<cfset languageName = qLanguage.languageName /> | |
</cfif> | |
<cfreturn languageName /> | |
</cffunction> | |
<cffunction name="findTermsByHash" returntype="query" access="public" output="false"> | |
<cfargument name="termHash" type="string" required="true" /> | |
<cfset var qTerms = '' /> | |
<cfquery name="qTerms" datasource="#getUiDataSource()#"> | |
select uiTermId, termHash, term, lan | |
from UITerm | |
where termHash = <cfqueryparam value="#arguments.termHash#" cfsqltype="cf_sql_varchar" /> | |
</cfquery> | |
<cfreturn qTerms /> | |
</cffunction> | |
<cffunction name="findAllTerms" returntype="query" access="public" output="false"> | |
<cfset var qAllTerms = '' /> | |
<cfquery name="qAllTerms" datasource="#getUiDataSource()#"> | |
select uiTermId, termHash, term, lan | |
from UITerm | |
order by term | |
</cfquery> | |
<cfreturn qAllTerms /> | |
</cffunction> | |
<cffunction name="checkForDuplicateTerm" returntype="boolean" access="public" output="false"> | |
<cfargument name="termHash" type="string" required="true" /> | |
<cfset var qTerm = '' /> | |
<cfset isDuplicate = false /> | |
<cfquery name="qTerm" datasource="#getUiDataSource()#"> | |
select uiTermId | |
from UITerm | |
where termHash = <cfqueryparam value="#arguments.termHash#" cfsqltype="cf_sql_varchar" /> | |
</cfquery> | |
<cfif qTerm.RecordCount> | |
<cfset isDuplicate = true /> | |
</cfif> | |
<cfreturn isDuplicate /> | |
</cffunction> | |
<cffunction name="findAllHashes" returntype="query" access="public" output="false"> | |
<cfargument name="allTerms" type="query" required="true" /> | |
<cfset qAllHashes = '' /> | |
<cfquery name="qAllHashes" dbtype="query"> | |
select termHash | |
from arguments.allTerms | |
where lan = 'en' | |
order by term | |
</cfquery> | |
<cfreturn qAllHashes /> | |
</cffunction> | |
<cffunction name="deleteTerms" returntype="void" access="public" output="false"> | |
<cfargument name="hash" type="string" required="true" /> | |
<cfset var qDeleteTerms = '' /> | |
<cfquery name="qDeleteTerms" datasource="#getUiDataSource()#"> | |
delete | |
from UITerm | |
where termHash = <cfqueryparam value="#arguments.hash#" cfsqltype="cf_sql_varchar" /> | |
</cfquery> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment