Sha256: 7a465336d401d6c442b36db991d3e8f8284970ab1849324f408a6d992aae04e2

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Stringex
  module Localization
    module Backend
      module Internal
        DEFAULT_LOCALE = :en

        class << self
          def locale
            @locale || default_locale
          end

          def locale=(new_locale)
            @locale = new_locale.to_sym
          end

          def default_locale
            @default_locale || DEFAULT_LOCALE
          end

          def default_locale=(new_locale)
            @default_locale = @locale = new_locale.to_sym
          end

          def with_locale(new_locale, &block)
            original_locale = locale
            self.locale = new_locale
            yield
            self.locale = original_locale
          end

          def translations
            # Set up hash like translations[:en][:transliterations]["é"]
            @translations ||= Hash.new { |k, v| k[v] = Hash.new({}) }
          end

          def store_translations(locale, scope, data)
            self.translations[locale.to_sym][scope.to_sym] = Hash[data.map { |k, v| [k.to_sym, v] }] # Symbolize keys
          end

          def initial_translation(scope, key, locale)
            translations[locale][scope][key.to_sym]
          end

          def reset!
            @translations = @locale = @default_locale = nil
          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/internal.rb
stringex-2.0.2 lib/stringex/localization/backend/internal.rb
stringex-2.0.1 lib/stringex/localization/backend/internal.rb
stringex-2.0.0 lib/stringex/localization/backend/internal.rb