Home arrow Ruby-on-Rails arrow Getting Text and Currency Working for Multiple Language Support
RUBY-ON-RAILS

Getting Text and Currency Working for Multiple Language Support


In this conclusion to a four-part article series on adding support for multiple languages to a Ruby-on-Rails ecomerce application, you will learn how to localize numbers and currency, set character encoding, and more. This article is excerpted from chapter 10 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
July 02, 2010
TABLE OF CONTENTS:
  1. · Getting Text and Currency Working for Multiple Language Support
  2. · Adding Unicode (UTF-8) Support
  3. · Setting Character Encoding in HTML
  4. · Setting Character Encoding for the HTTP Response
  5. · Changing the Database to Use UTF-8

print this article
SEARCH DEVARTICLES

Getting Text and Currency Working for Multiple Language Support
(Page 1 of 5 )

Localizing Numbers and Currencies

Next, we’ll show you how to change the product catalog to display the correctly formatted page count and price for the currently selected locale.

Open app/views/catalog/catalog.rhtml in your editor and append .localize to the code that prints the page count:

<h2>by <%= @book.authors.map{|a| a.name}.join(", ") %></h2>
<%= image_tag url_for_file_column(:book, :cover_image) ?
unless @book.cover_image.blank? % >
<dl>
 
<dt>Price</dt>
  <dd>$<%= @book.price -%></dd>
  <dt>Page count</dt>
 
<dd><%= @book.page_count.localize -%></dd>

This will now localize the page count. For example, a book having 400000 pages will be displayed as 400,000.

To localize the price field, we first need to modify the Book model ( app/models/book.rb ), as shown here:

class Book < ActiveRecord::Base
  acts_as_taggable

  has_and_belongs_to_many :authors
  belongs_to :publisher
  has_many :cart_items
  has_many :carts, :through => :cart_items

  translates :title, :blurb
  acts_as_ferret :fields => [:title, :author_names]

  composed_of :price_localized, :class_name => "Globalize::Currency",
      :mapping => [ %w(price cents) ]

This adds the new field price_localized to the model, which will be properly formatted when used in a view.

Next, change the catalog view ( app/views/catalog/show.rhtml ) to use the new field:

<h2>by <%= @book.authors.map{|a| a.name}.join(", ") %></h2>
<%= image_tag url_for_file_column(:book, :cover_image) ?
unless @book.cover_image.blank? % >
<dl>
 
<dt>Price</dt>
 
<dd>$<%= @book.price_localized -%></dd>
 
<dt>Page count</dt>
  <dd><%= @book.page_count.localize -%></dd>

Now if you access the book details page ( http://localhost:3000/catalog/show/1 ), you should see the following:

--------------------------------------------
Price
    $10,000.00
Page count
    400,000
--------------------------------------------


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials