Home arrow Ruby-on-Rails arrow Page 5 - Translating a Site for Multiple Language Support
RUBY-ON-RAILS

Translating a Site for Multiple Language Support


In this third article in a four-part series on adding support for multiple languages to a Ruby-on-Rails ecomerce application, you will learn how to add a translation to a site, edit the translation, 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 / 10
July 01, 2010
TABLE OF CONTENTS:
  1. · Translating a Site for Multiple Language Support
  2. · Implementing the Edit Translation User Story
  3. · Implementing the Delete Translation User Story
  4. · Translating the Model
  5. · Localizing Dates, Numbers, and Currency

print this article
SEARCH DEVARTICLES

Translating a Site for Multiple Language Support - Localizing Dates, Numbers, and Currency
(Page 5 of 5 )

As we mentioned earlier, Globalize supports localization of dates, numbers, and currencies. We’ll demonstrate how this works on parts of the Emporium site.

Localizing Dates

We’ll show you how to localize dates by changing the forum that we created in Chapter 6. Recall that we implemented the View Forum user story, where we display a list of posts, including the date and time when the posts were created. Currently, the format is 01:50:50 2006-07-07, which is not formatted according to the selected locale.

To fix this, open the forum helper app/helpers/forum_helper.rb in your editor and change it as follows:

module ForumHelper
  def display_as_threads(posts)
    content = ''
    for post in posts
      url = link_to("#{h post.subject}", {:action => 'show', :id => post.id})
      margin_left = post.depth*20
      content << %(
      <div style="margin-left:#{margin_left}px">
       #{url} by #{h post.name} &middot; ?
#{post.created_at.localize(DEFAULT_DATE_FORMAT)}
      </div>)
    end
    content
  end
end

We changed the display_as_threads method to localize the date the post was created on.

Add the following line at the end of config/environment.rb to complete the localization change:

DEFAULT_DATE_FORMAT = '%H:%M:%S - %A %B %d
%Y'

Restart the server, open your browser, and change the locale to Swedish, by accessing http://localhost:3000/forum?locale=sv . The date should now look like this: 01:50:50 -Fredag Juli 07 2006.

The formatting directives for dates and times follow the same rules as strftime . Table 10-1 lists some of the more important data and time formatting directives.

Table 10-1. Some Formatting Directives for Dates and Times  

 

 

 

Code

Description

%a

Abbreviated weekday name; for example, Sun

%A

Full weekday name; for example, Sunday

%b

Abbreviated month name; for example, Jan

%B

Full month name; for example, January

%c

Preferred local date and time representation

%d

Day of the month

%H

Hour of the day, using the 24-hour clock format

%I

Hour of the day, using the 12-hour clock format

%j

Day of the year

%m

Month of the year

%M

Minute of the hour

%p

AM or PM

%S

Second of the minute

%U

Week number of the current year (count starting from Sunday)

%W

Week number of the current year (count starting from Monday)

%w

Day of the week

%y

Year without century

%Y

Year with century

%Z

Time zone name

 

 

 

Please check back tomorrow for the continuation of this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials