Sha256: bf9badc3a336ba66925053af65b59e953b168f39b8e456d1f125034c5472c4af

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

class LocaleJs::LocaleController < ApplicationController
  
  # remember to add the locales route
  # map.connect '/locale/:action.:format', :controller => :locale
  # then add this to your layout <%= javascript_include_tag  "/locale/#{I18n.locale}.js" %>
  # your locale will be available in a javascript global: I18n
  
  rescue_from AbstractController::ActionNotFound, :with => :action_mising
  
  # this method searches the locales dir and then defines methods on this class for each locale
  Dir.glob(File.join(Rails.root,'config/locales/*.yml')).map do |file|
    locale = File.basename(file).match(/(.+)\.yml/)[1]
    define_method(locale) do
      generate_locale(locale)
    end
  end
  
  private
  def generate_locale(locale)
    expires_in(30.days)
    I18n.locale = locale
    # initialize i18n
    I18n.translate :hello
    @locale = I18n.backend.send(:translations)[I18n.locale]
    render :partial => 'locale/i18n', :formats => [:js]
  end
  
  def action_missing(action)
    logger.debug("No config/locales/#{action}.yml exists! Reverting to #{I18n.default_locale}")
    generate_locale(I18n.default_locale)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locale_js-0.2.2 app/controllers/locale_js/locale_controller.rb