我正在处理在Coldfusion 9中完成的应用程序。我必须在CF11中迁移它,我想改进代码。 CF9中的当前应用程序如下所示:
Application.cfm :
<cfapplication name="MyApp"
clientManagement="yes"
clientStorage="registry"
setClientCookies="yes"
sessionManagement="yes"
sessionTimeout="#CreateTimeSpan(0,1,0,0)#"
applicationTimeout="#CreateTimeSpan(0,0,2,0)#"
/>
<cfset application.datasource = "MyApp"/>
<cfset application.name = "MyApp"/>
<cfset application.access = "App"/>
<cfset application.version = "1.1"/>
<cfset application.title = "My Application"/>
<cfset application.inc_path = "includes"/>
<cfset application.com_path = "components"/>
<cfset application.scripts_path = "scripts"/>
<cfset application.styles_path = "styles"/>
<cfset application.email = "firstname.lastnane@myapplication.com" />
<cfset application.YEAR = 2016/>
<cfif Not IsDefined("session.language_cd")>
<cfset session.language_cd = "ENG">
</cfif>
<cfset session.email_support_team = "support@myapplication.com" />
<cfinclude template="ui_lib.cfm">
在文件&#34; ui_lib.cfm &#34;中,在所有其他.cfm文件中定义并使用了很多变量:
<cfswitch expression="#session.ui_lng_cd#">
<cfcase value="FRA">
<cfset ui_lib_abbrev = "Abbréviation" />
<cfset ui_lib_acces = "Accès" />
<cfset ui_lib_active_sans_accent = "Actif" />
</cfcase>
<cfcase value="ENG">
<cfset ui_lib_abbrev = "Abbreviation" />
<cfset ui_lib_acces = "Access" />
<cfset ui_lib_active_sans_accent = "Active" />
</cfcase>
</cfswitch>
例如在 menu.cfm :
中<div id="menu">
<h5><cfoutput>#session.user#</cfoutput></h5>
<ul>
<li><cfoutput>#ui_lib_abbrev#</cfoutput></li>
<li><cfoutput>#ui_lib_acces#</cfoutput></li>
<li><cfoutput>#ui_lib_active_sans_accent#</cfoutput></li>
</ul>
</div>
我尝试创建 Application.cfc 来替换 application.cfm :
<cfcomponent displayname="MyApp">
<cfset This.name = "MyApp">
<cfset This.sessionManagement="yes">
<cfset This.clientManagement="no">
<cfset This.loginStorage="session">
<cfset This.sessionTimeout = CreateTimeSpan(0,0,0,1)>
<cfset This.applicationTimeout = CreateTimeSpan(0,0,2,0) >
<cfset This.setClientCookies="no">
<cfset This.domainCookies="yes">
<cfset This.scriptProtect = "All">
<cffunction name="onApplicationStart">
<cfscript>
Application.homePage = "/index.cfm";
Application.datasource = "MyApp";
Application.name = "MyApp";
Application.access = "App";
Application.version = "1.1";
Application.title = "My Application";
Application.inc_path = "includes";
Application.com_path = "components";
Application.scripts_path = "scripts";
Application.styles_path = "styles";
Application.email = "firstname.lastnane@myapplication.com"
Application.YEAR = 2016;
</cfscript>
<cfinclude template="ui_lib.cfm">
</cffunction>
<cffunction name="onSessionStart">
<cfif Not IsDefined("session.language_cd")>
<cfset session.language_cd = "ENG">
</cfif>
<cfif Not IsDefined("session.g_exercice")>
<cfset todayDate = Now()>
<cfset SESSION.g_exercice = #DateFormat(todayDate, "yyyy")#>
</cfif>
<cfif Not IsDefined("session.sec_first_pass")>
<cfset SESSION.sec_first_pass = 0>
</cfif>
<cfset session.email_support_team = "support@myapplication.com" />
</cffunction>
</cfcomponent>
我创建了application.cfc并删除了application.cfm并尝试运行该应用程序。没关系,但我对ui_lib.cfm中定义的变量有Coldfusion错误。服务器说没有定义变量。你能告诉我为什么以及如何解决这个问题吗?
我想知道使用具有相同定义的Application.cfc文件或保留当前文件Application.cfm是否更好?
提前感谢您的帮助。
答案 0 :(得分:2)
我已经对cfc转换做了一些cfm,答案取决于用法。如前所述,应用程序范围内基于会话的逻辑不起作用。
如果有一种方法可以更改用户的语言来更新session.ui_lng_cd值,从而更新显示给用户的语言,那么最简单的答案就是在OnRequestStart事件中设置语言变量。在设置和使用时,您必须将变量的范围更改为请求范围,但希望通过全局搜索和替换可以轻松完成。
附加说明:
也可以将这些语言变量存储在会话中,方法是将代码放在OnSessionStart事件中,但通常最好保持会话存储最小化,就好像你开始在会话中存储很多内容一样。繁忙的网站,它可以消耗大量的内存。
这样做的另一种方法需要更多的工作,但我认为如果你有很多这样的话会更有效率的是在应用程序中创建一个存储语言字符串的二级数组并使用会话语言作为第一个关键水平。这样,每次对旧变量的请求都会使用非常少的内存。
所以在你的application.cfc OnApplicationStart中你有:
application.languageStrings = {};
application.languageStrings["ENG"] = {};
application.languageStrings["ENG"]["ui_lib_abbrev"] = "Abbreviation";
application.languageStrings["ENG"]["ui_lib_acces"] = "Access";
application.languageStrings["ENG"]["ui_lib_active_sans_accent"] = "Active";
application.languageStrings["FRA"] = {};
application.languageStrings["FRA"]["ui_lib_abbrev"] = "Abbréviatio";
application.languageStrings["FRA"]["ui_lib_acces"] = "Accès";
application.languageStrings["FRA"]["ui_lib_active_sans_accent"] = "Actif";
要输出正确的字符串,请使用:
#application.languageStrings[session.language_cd]["ui_lib_active_sans_accent"]#
答案 1 :(得分:1)
UDF中定义的变量位于variables
范围内,但是通过将UDF置于onApplicationStart()
内,include只发生一次而不是每次请求。我想你已经想出来并在variables
范围内再次定义它们。
我觉得更合适的解决方案是将它们放入application
范围,这样它们只是定义了一次,而不是在每次请求时重新定义它们。
application.ui = {
"ENG" = {
"abbrev" = "Abbreviation"
, "acces" = "Access"
, "active_sans_accent" = "Active"
}
, "FRA" = {
"abbrev" = "Abbréviation"
, "acces" = "Accès"
, "active_sans_accent" = "Actif"
}
};
然后只需更新任何现有的引用:#application.ui[session.ui_lng_cd].abbrev#
。
如果将来需要管理翻译的上下文,请查看Resource Bundles。