任务
我的任务是获取一个 php i18n 语言文件并将其转换为 ColdFusion 结构。这是输入的一部分
...
"ASK_DELETE" => "<em>D</em>elete", // 'd' is the accesskey identifier
"BACKUP_OF" => 'Backup of',
"PAGE_TITLE" => "Page Title",
...
当前的转换尝试
下面的代码确实适用于我收到的 php 语言文件。问题是,代码看起来像意大利面条代码的开头。我不喜欢的一些事情是
- 如果再出现一个字符串模式,事情就会崩溃
- 我有一个名为“值”的变量
- 大量基于中弦的斩波。
该代码看起来像意大利面条代码的开头。对此应该做哪些事情?
<cffunction name="readPropertiesFile" returnType="Struct" hint="Read a properties file and return a structure">
<cfargument name="propertiesFile" type="string" required="true" hint="path to properties file">
<cfscript>
VAR stProperties = {};
VAR phpText = "";
VAR key = "";
VAR value = "";
VAR line = "";
var propertiesFilePath = "#GetDirectoryFromPath(GetBaseTemplatePath())##arguments.propertiesfile#";
if (NOT FileExists(propertiesFilePath))
return stProperties;
</cfscript>
<!--- read props file --->
<cffile action="read" file="#propertiesFilePath#" variable="phpText">
<!--- remove any whitespace at top and tail --->
<cfset phpText = trim(phpText)>
<!--- remove comments and blank lines --->
<cfset phpText = ReReplace(phpText,"(?m)\##.*?$", "","all")>
<cfset phpText = ReReplace(phpText,"[#Chr(10)#]{2,}", "#Chr(10)#","all")>
<cfset phpText = ReplaceList(phpText, '",', '"')>
<!--- loop over each line, ignore comments (#...) and insert keys/values into return struct --->
<cfloop list="#phpText#" index="line" delimiters="#CHR(10)#">
<cfscript>
line = trim(line);
splitAt = Find("=>", line);
CommentAt = Find("//", line);
if (splitAt != 0) {
key = replacelist(trim(Left(line, splitAt - 1)), '"', "");
if (CommentAt == 0)
value = replacelist(trim(Mid(line, splitAt + 2, 1000)), '"', '');
else
value = replacelist(trim(Mid(line, splitAt + 2, CommentAt - (splitAt + 2))), '"', '');
// Remove trailing ,
if (right(value, 1) == ",")
value = mid(value, 1, len(value) - 1);
if (right(value, 1) == "'")
value = mid(value, 1, len(value) - 1);
if (left(value, 1) == "'")
value = mid(value, 2, 1000);
stProperties[key] = value;
} // end if
</cfscript>
</cfloop>
<cfreturn stProperties>