# L10n of numbers?

**URL:** <https://www.ruby-forum.com/t/l10n-of-numbers/125255>\
**Category:** Rails I18n\
**Created:** [January 7, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/l10n-of-numbers/125255 "2008-01-07T15:30:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [January 7, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/l10n-of-numbers/125255/1 "2008-01-07T15:30:38Z")

</div>

I need to localize numbers in text fields and have been looking at the  
various L10n plugins that claim to have number support. Support seems  
to mean that the plugin includes a Number#to\_localized\_string method,  
which isn’t very useful for my purposes.

I figure I could build my own numeric\_field helper easy enough; the  
question is how I’d go about parsing the input before it reaches  
Rails.

Currently I’m thinking along the lines of prefixing the field names  
with some string, e.g. ‘numeric\_value\_’, parsing the (possibly nested)  
parameter hash (from a before filter?) looking for these things, and  
then adding them back into params as standard English-formatted  
numbers without the prefix.

Thoughts/feedback…?

Regards,  
Isak

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [July 2, 2008, 11:46am UTC](https://www.ruby-forum.com/t/l10n-of-numbers/125255/2 "2008-07-02T11:46:22Z")

</div>

Hey Isak,

I currently working on the same problem. In my last projects I have  
workaround with an own setter for the numbers. e.g.

the active-record model  
class Article  
def price=(value)  
self.price = value.to\_delocalized\_decimal  
end  
end

and the String-class extension:

class String  
def to\_delocalized\_decimal  
return self if !self.is\_a?(String) or self.blank?  
BigDecimal.new(string.sub(’,’, ‘.’))  
end  
end

maybe this is usefull for your research-program. But i guess its better  
to use extra methods to fill your database-field. In this case the  
output would be also localized. e.g.

def localized\_price=(value)  
self.price = value.to\_delocalized\_decimal  
end  
def localized\_price  
price.to\_delocalized\_decimal # use another BigDecimal-extension  
end

and the premium-version would be an ActiveRecord::Base-extension. Any  
suggestions how to build such a plugin?

cheerio  
benni

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [July 2, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/l10n-of-numbers/125255/3 "2008-07-02T16:39:17Z")

</div>

On 2.7.2008, at 12.46, Benjamin M. wrote:

> end  
> maybe this is usefull for your research-program. But i guess its  
> better  
> to use extra methods to fill your database-field. In this case the  
> output would be also localized. e.g.
> 
> def localized\_price=(value)  
> self.price = value.to\_delocalized\_decimal  
> end  
> def localized\_price  
> price.to\_delocalized\_decimal # use another BigDecimal-extension  
> end

Just curious, but you do realize that to\_delocalized\_decimal is only  
defined in the String class? That has two consequences: any other  
object than a string will throw an exception. That’s ok, if you only  
ever get strings as values. However, in that case (and anyway since  
you’re defining the method in the String class) there’s no idea  
checking whether self is a string in the to\_delocalized\_decimal  
method. self.is\_a?(String) will always return true when used inside a  
method of the String class.

//jarkko

–  
Jarkko L.

> **[jlaine.net](https://jlaine.net/)**
>
> Your freedom to carry a gun messes with my freedom to walk around without fear. Your freedom to “stand your ground” with force is at odds with my …

> **[Home](https://dotherightthing.com/)**
>
> do the right thing

[http://www.railsecommerce.com](http://www.railsecommerce.com)  
[http://odesign.fi](http://odesign.fi)

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [July 2, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/l10n-of-numbers/125255/4 "2008-07-02T21:22:08Z")

</div>

On 2.7.2008, at 21.00, Benjamin M. wrote:

> end  
> def localized\_price=(value)  
> \<%= f.text\_field :localized\_price %\>
> 
> But I guess, you have a solution more elegant? I’m dreaming of an  
> ActiveRecordPlugin, so I just have to write …

Sorry, I’ve just used a before\_save filter defined in AR::Base. It  
would be fairly easy to make a plugin out of that, all it takes is  
some metaprogramming skills. If you aren’t familiar with  
metaprogramming in Ruby, I highly recommend PragDave’s recent  
screencasts  
([Pragmatic Bookshelf: By Developers, For Developers](http://www.pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming)  
). They’ll cost you $5 a piece, but with the current state of the  
dollar that’s not too much asked imho.

Cheers,  
//jarkko

–  
Jarkko L.

> **[jlaine.net](https://jlaine.net/)**
>
> Your freedom to carry a gun messes with my freedom to walk around without fear. Your freedom to “stand your ground” with force is at odds with my …

> **[Home](https://dotherightthing.com/)**
>
> do the right thing

[http://www.railsecommerce.com](http://www.railsecommerce.com)  
[http://odesign.fi](http://odesign.fi)

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [July 2, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/l10n-of-numbers/125255/5 "2008-07-02T20:00:34Z")

</div>

Hey Jarkko,  
thanks for your comment. I give it another try:

# the extensions

class String  
def to\_delocalized\_decimal  
BigDecimal.new(self.sub(’,’, ‘.’))  
rescue  
self  
end  
end  
class BigDecimal  
def to\_localized\_string(precision=2)  
("%01.#{precision}f" % self).to\_s.sub(’.’, ‘,’)  
rescue  
self.to\_s  
end  
end

# my ActiveRecord-Class

class Article \< ActiveRecord::Base  
def localized\_price=(value)  
self.price = value.to\_delocalized\_decimal unless value.blank?  
end  
def localized\_price  
price.to\_localized\_string if price  
end  
end

# my new.html.erb

…  
**Price**  
  
\<%= f.text\_field :localized\_price %\>

But I guess, you have a solution more elegant? I’m dreaming of an  
ActiveRecordPlugin, so I just have to write …

class Article  
localized\_decimal :price, :tax …  
end

… and I got the two setter and getter methods for free. Anyone have an  
idea how to make it?

Greetings  
Benni
