Home arrow Ruby-on-Rails arrow 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
(Page 1 of 5 )

Implementing the Add Translation User Story

Add the following code (showing the create action) to translate_controller.rb :

  def create
    from = params[:view][:text]
    singular = params[:view][:singular_form]
    plural = params[:view][:plural_form]
    if(plural.empty? && !singular.empty? && !from.empty?)

      Locale.set_pluralized_translation(from, 1, singular)
      flash[:notice] = "Translated '#{from}' to '#{singular}'"
   
elsif(!plural.empty? && !singular.empty? && !from.empty?)
      Locale.set_translation(from, Locale.language, singular, plural)
      flash[:notice] = ?
"Translated '#{from}' to singular '#{singular}' and plural '#{plural}'"
    else
      flash[:notice] = ?
"Please specify singular and/or plural form for the translation"
    end
    redirect_to :controller => 'translate', :action => 'index'
 
end

The if clause checks if the user is entering a translation that has only a singular form, and then uses Locale.set_pluralized_translation to add the translation to the database. The first parameter is the base language text, the second parameter specifies that the text is in singular form ( 1 for singular and 0 for plural), and the last parameter specifies the translated text in singular form.

The elsif clause uses Locale.set_translation to add a new translation to the database, which has both singular and plural forms. The method’s first argument is the base language text, the second is the current locale, the third is the singular form, and the fourth is the plural form of the translated text.

The controller shows an error message if no text was entered in both the singular and text fields. At the end of the action, we redirect the user to the same translation list page.

Type “Next page” in the Text field and “Nästa sida” in the Singular form field, and then click Add translation. This time, it should succeed, and you should see the list showing the new translation at the top of the list, as shown in Figure 10-4.

You could now try to click the “Nästa sida” text, but the in-place editor would show an error message, because we haven’t created the action for it yet.


Figure 10-4.  Translation view showing a list of translations


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