models

Class ResourceService

lucee.Component
    extended by models.ResourceService

Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp www.ortussolutions.com Inspired by Paul Hastings --- This service allows you to work with java/json resource bundles for localization of strings. It also has several convenience methods when working with replacements of localized strings.

Class Attributes:
  • singleton
  •  
  • synchronized : false
  •  
  • accessors : true
  •  
  • persistent : false
  •  
    Property Summary
    type property default serializable required
    struct bundles
          The bundles loaded into the resource service identifiable by key name.

    • access = public
    • returntype = any
    true false
    any controller


    • access = public
    • returntype = any
    • inject = coldbox
    true false
    any i18n


    • access = public
    • returntype = any
    • inject = provider:i18n@cbi18n
    true false
    any interceptorService


    • access = public
    • returntype = any
    • inject = coldbox:interceptorService
    true false
    any log


    • access = public
    • returntype = any
    • inject = logbox:logger:{this}
    true false
    any settings


    • access = public
    • returntype = any
    • inject = coldbox:moduleSettings:cbi18n
    true false
    Constructor Summary
    init()
          Constructor.
    Method Summary
    private any discoverResourcePath(any resourcePath)
         Locate the resource on disk and check whether it's a Java or JSON bundle.
    private any flattenStruct(struct originalStruct, [struct flattenedStruct='[runtime expression]'], [string prefixString=''])
         flatten a struct, so we can use keys in format 'main.
    string formatRBString(any rbString, any values)
         Performs messageFormat like operation on compound rb string.
    string getBundles()
    string getController()
    string getI18n()
    string getInterceptorService()
    array getLoadedBundles()
         Get a list of all loaded bundles.
    string getLog()
    array getRBKeys(any rbFile, [any rbLocale=''])
         Returns an array of keys from a specific resource bundle.
    any getRBString(any rbFile, any rbKey, [any rbLocale='en_US'], [any defaultValue])
         Returns a given key from a specific resource bundle file and locale.
    any getResource(any resource, [any defaultValue], [any locale='[runtime expression]'], [any values], [any bundle='default'])
         Get a resource from a specific loaded bundle and locale.
    struct getResourceBundle(any rbFile, [any rbLocale='en_US'])
         Reads,parses and returns a resource bundle in struct format.
    private any getResourceFileInputStream(string rbFilePath)
         get Java FileInputStream for resource bundle.
    string getSettings()
    ResourceService loadBundle(string rBFile, [string rbLocale='en_US'], [boolean force='false'], [string rbAlias='default'])
         Tries to load a resource bundle into ColdBox memory if not loaded already.
    private struct loadBundleFromDisk(string resourceBundleFullPath)
         Load a bundle from disk.
    private any loadJavaResource(string resourceBundleFullPath)
         loads a java resource file from file.
    private any loadJsonResource(string resourceBundleFullPath)
         loads a JSON resource file from file.
    string messageFormat(string thisPattern, any args, [any thisLocale=''])
         performs messageFormat on compound rb string.
    any setBundles(struct bundles)
    any setController(any controller)
    any setI18n(any i18n)
    any setInterceptorService(any interceptorService)
    any setLog(any log)
    any setSettings(any settings)
    boolean verifyPattern(string pattern)
         Performs verification on MessageFormat pattern.
     
    Methods inherited from class lucee.Component
    None

    Constructor Detail

    init

    public init()

    Constructor


    Property Detail

    bundles

    property struct bundles

    The bundles loaded into the resource service identifiable by key name

    Attributes:
    access - public
    required - false
    returntype - any
    serializable - true

    controller

    property any controller

    Attributes:
    access - public
    required - false
    returntype - any
    inject - coldbox
    serializable - true

    i18n

    property any i18n

    Attributes:
    access - public
    required - false
    returntype - any
    inject - provider:i18n@cbi18n
    serializable - true

    interceptorService

    property any interceptorService

    Attributes:
    access - public
    required - false
    returntype - any
    inject - coldbox:interceptorService
    serializable - true

    log

    property any log

    Attributes:
    access - public
    required - false
    returntype - any
    inject - logbox:logger:{this}
    serializable - true

    settings

    property any settings

    Attributes:
    access - public
    required - false
    returntype - any
    inject - coldbox:moduleSettings:cbi18n
    serializable - true

    Method Detail

    discoverResourcePath

    private any discoverResourcePath(any resourcePath)

    Locate the resource on disk and check whether it's a Java or JSON bundle.

    Parameters:
    resourcePath - The resource path with no extension included
    Returns:
    The located properties or json path or empty path indicating it was not located.

    flattenStruct

    private any flattenStruct(struct originalStruct, [struct flattenedStruct='[runtime expression]'], [string prefixString=''])

    flatten a struct, so we can use keys in format 'main.sub1.sub2.resource'.

    Parameters:
    originalStruct - true
    flattenedStruct - necessary for recursion
    prefixString
    Returns:
    struct resourcebundle
    Throws:
    ResourceBundle.InvalidBundlePath

    formatRBString

    public string formatRBString(any rbString, any values)

    Performs messageFormat like operation on compound rb string. So if you have a string with {1} it will replace it. You can also have multiple and send in an array to do replacements.

    Parameters:
    rbString - A localized string with {bnamed|positional} replacements
    values - Array, Struct or single value to format into the rbString

    getBundles

    public string getBundles()


    getController

    public string getController()


    getI18n

    public string getI18n()


    getInterceptorService

    public string getInterceptorService()


    getLoadedBundles

    public array getLoadedBundles()

    Get a list of all loaded bundles

    Returns:
    array of all keys of loaded bundles

    getLog

    public string getLog()


    getRBKeys

    public array getRBKeys(any rbFile, [any rbLocale=''])

    Returns an array of keys from a specific resource bundle. NOT FROM MEMORY

    Parameters:
    rbFile - This must be the path + filename UP to but NOT including the locale. We auto-add the local and .properties to the end.
    rbLocale - The locale to use, if not passed, defaults to default locale.
    Throws:
    ResourceBundle.InvalidBundlePath if bundlePath is not found

    getRBString

    public any getRBString(any rbFile, any rbKey, [any rbLocale='en_US'], [any defaultValue])

    Returns a given key from a specific resource bundle file and locale. NOT FROM MEMORY

    Parameters:
    rbFile - This must be the path + filename UP to but NOT including the locale. We auto-add the local and .properties to the end.
    rbKey - The key to retrieve
    rbLocale - The locale of the bundle. Default is en_US
    defaultValue - A default value to send back if resource not found
    Throws:
    ResourceBundle.RBKeyNotFoundException if rbKey is not found

    getResource

    public any getResource(any resource, [any defaultValue], [any locale='[runtime expression]'], [any values], [any bundle='default'])

    Get a resource from a specific loaded bundle and locale

    Parameters:
    resource - The resource (key) to retrieve from the main loaded bundle.
    defaultValue - A default value to send back if the resource (key) not found
    locale - Pass in which locale to take the resource from. By default it uses the user's current set locale
    values - An array, struct or simple string of value replacements to use on the resource string
    bundle - The bundle alias to use to get the resource from when using multiple resource bundles. By default the bundle name used is 'default'

    getResourceBundle

    public struct getResourceBundle(any rbFile, [any rbLocale='en_US'])

    Reads,parses and returns a resource bundle in struct format. It also merges the hierarchical bundles for country and variant if found.

    Parameters:
    rbFile - This must be the path + filename UP to but NOT including the locale. We auto-add the local and .properties to the end.
    rbLocale - The locale of the resource bundle
    Throws:
    ResourceBundle.InvalidBundlePath if bundlePath is not found

    getResourceFileInputStream

    private any getResourceFileInputStream(string rbFilePath)

    get Java FileInputStream for resource bundle

    Parameters:
    rbFilePath - path + filename for resource, including locale + .properties
    Returns:
    java.io.FileInputStream
    Throws:
    ResourceBundle.InvalidBundlePath

    getSettings

    public string getSettings()


    loadBundle

    public ResourceService loadBundle(string rBFile, [string rbLocale='en_US'], [boolean force='false'], [string rbAlias='default'])

    Tries to load a resource bundle into ColdBox memory if not loaded already

    Parameters:
    rBFile - This must be the path + filename UP to but NOT including the locale. We auto-add .properties or .json to the end alongside the locale
    rbLocale - The locale of the bundle to load
    force - Forces the loading of the bundle even if its in memory
    rbAlias - The unique alias name used to store this resource bundle in memory. The default name is the name of the rbFile passed if not passed.

    loadBundleFromDisk

    private struct loadBundleFromDisk(string resourceBundleFullPath)

    Load a bundle from disk

    Parameters:
    resourceBundleFullPath - full path to a (partial) resourceFile
    Returns:
    struct resourcebundle
    Throws:
    ResourceBundle.InvalidBundlePath

    loadJavaResource

    private any loadJavaResource(string resourceBundleFullPath)

    loads a java resource file from file

    Parameters:
    resourceBundleFullPath - full path to a (partial) resourceFile
    Returns:
    struct resourcebundle
    Throws:
    ResourceBundle.InvalidBundlePath

    loadJsonResource

    private any loadJsonResource(string resourceBundleFullPath)

    loads a JSON resource file from file

    Parameters:
    resourceBundleFullPath - full path to a (partial) resourceFile
    Returns:
    struct resourcebundle
    Throws:
    ResourceBundle.InvalidJSONBundlePath

    messageFormat

    public string messageFormat(string thisPattern, any args, [any thisLocale=''])

    performs messageFormat on compound rb string

    Parameters:
    thisPattern - pattern to use in formatting
    args - substitution values, simple or array
    thisLocale - locale to use in formatting, defaults to en_US

    setBundles

    public any setBundles(struct bundles)

    Parameters:
    bundles

    setController

    public any setController(any controller)

    Parameters:
    controller

    setI18n

    public any setI18n(any i18n)

    Parameters:
    i18n

    setInterceptorService

    public any setInterceptorService(any interceptorService)

    Parameters:
    interceptorService

    setLog

    public any setLog(any log)

    Parameters:
    log

    setSettings

    public any setSettings(any settings)

    Parameters:
    settings

    verifyPattern

    public boolean verifyPattern(string pattern)

    Performs verification on MessageFormat pattern

    Parameters:
    pattern - format pattern to test