# Storing strings outside the ruby code

**URL:** <https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770>\
**Category:** Rails\
**Created:** [June 2, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770 "2007-06-02T12:25:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 2, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/1 "2007-06-02T12:25:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 2, 2007, 12:34pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/2 "2007-06-02T12:34:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 2, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/3 "2007-06-02T13:06:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 2, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/4 "2007-06-02T14:48:41Z")

</div>

On Jun 2, 6:25 am, jsd [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://wiki.rubyonrails.org/rails/pages/Internationalization)

As well as this forum: [http://www.ruby-forum.com/forum/20](http://www.ruby-forum.com/forum/20)

---

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 2, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/5 "2007-06-02T15:43:46Z")

</div>

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

[http://wiki.rubyonrails.org/rails/pages/InternationalizationComparison](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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > them in your ruby code?
> > 
> > I don’t understand, please explain
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Husein C.  
Yucca Intelligence Development

> **[قفل دیجیتال | قفل هوشمند | قفل الکترونیکی کره‌ای | یوکا](https://yuccahq.com/)**
>
> نگران قیمت بی‌ثبات هستی؟ از تخصصی‌ترین برند قفل هوشمند با اطمینان و تضمین قیمت خرید کن! معتبرترین برند کره جنوبی:قفل الکترونیکی یوکا همین الان تماس بگیرید و از تخفیف‌های شگفت‌انگیز ما استفاده کنید.

---

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 3, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/6 "2007-06-03T15:53:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jsd](https://www.ruby-forum.com/u/jsd)\
**Post date:** [June 3, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/storing-strings-outside-the-ruby-code/105770/7 "2007-06-03T14:42:00Z")

</div>

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/](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
