Home arrow Ruby-on-Rails arrow Page 4 - 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 for the HTTP Response
(Page 4 of 5 )

Next, add a new after_filter to app/controllers/application.rb that sends the correct Content-Type HTTP header. Update app/controllers/application.rb as follows:

class ApplicationController < ActionController::Base
before_filter :set_locale
after_filter :set_charset


  def set_charset
    headers["Content-Type"] = "text/html; charset=utf-8" if headers ?

  def set_locale
    accept_lang = request.env['HTTP_ACCEPT_LANGUAGE']
    accept_lang = accept_lang.empty? ? nil : accept_lang[/[^,;]+/]

    locale = params[:locale] || session[:locale] || accept_lang || DEFAULT_LOCALE

Now, when a user accesses a page at the Emporium site, the following response header is sent to the browser:

Content-Type: text/html; charset=utf-8

This helps the browser in selecting the correct character encoding for displaying your page.

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