Iam a nubie, i wanna know how can i load/read a .properties file in to
my rails application. For example i have a property file where i have
defined the spanish equivalants of some english words as follows
user Name:nombre del usuario
user Code :código del usuario
likewise. now , where should i store this properties file in the
directory structure and how can i read these in to my application and
display in the view page accordingly?
When you say .properties file, what format is it in? Your example
looks a lot like YAML, which Ruby has good support for, for instance.
If it were a YAML file, you could just open the file into a YAML
object (which you can find out about here http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html) and parse
it as such.
XML would also be relatively easily parsable.
At worst case, you could parse the .properties file with some form of
regex.
Does that help? Any more questions, based on those ideas?
Good Morning. well,the .properties file i mention is just a text file
similar to in Java where we load a .properties file in to a class and
parse accordingly.
First of all, i would like to know where should i place this .properties
file,its exact physical location in the directroy structure of my rails
application?
Now i am able to find a piece of code to load a .properties file using
ruby
as follows
def load_properties(properties_filename)
properties = {}
File.open(properties_filename, ‘r’) do |properties_file|
properties_file.read.each_line do |line|
line.strip!
if (line[0] != ?# and line[0] != ?=)
i = line.index(‘=’)
if (i)
properties[line[0…i - 1].strip] = line[i + 1…-1].strip
else
properties[line] = ‘’
end
end
end
end
properties
end
But i dont know how from where should i load this file ? and where
should i write this code, is it in the controller or model?
if possible, tell me how can i parse an xml file as .properties file
Awaiting your reply
Sathya
Tom A. wrote:
When you say .properties file, what format is it in? Your example
looks a lot like YAML, which Ruby has good support for, for instance.
If it were a YAML file, you could just open the file into a YAML
object (which you can find out about here http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html) and parse
it as such.
XML would also be relatively easily parsable.
At worst case, you could parse the .properties file with some form of
regex.
Does that help? Any more questions, based on those ideas?
Can you not just put it in the config dir? And just read from there.
Also, it strikes me that it would be far easier to take an objective
view on it and say, well, rails doesnt support .properties, im not
working in java, so i best use YAML, which it has great support for and
would save me a whole heap of work as all the class files exsist to
manipulate YAML. The only reason you might want to use .properties is if
you were porting from a java app and you have thousands of strings in
the file which you didnt want to convert to YAML - unless this is the
case just seems like YAML would be the better option, would certainly
yeild more flexibility.
Furthermore, if you want to use it for internationalisation, take a look
at the ‘globalize’ plugin. There is as yet no support in rails for
multi-language like there is in java.
Cheers
Tim
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.