Storing strings outside the ruby code

Hello,
I would like to store my strings, messages, etc. in some “property” file
like in Java. Is there any way how to store these messages outside the
ruby code?

jsd wrote:

Hello,
I would like to store my strings, messages, etc. in some “property” file
like in Java. Is there any way how to store these messages outside the
ruby code?

You want to store some strings in global variable, so you can access
them in your ruby code?

I don’t understand, please explain

I want to store my strings, flash/error messages in some external file,
e.g. /app/helpers/my_strings.messages
In this file I want to have something like :

error.msg.login.admin=“You need to login before…”
error.msg.field.required=“This field is required,…”

generally:
(key=“value”)

Later, I want to access this file with my messages through some object :
e.g. Messages.read(“error.msg.login.admin”) - and this will return value
associated to specific key (analogy of Messages.properties file in
Java).

Is this possible in RoR applications?

Jamal S. wrote:

jsd wrote:

Hello,
I would like to store my strings, messages, etc. in some “property” file
like in Java. Is there any way how to store these messages outside the
ruby code?

You want to store some strings in global variable, so you can access
them in your ruby code?

I don’t understand, please explain

On Jun 2, 6:25 am, jsd [email protected] wrote:

Hello,
I would like to store my strings, messages, etc. in some “property” file
like in Java. Is there any way how to store these messages outside the
ruby code?

I assume you’re wanting to do this as part of some i18n or l10n
effort. But even if not, the following should apply. I don’t have a
really good straight answer for you, but you should check out the
discussion on this page:

http://wiki.rubyonrails.org/rails/pages/Internationalization

As well as this forum: http://www.ruby-forum.com/forum/20

If you’re looking for localizing you rails application, look here:

http://wiki.rubyonrails.org/rails/pages/InternationalizationComparison

But if you want a simple file with messages, you can use YAML…

– my_strings.messages file (It must conform to YAML file format):
error.msg.login.admin: You need to login before…
error.msg.field.required: This field is required,…

– ruby code to load it
require “yaml”
messsage = File.open(‘my_strings.messages’) { |yaml_file|
YAML::load(yaml_file) }

– and use it like a hash (It’s a Hash indeed!)
messsage[‘error.msg.login.admin’]

HTH

  • H

On 6/2/07, jsd [email protected] wrote:

them in your ruby code?

I don’t understand, please explain


Posted via http://www.ruby-forum.com/.


Husein C.
Yucca Intelligence Development

Do you mean like in YAML or some such? I think AWDWR or Ruby for Rails
describes marshalling objects so they can be stored in a file.

Bill

Hello

I wanted a simular thing. Property files in JAVA were great, but they
don’t seem to match Rails Testing Framework. Therefor I made a model
called ActiveProperty. This will store values as a key / value pair by
using an ActiveRecord.
Once stored, the property is available as simple method.

ApplicationProperty.test
→ “no key/value pair”
ApplicationProperty.test = “This is a test”
→ “This is a test”
ApplicationProperty.test
→ “This is a test”

See http://johnnybusca.blogspot.com/ for the source code with tests
how to use.

By storing these properties as activerecords, you can use the
active_property.yml fixture to override the values for test purposes.
This is very handy for testing file manupulations.

Best regards,
Jeroen