{"id":2328,"date":"2012-05-08T14:23:20","date_gmt":"2012-05-08T17:23:20","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=2328"},"modified":"2012-05-08T15:13:37","modified_gmt":"2012-05-08T18:13:37","slug":"i18n-alchemy-localization-and-parsing","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2012\/05\/i18n-alchemy-localization-and-parsing\/","title":{"rendered":"I18n Alchemy – localization and parsing"},"content":{"rendered":"
Today I want to show you a project I’ve started over a year ago, during Mendicant University<\/a> core skills course. For those who don’t know, Mendicant University is a group of skilled software developers that offer courses, mentoring, and help out the community, started by Gregory Brown<\/a>, and that nowadays counts with some other awesome folks as part of the staff. I highly recommend taking a look at and enrolling.<\/p>\n Back to I18n, during Mendicant University we were supposed to create a project in Ruby, not specifically with Rails, and I decided to scratch my own itch by trying to solve a problem we usually have in Brazil: receiving date\/time\/numeric input from user interface. I know and have already used the delocalized gem<\/a>, and it works quite nice, but sometimes I felt a bit uncomfortable about how it handled some parts of localization\/parsing. This is mainly due to the need to monkey patch both ActiveRecord to handle input, and ActionView to handle output. Besides that, and most important, I had to come up with some project and I thought that’d be a good challenge :D.<\/p>\n The main goal of this project is to provide a proxy object to use with your ORM (currently ActiveRecord only) that will be responsible for localizing and parsing the date\/time\/numeric attributes when getting or setting their values, respectively. Lets see some quick examples:<\/p>\n Now that we have a localized proxy for the And also date\/time attributes, for instance:<\/p>\n I18n Alchemy can also receive a hash of attributes, the same way you use with your models when calling The parsing\/localization formats are basically the same ones you already use in your Rails application. You can check the basic locale configuration for I18n Alchemy in its README on github<\/a>.<\/p>\n I18n Alchemy is a small and new project which solves most of the problems we commonly face when dealing with localization and parsing of date\/time\/numeric values. It is tested with Rails 3.0, 3.1 and 3.2 and works with all the basic methods, such as It was a really fun time creating it during Mendicant University, and it took a long time until I decided to release it as a gem. There is still a bunch of things to do, but I wanted to ask you to give it a try and let me know about any feedback you have.<\/p>\n As a side note, if you are interested in knowing more about the design decisions that led this project, you may want to take a look at Gregory Brown's post on Ruby Best Practices, entitled \"Issue #23: SOLID Design Principles\"<\/a>, more specifically in the Open\/closed principle topic.<\/p>\n I'm releasing the first 0.0.1 version today, and I hope you find it useful. Have any comments? Let us know!<\/p>\n","protected":false},"excerpt":{"rendered":" Today I want to show you a project I’ve started over a year ago, during Mendicant University core skills course. For those who don’t know, Mendicant University is a group of skilled software developers that offer courses, mentoring, and help out the community, started by Gregory Brown, and that nowadays counts with some other awesome … \u00bb<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[163,58,161,165,162,7,60,164],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2328"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=2328"}],"version-history":[{"count":25,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2328\/revisions"}],"predecessor-version":[{"id":2694,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2328\/revisions\/2694"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=2328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=2328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=2328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\r\n# Include the proxy in your model\r\nclass Product < ActiveRecord::Base\r\n include I18n::Alchemy\r\nend\r\n\r\n# Grab your object from the database\r\n@product = Product.first\r\n# Instantiate the localized proxy\r\n@localized = @product.localized\r\n<\/pre>\n
@product<\/code> object, we can get\/set numeric attributes with localized values, such as:<\/p>\n
\r\n@localized.price = \"1.99\"\r\n\r\n@product.price # => 1.99\r\n@localized.price # => \"1.99\"\r\n\r\nI18n.with_locale :pt do\r\n @localized.price = \"1,88\"\r\n\r\n @product.price # => 1.88\r\n @localized.price # => \"1,88\"\r\nend\r\n<\/pre>\n
\r\n@localized.released_at = \"12\/31\/2011\"\r\n\r\n@product.released_at # => Date.new(2011, 12, 31)\r\n@localized.released_at # => \"12\/31\/2011\"\r\n\r\nI18n.with_locale :pt do\r\n @localized.released_at = \"31\/12\/2011\"\r\n\r\n @product.released_at # => Date.new(2011, 12, 31)\r\n @localized.released_at # => \"31\/12\/2011\"\r\nend\r\n<\/pre>\n
new<\/code>. That means you can use it like this:<\/p>\n
\r\n# You could be using params[:product] for instance.\r\nI18n.with_locale :pt do\r\n @localized = @product.localized(:price => \"1,88\") \r\n\r\n @product.price # => 1.88\r\n @localized.price # => \"1,88\"\r\nend\r\n<\/pre>\n
Wrapping up<\/h2>\n
attributes=<\/code>,
assign_attributes<\/code>,
update_attributes<\/code> and nested attributes as well.<\/p>\n