Sha256: dda10c398c7aa66e75a4167eebc883700003a65c362a2d1cb3a6cf16c0aa18cc

Contents?: true

Size: 1.94 KB

Versions: 24

Compression:

Stored size: 1.94 KB

Contents

module Alchemy
  module I18n

    # Alchemy translation methods
    #
    # 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)
    #
    # It offers a shortcut method and view helper called _t
    #
    # === 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!
      humanize_default_string!(msg, options)
      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
      @@available_locales ||= nil
      @@available_locales || translation_files.collect { |f| f.match(/.{2}\.yml$/).to_s.gsub(/\.yml/, '').to_sym }
    end

    def self.available_locales=(locales)
      @@available_locales = Array(locales).map { |locale| locale.to_sym }
      @@available_locales = nil if @@available_locales.empty?
    end

    def self.translation_files
      Dir.glob(File.join(File.dirname(__FILE__), '../../config/locales/alchemy.*.yml'))
    end

  private

    def self.humanize_default_string!(msg, options)
      if options[:default].blank?
        options[:default] = msg.is_a?(Symbol) ? msg.to_s.humanize : msg
      end
    end

  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
alchemy_cms-3.2.1 lib/alchemy/i18n.rb
alchemy_cms-3.1.3 lib/alchemy/i18n.rb
lc_alchemy_cms-3.2.1 lib/alchemy/i18n.rb
lc_alchemy_cms-3.2.0 lib/alchemy/i18n.rb
alchemy_cms-3.2.0 lib/alchemy/i18n.rb
alchemy_cms-3.2.0.rc1 lib/alchemy/i18n.rb
alchemy_cms-3.2.0.beta lib/alchemy/i18n.rb
alchemy_cms-3.1.1 lib/alchemy/i18n.rb
alchemy_cms-3.0.4 lib/alchemy/i18n.rb
alchemy_cms-3.1.0 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.rc3 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.rc2 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.rc1 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.beta6 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.beta5 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.beta4 lib/alchemy/i18n.rb
alchemy_cms-3.0.3 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.beta3 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.beta2 lib/alchemy/i18n.rb
alchemy_cms-3.1.0.beta1 lib/alchemy/i18n.rb