"Edgardo Hames" <ehames / gmail.com> schrieb im Newsbeitrag
news:478c16ae040719105634d4bfb9 / mail.gmail.com...
> On Tue, 20 Jul 2004 01:07:07 +0900, Robert Klemme <bob.news / gmx.net>
wrote:
> >
> > "Edgardo Hames" <ehames / gmail.com> schrieb im Newsbeitrag
> > news:478c16ae04071907072c000169 / mail.gmail.com...
> >
> > > Hi, everybody. I wondered how would you write a ResourceBundle-like
> > > class in Ruby. Basically, what I need is hash which is initialized
by
> > > reading key,value pairs from a file.
> >
> > I'm not 100% sure at the moment, but the lookup
> > is two level, isn't it?  I mean, you select a bundle via Locale and
then
> > do the lookup along the Locale "hierarchy".  So you probably need some
> > parent member along these lines:
> >
> > class ResourceBundle < Hash
> >  attr_accessor :parent
> >
> >  def initialize(fileName)
> >    File.open(fileName) do |io|
> >      io.each do |line|
> >        /^\s*(\S+)\s*[=:]\s*(.*)$/ =~ line and self[$1]= $2
> >      end
> >    end
> >  end
> >
> >  def [](key)
> >    result = super
> >    result = parent[key] if result.nil? && parent
> >    result
> >  end
> >
> > end
>
> I don't understand the parent member. Is it the fall back bundle it is
> used in case the program doesn't found the localized one?

Yes.  Java Locale lookup is done like this:
getBundle uses the base name, the specified locale, and the default locale
(obtained from Locale.getDefault) to generate a sequence of candidate
bundle names. If the specified locale's language, country, and variant are
all empty strings, then the base name is the only candidate bundle name.
Otherwise, the following sequence is generated from the attribute values
of the specified locale (language1, country1, and variant1) and of the
default locale (language2, country2, and variant2):

  a.. baseName + "_" + language1 + "_" + country1 + "_" + variant1
  b.. baseName + "_" + language1 + "_" + country1
  c.. baseName + "_" + language1
  d.. baseName + "_" + language2 + "_" + country2 + "_" + variant2
  e.. baseName + "_" + language2 + "_" + country2
  f.. baseName + "_" + language2
  g.. baseName
Taken from:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader)


> When is it
> filled up with data?

Well, the code was just meant as a quick hack illustrating example.
#initialize probably should not do the reading from the file at all.
Rather some other method should do it and set parents properly.

Regards

    robert