i18n url based breadcrumbs in Rails
in application_helper.rb
# Generate URL-based Breadcrumbs.
# Based on http://rubysnips.com/url-based-breadcrumbs
# Modified and adapted for i18n by Claudio Poli (http://www.icoretech.org)
def show_breadcrumbs(wrapper = "p", separator = " » ")
r = []
r << link_to(I18n.t(:home), "/")
url = request.path.split('?')
segments = url[0].split('/')
segments.shift
segments.each_with_index do |segment, i|
title = segment.gsub(/-/, ' ').titleize
title = I18n.t(title.downcase.to_sym, :default => title)
r << link_to_unless_current(title, "/" + (0..(i)).collect{ |seg| segments[seg] }.join("/"))
end
content_tag(wrapper, "#{I18n.t(:path)}: " + r.join(separator), :class => "breadcrumbs")
end
Rails I18n SimpleBacked Italian translation files
I opened up a little github repository to store italian language files for Rails’ recently integrated I18n gem.
You can find the files at http://github.com/masterkain/rails-i18n-italian-language/tree/master
Again on Rails Edge i18n with SimpleBackend
one hour ago a changeset changed how i18n in rails edge behaves, the main feature, apart some bug fixes, are that the language files can be in yaml.
it-IT:
activerecord:
errors:
# The values :model, :attribute and :value are always available for interpolation
# The value :count is available when applicable. Can be used for pluralization.
messages:
inclusion: "non è incluso nella lista"
exclusion: "è riservato"
invalid: "è invalido"
confirmation: "non coincide con la conferma"
accepted: "deve essere accettata"
empty: "non può essere vuoto"
blank: "non può essere vuoto"
too_long: "è troppo lungo (il massimo è {{count}} lettere)"
too_short: "è troppo corto (il minimo è {{count}} lettere)"
wrong_length: "è della lunghezza sbagliata (deve essere di {{count}} lettere)"
taken: "è già in uso"
not_a_number: "non è un numero"
greater_than: "deve essere superiore a {{count}}"
greater_than_or_equal_to: "deve essere superiore o uguale a {{count}}"
equal_to: "deve essere uguale a {{count}}"
less_than: "deve essere meno di {{count}}"
less_than_or_equal_to: "deve essere meno o uguale a {{count}}"
odd: "deve essere dispari"
even: "deve essere pari"
# Append your own errors here or at the model/attributes scope.
can_only_contain_letters_and_numbers: "può contenere solo lettere e numeri"
can_t_be_blank_or_is_not_valid: "non può essere vuoto o non valido"
models:
# Overrides default messages
attributes:
# Overrides model and default messages.
class ApplicationController < ActionController::Base
before_filter :set_locale
private
# Sets the locale for the current request.
def set_locale
# I18n.default_locale returns the current default locale. Defaults to 'en-US'
locale = params[:locale] || session[:locale] || (this_user.site_language if is_logged_in?) || I18n.default_locale
# TODO: insert a conditional check there to make sure be choose a valid supported locale.
session[:locale] = locale
# Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
I18n.locale = locale
# Allow client libraries to pass a block that populates the translation
# storage. Decoupled for backends like a db backend that persist their
# translations, so the backend can decide whether/when to yield or not.
I18n.populate do
# Allows client libraries to pass arguments that specify a source for
# translation data to be loaded by the backend. The backend defines
# acceptable sources.
# E.g. the provided SimpleBackend accepts a list of paths to translation
# files which are either named *.rb and contain plain Ruby Hashes or are
# named *.yml and contain YAML data.)
I18n.load_translations "lib/locale/#{locale}.yml"
end
end
More on i18n, active record error messages
Meanwhile I’m waiting for some fixes to be tossed in into rails core (YAML files instead of ruby for example), here’s how to translate your active record error message in rails edge using the new i18n framework.
I18n.backend.store_translations "it-IT", {
:active_record => {
:error_messages => {
:inclusion => "non è incluso nella lista",
:exclusion => "è riservato",
:invalid => "è invalido",
:confirmation => "non coincide con la conferma",
:accepted => "deve essere accettata",
:empty => "non può essere vuoto",
:blank => "non può essere vuoto",
:too_long => "è troppo lungo (il massimo è {{count}} lettere)",
:too_short => "è troppo corto (il minimo è {{count}} lettere)",
:wrong_length => "è della lunghezza sbagliata (deve essere di {{count}} lettere)",
:taken => "è già in uso",
:not_a_number => "non è un numero",
:greater_than => "deve essere superiore a {{count}}",
:greater_than_or_equal_to => "deve essere superiore o uguale a {{count}}",
:equal_to => "deve essere uguale a {{count}}",
:less_than => "deve essere meno di {{count}}",
:less_than_or_equal_to => "deve essere meno o uguale a {{count}}",
:odd => "deve essere dispari",
:even => "deve essere pari",
:can_only_contain_letters_and_numbers => "può contenere solo lettere e numeri"
}
},
:yes => "sì",
:no => "no"
}
Custom validation message example:
validates_format_of :username, :with => /^\w+$/i, :message => :can_only_contain_letters_and_numbers
rails 2.2 i18n for the impatients
it’s all very straightforward and you can taste it already, grab a copy of edge rails and do this:
create a directory called "locale" in lib, and place two files, for example en-US.rb and it-IT.rb
Inside write:
# en-US.rb
I18n.store_translations 'en-US',
:yes => "yes",
:no => "no",
:action_show => "show",
:welcome => 'Welcome {{name}}!',
:inbox => ['1 message', '{{count}} messages']
as you can see we now have some symbol with our translation, do the same for it-IT.
it’s worth noticing the last two lines, {{name}} is a placeholder, let’s see how the things works.
open your ApplicationHelper and use this shortcut
def t(*args)
translate(*args)
end
after that open a view and put this:
t(:welcome, :name => "kain") # sorry for the formatting
next: ApplicationController
before_filter :set_locale
def set_locale
default_locale = 'en-US'
locale = params[:locale] || session[:locale] || (this_user.site_language if is_logged_in?) || default_locale
session[:locale] = locale
I18n.locale = locale
I18n.populate do
require "lib/locale/#{locale}.rb" # << WARNING: this is dangerous, change the method to load libs, maybe initializer
end
end
voilà, all done.
thanks to http://almosteffortless.com/2008/07/21/simple-localization-in-rails-22/