Sha256: b6d426241ac21b6b2554c7fe7616fb564d1349748ef5b81145f261c55fdf919f

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module Stringex
  module Localization
    module Backend
      class I18n < Base
        LOAD_PATH_BASE = File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', '..', '..', 'locales')

        class << self
          def locale
            @locale || ::I18n.locale
          end

          def locale=(new_locale)
            @locale = new_locale
          end

          def default_locale
            ::I18n.default_locale
          end

          def default_locale=(new_locale)
            ::I18n.default_locale = new_locale
          end

          def with_locale(new_locale, &block)
            ::I18n.with_locale new_locale, &block
          end

          def store_translations(locale, scope, data)
            ::I18n.backend.store_translations(locale, { :stringex => { scope => data } })
          end

          def initial_translation(scope, key, locale)
            # I18n can't return a nil as default as this gets interpreted as if no default
            # is specified, so we use a string instead.
            translated = ::I18n.translate(key, :scope => [:stringex, scope], :locale => locale, :default => "__default__")
            translated == "__default__" ? nil : translated
          end

          def load_translations(locale = nil)
            locale ||= self.locale
            path = Dir[File.join(LOAD_PATH_BASE, "#{locale}.yml")]
            ::I18n.load_path |= Dir[File.join(LOAD_PATH_BASE, "#{locale}.yml")]
            ::I18n.backend.load_translations
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stringex-2.0.4 lib/stringex/localization/backend/i18n.rb