Home arrow Ruby-on-Rails arrow Page 3 - Getting Text and Currency Working for Multiple Language Support

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
  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

Getting Text and Currency Working for Multiple Language Support - Setting Character Encoding in HTML
(Page 3 of 5 )

Start by changing the HTML page to use the UTF-8 character encoding. Update the layout file, app/views/layout/application.rhtml, as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
<html xmlns="
    lang="<%= Locale.active.language.iso_639_1 -%>"
    xml:lang="<%= Locale.active.language.iso_639_1 -%>">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title><%= @page_title || 'Emporium' %></title>
  <%= stylesheet_link_tag "style" %>
  <%= javascript_include_tag :defaults %>

<div id="header">
  <h1 id="logo">Emporium&trade;</h1>

We specify the character encoding by adding a Content-Type meta tag to the head tag. This will help browsers select the appropriate character encoding while rendering the page.

We also changed the html tag so that it declares that the content of the page is written in the language currently selected ( Globalize::Locale.active.language ). This helps search engines in identifying your content and also helps you in positioning your site higher in search results in languages other than English. This is referred to as search engine optimization (SEO).

Tip  Currently, we change the locale by appending a parameter to the URL, such as /catalog?locale=sv . This is not optimal from a SEO perspective, as some search engines prefer to index pages without parameters. To fix this, you can add the following to your config/routes.rb file: map.connect ':language/:controller/:action/:id', :defaults => { :language => Locale.language.code, :id => nil } . Then you can use URLs like /sv/catalog or /de/catalog instead, which are more likely to be indexed by search engines.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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