Sha256: a7606e5488e3eaa3d60684446d51ec77b337ec83d948524fcb539e2fcb4ea434

Contents?: true

Size: 1.13 KB

Versions: 24

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

# For example, the editor UI can be in English while the displayed Maglev content is in French.
# That's why we need another kind of I18n.locale only dedicated to Maglev content.
# Our Maglev::Translatable concern relies on this specific locale.
module Maglev
  module I18n
    class UnavailableLocaleError < RuntimeError; end

    class << self
      def available_locales
        Current.available_locales || %i[en]
      end

      def available_locales=(locales)
        Current.available_locales = locales.map(&:to_sym)
      end

      def default_locale
        available_locales.first
      end

      def current_locale
        Current.locale || default_locale
      end

      def current_locale=(locale)
        locale = locale.to_sym

        raise UnavailableLocaleError unless available_locales.include?(locale)

        Current.locale = locale
      end

      def with_locale(locale, &block)
        Current.set(locale: locale, &block)
      end
    end

    class Current < ActiveSupport::CurrentAttributes
      attribute :locale
      attribute :available_locales
    end
    private_constant :Current
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
maglevcms-1.7.3 lib/maglev/i18n.rb
maglevcms-1.7.2 lib/maglev/i18n.rb
maglevcms-1.7.1 lib/maglev/i18n.rb
maglevcms-1.7.0 lib/maglev/i18n.rb
maglevcms-1.6.1 lib/maglev/i18n.rb
maglevcms-1.6.0 lib/maglev/i18n.rb
maglevcms-1.5.1 lib/maglev/i18n.rb
maglevcms-1.4.0 lib/maglev/i18n.rb
maglevcms-1.3.0 lib/maglev/i18n.rb
maglevcms-1.2.2 lib/maglev/i18n.rb
maglevcms-1.2.1 lib/maglev/i18n.rb
maglevcms-1.2.0 lib/maglev/i18n.rb
maglevcms-1.1.7 lib/maglev/i18n.rb
maglevcms-1.1.6 lib/maglev/i18n.rb
maglevcms-1.1.5 lib/maglev/i18n.rb
maglevcms-1.1.4 lib/maglev/i18n.rb
maglevcms-1.1.3 lib/maglev/i18n.rb
maglevcms-1.1.2 lib/maglev/i18n.rb
maglevcms-1.1.1 lib/maglev/i18n.rb
maglevcms-1.1.0 lib/maglev/i18n.rb