Sha256: 7101bc4f6f022938aa05b7a89817047717ae0c0c48d2aad9df5a09aa8d29ba4f

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

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

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

          def locale=(new_locale)
            ::I18n.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 ||= ::I18n.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

          def reset!
            # Can't reset I18n. Needed?
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stringex-2.0.3 lib/stringex/localization/backend/i18n.rb
stringex-2.0.2 lib/stringex/localization/backend/i18n.rb
stringex-2.0.1 lib/stringex/localization/backend/i18n.rb
stringex-2.0.0 lib/stringex/localization/backend/i18n.rb