Sha256: acfc9489c1fe0b5fc0448df069a36731b10ff9fa5bf594bf75df4b95cdf101ec

Contents?: true

Size: 1.5 KB

Versions: 28

Compression:

Stored size: 1.5 KB

Contents

module Alchemy
  class I18n

    # A I18n Proxy for Alchemy translations
    #
    # Instead of having to translate strings and defining a default value:
    #
    #     Alchemy::I18n.t("Hello World!", :default => 'Hello World!')
    #
    # We define this method to define the value only once:
    #
    #     Alchemy::I18n.t("Hello World!")
    #
    # Note that interpolation still works:
    #
    #     Alchemy::I18n.t("Hello %{world}!", :world => @world)
    #
    # === Notes
    #
    # All translations are scoped into the +alchemy+ namespace.
    # Even scopes are scoped into the +alchemy+ namespace.
    #
    # So a call for t('hello', :scope => :world) has to be translated like this:
    #
    #   de:
    #     alchemy:
    #       world:
    #         hello: Hallo
    #
    def self.t(msg, *args)
      options = args.extract_options!
      options[:default] = options[:default] ? options[:default] : msg.to_s.humanize
      scope = ['alchemy']
      case options[:scope].class.name
      when "Array"
        scope += options[:scope]
      when "String"
        scope << options[:scope]
      when "Symbol"
        scope << options[:scope] unless options[:scope] == :alchemy
      end
      ::I18n.t(msg, options.merge(:scope => scope))
    end

    def self.available_locales
      translation_files.collect { |f| f.match(/.{2}\.yml$/).to_s.gsub(/\.yml/, '') }.uniq
    end

    def self.translation_files
      Rails.application.config.i18n.load_path.select { |p| p.split('/').last.match(/alchemy/) }
    end

  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
alchemy_cms-2.3.2 lib/alchemy/i18n.rb
alchemy_cms-2.2.4 lib/alchemy/i18n.rb
alchemy_cms-2.4.1 lib/alchemy/i18n.rb
alchemy_cms-2.5.0.b5 lib/alchemy/i18n.rb
alchemy_cms-2.5.0.b2 lib/alchemy/i18n.rb
alchemy_cms-2.4.0 lib/alchemy/i18n.rb
alchemy_cms-2.4.rc4 lib/alchemy/i18n.rb
alchemy_cms-2.4.rc2 lib/alchemy/i18n.rb
alchemy_cms-2.4.rc1 lib/alchemy/i18n.rb
alchemy_cms-2.3.1 lib/alchemy/i18n.rb
alchemy_cms-2.4.beta2 lib/alchemy/i18n.rb
alchemy_cms-2.2.3.2 lib/alchemy/i18n.rb
alchemy_cms-2.2.3.1 lib/alchemy/i18n.rb
alchemy_cms-2.3.0 lib/alchemy/i18n.rb
alchemy_cms-2.3.rc5 lib/alchemy/i18n.rb
alchemy_cms-2.2.2 lib/alchemy/i18n.rb
alchemy_cms-2.2.1 lib/alchemy/i18n.rb
alchemy_cms-2.2.0 lib/alchemy/i18n.rb
alchemy_cms-2.2.rc15 lib/alchemy/i18n.rb
alchemy_cms-2.2.rc14 lib/alchemy/i18n.rb