Monday, November 8, 2010

I18n yml files in Rails3

Your locale files are located in config/locales directory by default. Let's make two locale files: en.yml and for example: hu.yml. In that files we can describe our string translations what we will use in our page view with helpers. Take care of yml rules, you can use spaces only instead of tabs, and spaces determine level of string. You can use double quotes to define a string, but if you want to use quotes inside the string, use html entity: " for it. You can use html in strings fo course, but don't forget raw helper in view before. Finally, you can give variables to yml simply, between %{var} (in rails2: {{var}} ) signs and full string needs to be between double quotes. I used to create yml blocks by controllers.


Labels are very simple also. Just take under helpers: and controller_name: (helpers: and contact: in the example below). Submit definitions are the same like labels.


en:
  contact:
    prices:
      show_price: "%{price_dollar}$"
      quote: "Coordinator: & quot;Crucifixion?& quot? & lt;br /& gt; Mr. Cheeky: & quot;Er, no, freedom actually.& quot;"

  attributes:
    created_at: "Created at"
    updated_at: "Updated at"

  helpers:
    submit:
      contact:
        create: "Order"
        update: "Modify"
    label:
      contact:
        name: "Your name"
        company_name: "Company name"
        phone: "Phone number"
        email: "E-mail"
        comment: "Comment"

  date:
    formats:
      default: "%Y-%m-%d"
      short: "%b %d"
      long: "%B %d, %Y"
    day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
    abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
    month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
    abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
    order: [ :year, :month, :day ]
 
  time:
    formats:
      default: "%a, %d %b %Y %H:%M:%S %z"
      short: "%d %b %H:%M"
      long: "%B %d, %Y %H:%M"
    am: "am"
    pm: "pm"


And the reference in our view file (apps/views/contact/index.html.erb):


<%= form_for @contact, :as   => :contact,
   :url  => {:action => 'new'},
   :html => {} do |f| %>

<%=f.label :name %>

<%= f.submit %>

<%=t :show_price, :scope => [:contact, :prices], :price_dollar => 50 %>

<%=raw t(:quote, :scope => [:contact, :prices], :locale=>'en') %>
<%=l Date.today, :format => :long %>

You can define exact locale in view like :locale=>'hu'. Localize dates with l helper function. You can use I18n.locale in controller for getting current locale. You can get all locales in an array with: I18n.available_locales . Don't forget to check application.rb in config directory, where you can setting up default locale:


config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
     config.i18n.default_locale = :hu

Then make a method in app/controllers/application_controller.rb for users to change their language and create a before_filter:


class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale

  def set_locale
  locale = params[:locale] || session[:locale] || I18n.default_locale.to_s
  locale = I18n.available_locales.include?(locale.to_sym) ? locale : 18n.default_locale.to_s
  session[:locale] = I18n.locale = locale
  end
end

Then create your link to set locale in your layout: link_to('English', url_for(:locale => 'en')). That's all! Questions?

No comments:

Post a Comment