Java ResourceBundle like class in Ruby

E

Edgardo Hames

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've thought of this

class ResourceBundle < Hash
def initialize(fileName)
#load data from fileName
end
end

What do you think of this approach?

Regards,
Ed
 
A

Austin Ziegler

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've thought of this

class ResourceBundle < Hash
def initialize(fileName)
#load data from fileName
end
end

What do you think of this approach?

Something like PStore might be what you need. I'm not sure, as I've
never used it.

-austin
 
R

Robert Klemme

Edgardo Hames said:
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've thought of this

class ResourceBundle < Hash
def initialize(fileName)
#load data from fileName
end
end

What do you think of this approach?

Sounds reasonable enough. 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

Regards

robert
 
G

gabriele renzi

il Mon, 19 Jul 2004 23:07:52 +0900, Edgardo Hames <[email protected]>
ha scritto::


maybe you can use an hash and set the default value as a proc that
reads the value from the file.
Or just use *DBM or PStore
 
E

Edgardo Hames

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? When is it
filled up with data?

Regards,
Ed
 
A

Ara.T.Howard

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've thought of this

class ResourceBundle < Hash
def initialize(fileName)
#load data from fileName
end
end

What do you think of this approach?

Regards,
Ed

use yaml:

~ > cat a.rb
require 'yaml'

class ResourceBundle < Hash
def initialize path
update(YAML::load(IO.read(path)))
end
end

rb = ResourceBundle.new ARGV.shift
p rb.keys
p rb['answer']

~ > cat a.cfg
foo: bar
key: value
answer: 42

~ > ruby a.rb a.cfg
["answer", "foo", "key"]
42

this also handle nesting for you:

~ > cat b.cfg
foo: bar
key: value
answer:
nested: 42

~ > ruby a.rb b.cfg
["answer", "foo", "key"]
{"nested"=>42}


yaml is the light. ;-)

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
N

NAKAMURA, Hiroshi

Hi,

Edgardo said:
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've thought of this

class ResourceBundle < Hash
def initialize(fileName)
#load data from fileName
end
end

What do you think of this approach?

Though I have not yet understood what you need clearly, when you are
talking user defined Properties in Java instead of ResourceBundle,
lib/soap/property.rb and test/soap/test_property.rb in ruby/1.8.* or
ruby/1.9.* can be an implementation sample.

My implementation does not support parent-child property chaining of
ResourceBundle. Robert's one is clearly demonstrating the concept (and
already enough useful).

Regards,
// NaHi
 
R

Robert Klemme

Edgardo Hames said:
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...ing, java.util.Locale, java.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,842
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top