| [5] | 1 | <!--- |
|---|
| 2 | This code is a modified version of the resourceBundle.cfc by Paul Hastings. |
|---|
| 3 | You can find the original code + examples here: |
|---|
| 4 | |
|---|
| 5 | http://www.sustainablegis.com/unicode/resourceBundle/rb.cfm |
|---|
| 6 | |
|---|
| 7 | My modifications were to simply add a few var statements to rbLocale and |
|---|
| 8 | to add a few more methods, as well as adding persistance to the CFC. |
|---|
| 9 | ---> |
|---|
| 10 | |
|---|
| 11 | <cfcomponent displayname="resourceBundle" hint="Reads and parses resource bundle per locale"> |
|---|
| 12 | |
|---|
| 13 | <cffunction name="getResource" access="public" output="false" returnType="string" |
|---|
| 14 | hint="Returns bundle.X, if it exists, and optionally wraps it ** if debug mode."> |
|---|
| 15 | <cfargument name="resource" type="string" required="true"> |
|---|
| 16 | <cfset var val = ""> |
|---|
| 17 | |
|---|
| 18 | <cfif not isDefined("variables.resourceBundle")> |
|---|
| 19 | <cfthrow message="Fatal error: resource bundle not loaded."> |
|---|
| 20 | </cfif> |
|---|
| 21 | |
|---|
| 22 | <cfif not structKeyExists(variables.resourceBundle, arguments.resource)> |
|---|
| 23 | <cfset val = "_UNKNOWNTRANSLATION_"> |
|---|
| 24 | <cfelse> |
|---|
| 25 | <cfset val = variables.resourceBundle[arguments.resource]> |
|---|
| 26 | </cfif> |
|---|
| 27 | |
|---|
| 28 | <cfif isDebugMode()> |
|---|
| 29 | <cfset val = "*** #val# ***"> |
|---|
| 30 | </cfif> |
|---|
| 31 | |
|---|
| 32 | <cfreturn val> |
|---|
| 33 | |
|---|
| 34 | </cffunction> |
|---|
| 35 | |
|---|
| 36 | <cffunction access="public" name="getResourceBundle" output="No" returntype="struct" hint="reads and parses java resource bundle per locale"> |
|---|
| 37 | <cfargument name="rbFile" required="Yes" type="string"> |
|---|
| 38 | <cfargument name="rbLocale" required="No" type="string" default="en_US"> |
|---|
| 39 | <cfargument name="markDebug" required="No" type="boolean" default="false"> |
|---|
| 40 | |
|---|
| 41 | <cfset var isOk=false> <!--- success flag ---> |
|---|
| 42 | <cfset var rbIndx=""> <!--- var to hold rb keys ---> |
|---|
| 43 | <cfset var resourceBundle=structNew()> <!--- structure to hold resource bundle ---> |
|---|
| 44 | <cfset var thisLang=listFirst(arguments.rbLocale,"_")> |
|---|
| 45 | <cfset var thisDir=GetDirectoryFromPath(arguments.rbFile)> |
|---|
| 46 | <cfset var thisFile=getFileFromPath(arguments.rbFile)&".properties"> |
|---|
| 47 | <cfset var thisRBfile=thisDir & listFirst(thisFile,".") & "_"& arguments.rbLocale & "." & listLast(thisFile,".")> |
|---|
| 48 | <cfset var resourceBundleFile = ""> |
|---|
| 49 | |
|---|
| 50 | <cfif NOT fileExists(thisRBfile)> <!--- try just the language ---> |
|---|
| 51 | <cfset thisRBfile=thisDir & listFirst(thisFile,".") & "_"& thisLang & "." & listLast(thisFile,".")> |
|---|
| 52 | </cfif> |
|---|
| 53 | <cfif NOT fileExists(thisRBfile)> <!--- still nothing? strip thisRBfile back to base rb ---> |
|---|
| 54 | <cfset thisRBfile=arguments.rbFile&".properties"> |
|---|
| 55 | </cfif> |
|---|
| 56 | <cfif fileExists(thisRBFile)> |
|---|
| 57 | <cfset isOK=true> |
|---|
| 58 | <cffile action="read" file="#thisRBfile#" variable="resourceBundleFile" charset="utf-8"> |
|---|
| 59 | <cfloop index="rbIndx" list="#resourceBundleFile#" delimiters="#chr(10)#"> |
|---|
| 60 | <cfif len(trim(rbIndx)) and left(rbIndx,1) NEQ "##"> |
|---|
| 61 | <cfif NOT arguments.markDebug> |
|---|
| 62 | <cfset resourceBundle[trim(listFirst(rbIndx,"="))] = trim(listRest(rbIndx,"="))> |
|---|
| 63 | <cfelse> |
|---|
| 64 | <cfset resourceBundle[trim(listFirst(rbIndx,"="))] = "***"& trim(listRest(rbIndx,"=")) &"***"> |
|---|
| 65 | </cfif> |
|---|
| 66 | </cfif> |
|---|
| 67 | </cfloop> |
|---|
| 68 | </cfif> |
|---|
| 69 | <cfif isOK> |
|---|
| 70 | <cfreturn resourceBundle> |
|---|
| 71 | <cfelse> |
|---|
| 72 | <cfthrow message="Fatal error: resource bundle #thisRBfile# not found."> |
|---|
| 73 | </cfif> |
|---|
| 74 | </cffunction> |
|---|
| 75 | |
|---|
| 76 | <cffunction name="getResourceBundleData" access="public" output="No" hint="returns the struct only" returntype="struct"> |
|---|
| 77 | <cfreturn variables.resourceBundle> |
|---|
| 78 | </cffunction> |
|---|
| 79 | |
|---|
| 80 | <cffunction name="loadResourceBundle" access="public" output="false" returnType="void" |
|---|
| 81 | hint="Loads a bundle"> |
|---|
| 82 | <cfargument name="rbFile" required="Yes" type="string" hint="This must be the path + filename UP to but NOT including the locale. We auto-add .properties to the end."> |
|---|
| 83 | <cfargument name="rbLocale" required="No" type="string" default="en_US"> |
|---|
| 84 | |
|---|
| 85 | <cfset variables.resourceBundle = getResourceBundle(arguments.rbFile, arguments.rbLocale)> |
|---|
| 86 | </cffunction> |
|---|
| 87 | |
|---|
| 88 | </cfcomponent> |
|---|