# ClickToGlobalize module Globalize # :nodoc: class Locale # :nodoc: class << self alias_method :__translate, :translate def translate(key, default = nil, arg = nil, namespace = nil) # :nodoc: result = __translate(key, default, arg, namespace) notify_observers(key, result) result end # Returns the active Locale or create a new one, checking the choosen base language. # To easily plug-in this code I need always a ready Locale. def active @@active ||= Locale.set(@@base_language_code.locale) end # Check if the the class has a current active Locale, calling the homonymous method. # To easily plug-in this code I need always a ready Locale. def active? !active.nil? end def notify_observers(key, result) # :nodoc: observers.each { |observer| observer.update(key, result) } end def add_observer(observer) # :nodoc: observers << observer end def remove_observer(observer) # :nodoc: observers.delete(observer) end def observers # :nodoc: @observers ||= Set.new end def formatting # :nodoc: @formatting end # Sets the current formatting style. # # The options available are: # * textile (RedCloth gem) # * markdown (BlueCloth gem) def formatting=(formatting) @formatting = case formatting.to_sym when :textile then textile? ? :textile : nil when :markdown then markdown? ? :markdown : nil else raise ArgumentError end end # Returns the method for the current formatting style. # # The available methods are: # * textilize_without_paragraph (textile) # * markdown (markdown) def formatting_method case @formatting when :textile then :textilize_without_paragraph when :markdown then :markdown end end # Checks if the RedCloth gem is installed and already required. def textile? @textile ||= Object.const_defined?(:RedCloth) end # Checks if the BlueCloth gem is installed and already required. def markdown? @markdown ||= Object.const_defined?(:BlueCloth) end end end # Implements the Observer Pattern, when Locale#translate is called, # it notify LocaleObserver, passing the translation key and the result for # the current locale. class LocaleObserver attr_reader :translations def initialize # :nodoc: @translations = {} end def update(key, result) # :nodoc: @translations = @translations.merge({key, result}) end end module Helpers # :nodoc: @@partial = 'shared/_click_to_globalize' # Render +app/views/shared/_click_to_globalize.html.erb+. def click_to_globalize # Note: controller.class.globalize? is deprecated. return unless controller.globalize? && controller.class.globalize? render @@partial end # Get form_authenticity_token if the application is protected from forgery. # See ActionController::RequestForgeryProtection for details. def authenticity_token protect_against_forgery? ? form_authenticity_token : '' end # Returns the user defined languages in ApplicationController. def languages controller.class.languages end # Creates the HTML markup for the languages picker menu. # # Example: # # class ApplicationController < ActionController::Base # self.languages :english => 'en-US', :italian => 'it-IT' # end # # <%= languages_menu %> # # returns: #
def languages_menu returning result = '