#943 New config and localization APIs

brian Thu 28 Jan 2010

Just wanted to post a preview of the new API I've prototyped for the new Env design. Couple issues I wanted to solve:

  • config and localization should delegate to Env for pluggability
  • wanted a consistent caching strategy for props file
  • localization was limited to resources in pod, I wanted it to allow overrides with props file in etc too (most of my customers do localization in the field)
  • wanted to standardize on props as the standard file format (way simpler and way more efficient that arbitrary serialized objects)

This is the API I have developed so far (as methods on Env class):

**
** Return a merged key/value map of all the prop files found
** using the following resolution rules:
**   1. `Env.findAllFiles`: "etc/{pod}/{uri}"
**   2. `Pod.files`: "/{uri}"
**
** The uri must be relative.
**
** The files are parsed using `InStream.readProps` and merged according
** to their priority order.  If the file is defined as a resource in
** the pod itself, then it is treated as lowest priority.  The first
** file returned by 'findAllFiles' is treated as highest priority and
** overwrites any key-value pairs defined at a lower priority.
**
** The map is cached so that subsequent calls for the same path
** doesn't require accessing the file system again.  The 'maxAge'
** parameter specifies the tolerance accepted before a cache
** refresh is performed to check if any of the files have been
** modified.
**
** Also see `Pod.props`.
**
virtual Str:Str props(Pod pod, Uri uri, Duration maxAge)

**
** Lookup a configuration property for given pod/key pair.
** If not found then return 'def'.  Default implementation
** routes to `props` using max age of one minute:
**
**   props(pod, `config.props`, 1min).get(key, def)
**
** Also see `Pod.config`.
**
virtual Str? config(Pod pod, Str key, Str? def := null)

**
** Lookup a localized property for the specified pod/key pair.
** The following rules are used for resolution:
**   1. 'props(pod, `locale/{locale}.props`)'
**   2. 'props(pod, `locale/{lang}.props`)'
**   3. 'props(pod, `locale/en.props`)'
**   4. Fallback to 'pod::key' unless 'def' specified
**
** Where '{locale}' is `Locale.toStr` and '{lang}' is `Locale.lang`.
**
** Also see `Pod.locale`.
**
virtual Str? locale(Pod pod, Str key, Str? def := "pod::key", Locale locale := Locale.cur)

Login or Signup to reply.