ColdFusion:Android strings.xml文件功能

时间:2012-07-18 06:18:49

标签: coldfusion

我想知道ColdFusion中是否存在类似于Android strings.xml文件的任何字符串集中。 所以,如果我想对字符串

进行任何更改,我的代码将保持不变

1 个答案:

答案 0 :(得分:4)

ColdFusion是一种编程语言,而不是用户界面框架。

在ColdFusion中没有像Android的字符串资源管理那样,但是自己实现它很容易。

resources/strings.xml

<!-- keep as a structure with unique element names -->
<strings>
  <heading>This is a test.</heading>
  <greetings>
    <hello>Hello World!</hello>
    <bye>Goodbye World!</bye>
  </greetings>
</strings>

ColdFusion实用程序功能(例如在CFC组件util.cfc中):

<cffunction name="ReadResouceXml" returntype="struct" access="public" output="no">
  <cfargument name="path"   type="string" required="yes">
  <!--- internal use argument --->
  <cfargument name="xmlDoc" type="xml"    required="no">

  <cfset var xmlElem = "">
  <cfset var output  = StructNew()>

  <!--- read XML file from disk --->
  <cfif not StructKeyExists(arguments, "xmlDoc")>
    <cffile action="read" file="#ExpandPath(path)#" variable="xmlDoc" charset="UTF-8">
    <cfset xmlDoc = XmlParse(xmlDoc).XmlRoot>
  </cfif>      

  <!--- recursively convert XML to a CF struct --->
  <cfloop index="i" from="1" to="#ArrayLen(xmlDoc.XmlChildren)#">
    <cfset xmlElem = xmlDoc.XmlChildren[i]>
    <cfif ArrayLen(xmlElem.XmlChildren) gt 0>
      <cfset output[xmlElem.XmlName] = ReadResouceXml("", xmlElem)>
    <cfelse>
      <cfset output[xmlElem.XmlName] = xmlElem.XmlText>
    </cfif>
  </cfloop>
  <cfreturn output>
</cffunction>

Coldfusion用法:

<cfobject type="component" name="util" component="util">
<cfset strings = util.ReadResouceXml("resources/strings.xml")>

<cfoutput>
<h1>#strings.heading#</h1>
#strings.greetings.hello# - #strings.greetings.bye#
</cfoutput>