Mon 2008-11-24 ( En pr )

Teaching Rails more languages is now as simple as:

  • downloading example locales,
  • putting them in your config/locales folder, and
  • adding a method to ApplicationController that detects the user’s language:
class ApplicationController < ActionController::Base
  before_filter :set_locale
  def set_locale
    I18n.locale = request.env.fetch('HTTP_ACCEPT_LANGUAGE', '')[/(?:,|^)(de|en)\b/, 1]
  end
end

You have to adjust the regexp to allow automatic recognition of more languages; I haven’t found out yet how to get a list of all available locales without using I18n.load_path, but maybe the community helps me out.

It’s cool that changes to the locale files are applied instantly in development mode! A great improvement :D

Say something! / Sag was!

No markup, just plain monospace text. / Kein Markup, nur Normschrift-Klartext.