please see the Rails Plugin localization_simplified that includes most of the things metioned below.
While Rails and Ruby are not yet internationalized, there are still some things you can do. As Ruby classes and modules are open, you can change them from your own code. I have a directory app/overrides which contains among other things these files, resulting in german validation error messages and month names
# active_record/errors.rb module ActiveRecord class Errors begin @@default_error_messages.update( { :inclusion => "ist nicht in Liste gültiger Optionen enthalten", :exclusion => "ist reserviert", :invalid => "ist ungültig", :confirmation => "entspricht nicht der Bestätigung", :accepted => "muss akzeptiert werden", :empty => "darf nicht leer sein", :blank => "darf nicht leer sein", :too_long => "ist zu lang (höchstens %d Zeichen)", :too_short => "ist zu kurz (mindestens %d Zeichen)", :wrong_length => "hat eine falsche Länge (es sollten %d Zeichen sein)", :taken => "ist schon vergeben", :not_a_number => "ist keine Zahl", }) end end end # date.rb require 'date' class Date MONTHNAMES = [nil, 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember' ] end # all.rb Dir[File.dirname(__FILE__) + "/**/*.rb"].each { |file| require(file) }
Then, at the end of config/environment.rb I have
require "#{RAILS_ROOT}/app/overrides/all"
This is definitely not pretty. The best I can say is that it appears to
work and keeps the intrusive code confined to one place. It would be much better, if all verbatim strings within Rails were piped through gettext or similar.
One could use the method shown above to load gettext and wrap _() around the original verbatim strings. If there are no localizations, _() simply returns its argument so everything remains as it is, but for those willing/needing to localize the strings, there would be a clean hook to do so.
—MichaelSchuerig
You also might wish to override the basic error messages used in scaffolded pages and error_for helper:
module ActionView #nodoc
module Helpers
module ActiveRecordHelper
def error_messages_for(object_name, options = {})
options = options.symbolize_keys
object = instance_variable_get("@#{object_name}")
unless object.errors.empty?
content_tag("div",
content_tag(
options[:header_tag] || "h2",
"#{pluralize(object.errors.count, "error")} prohibited this #{object_name.to_s.gsub("_", " ")} from being saved"
) +
content_tag("p", "There were problems with the following fields:") +
content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) }),
"id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
)
end
end
end
end
end
and put this modification in the same file.
ri Date reveals the other constants awaiting localization: DAYNAMES, ABBR_MONTHNAMES and ABBR_DAYNAMES.
—Charles M. Gerungan
Globalize plugin has all this constants translated in quite a few languages already.
—Benol
NOTE: If your’re running fastCGI, you’ll have to stop and restart your web server after modifying environment.rb.
How can I do if I want multiple languages (for exemple: Fr, de,en) ?
See also HowToLocalizeActiveRecordErrors
require 'date'
# English: [nil] + %w(January February March April May June July August September October November December)
Date.const_set "MONTHNAMES", [nil] + %w(janúar febrúar mars apríl maí júní ágúst september október nóvember desember)
# English: [nil] + %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
Date.const_set "ABBR_MONTHNAMES", [nil] + %w(jan feb mar apr maí jún júl ág sept okt nóv des)
# English: %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
Date.const_set "DAYNAMES", %w(sunnudagur mánudagur þriðjudagur miðvikudagur fimmtudagur föstudagur laugardagur)
# English: %w(Sun Mon Tue Wed Thu Fri Sat)
Date.const_set "ABBR_DAYNAMES", %w(sun mán þri miðv fim fös lau)
—terceiro
category: I18n
please see the Rails Plugin localization_simplified that includes most of the things metioned below.
While Rails and Ruby are not yet internationalized, there are still some things you can do. As Ruby classes and modules are open, you can change them from your own code. I have a directory app/overrides which contains among other things these files, resulting in german validation error messages and month names
# active_record/errors.rb module ActiveRecord class Errors begin @@default_error_messages.update( { :inclusion => "ist nicht in Liste gültiger Optionen enthalten", :exclusion => "ist reserviert", :invalid => "ist ungültig", :confirmation => "entspricht nicht der Bestätigung", :accepted => "muss akzeptiert werden", :empty => "darf nicht leer sein", :blank => "darf nicht leer sein", :too_long => "ist zu lang (höchstens %d Zeichen)", :too_short => "ist zu kurz (mindestens %d Zeichen)", :wrong_length => "hat eine falsche Länge (es sollten %d Zeichen sein)", :taken => "ist schon vergeben", :not_a_number => "ist keine Zahl", }) end end end # date.rb require 'date' class Date MONTHNAMES = [nil, 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember' ] end # all.rb Dir[File.dirname(__FILE__) + "/**/*.rb"].each { |file| require(file) }
Then, at the end of config/environment.rb I have
require "#{RAILS_ROOT}/app/overrides/all"
This is definitely not pretty. The best I can say is that it appears to
work and keeps the intrusive code confined to one place. It would be much better, if all verbatim strings within Rails were piped through gettext or similar.
One could use the method shown above to load gettext and wrap _() around the original verbatim strings. If there are no localizations, _() simply returns its argument so everything remains as it is, but for those willing/needing to localize the strings, there would be a clean hook to do so.
—MichaelSchuerig
You also might wish to override the basic error messages used in scaffolded pages and error_for helper:
module ActionView #nodoc
module Helpers
module ActiveRecordHelper
def error_messages_for(object_name, options = {})
options = options.symbolize_keys
object = instance_variable_get("@#{object_name}")
unless object.errors.empty?
content_tag("div",
content_tag(
options[:header_tag] || "h2",
"#{pluralize(object.errors.count, "error")} prohibited this #{object_name.to_s.gsub("_", " ")} from being saved"
) +
content_tag("p", "There were problems with the following fields:") +
content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) }),
"id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
)
end
end
end
end
end
and put this modification in the same file.
ri Date reveals the other constants awaiting localization: DAYNAMES, ABBR_MONTHNAMES and ABBR_DAYNAMES.
—Charles M. Gerungan
Globalize plugin has all this constants translated in quite a few languages already.
—Benol
NOTE: If your’re running fastCGI, you’ll have to stop and restart your web server after modifying environment.rb.
How can I do if I want multiple languages (for exemple: Fr, de,en) ?
See also HowToLocalizeActiveRecordErrors
require 'date'
# English: [nil] + %w(January February March April May June July August September October November December)
Date.const_set "MONTHNAMES", [nil] + %w(janúar febrúar mars apríl maí júní ágúst september október nóvember desember)
# English: [nil] + %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
Date.const_set "ABBR_MONTHNAMES", [nil] + %w(jan feb mar apr maí jún júl ág sept okt nóv des)
# English: %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
Date.const_set "DAYNAMES", %w(sunnudagur mánudagur þriðjudagur miðvikudagur fimmtudagur föstudagur laugardagur)
# English: %w(Sun Mon Tue Wed Thu Fri Sat)
Date.const_set "ABBR_DAYNAMES", %w(sun mán þri miðv fim fös lau)
—terceiro
category: I18n