Sha256: 5465b12df1714c370fb623c7b845cf06718c073975714840d455dc29a9128896

Contents?: true

Size: 1.73 KB

Versions: 31

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Shimmer
  module Localizable
    extend ActiveSupport::Concern

    included do
      before_action :set_locale

      def default_url_options(options = {})
        options = {locale: I18n.locale}.merge options
        options[:debug] = true if I18n.debug?
        options
      end

      def request_locale
        request.env["HTTP_ACCEPT_LANGUAGE"].to_s[0..1].downcase.presence&.then { |e| e if I18n.available_locales.include?(e.to_sym) }
      end

      def set_locale
        I18n.locale = url_locale || request_locale || I18n.default_locale
        I18n.debug = params.key?(:debug)
      end

      def check_locale
        redirect_to url_for(locale: I18n.locale) if params[:locale].blank? && request.get? && request.format.html?
      end

      def url_locale
        params[:locale]
      end

      I18n.class_eval do
        next if defined? debug

        thread_mattr_accessor :debug

        class << self
          alias_method :old_translate, :translate
          def translate(key, options = {})
            untranslated_scopes = ["number", "transliterate", "date", "datetime", "errors", "helpers", "support", "time", "faker"].map { |k| "#{k}." }
            key = key.to_s.downcase
            untranslated = untranslated_scopes.any? { |e| key.include? e }
            key_name = [options[:scope], key].flatten.compact.join(".")
            option_names = options.except(:count, :default, :raise, :scope).map { |k, v| "#{k}=#{v}" }.join(", ")
            return "#{key_name} #{option_names}" if I18n.debug && !untranslated

            old_translate(key, **options)
          end
          alias_method :t, :translate

          def debug?
            debug
          end
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
shimmer-0.0.41 lib/shimmer/utils/localizable.rb
shimmer-0.0.40 lib/shimmer/utils/localizable.rb
shimmer-0.0.39 lib/shimmer/utils/localizable.rb
shimmer-0.0.38 lib/shimmer/utils/localizable.rb
shimmer-0.0.37 lib/shimmer/utils/localizable.rb
shimmer-0.0.36 lib/shimmer/utils/localizable.rb
shimmer-0.0.35 lib/shimmer/utils/localizable.rb
shimmer-0.0.34 lib/shimmer/utils/localizable.rb
shimmer-0.0.33 lib/shimmer/utils/localizable.rb
shimmer-0.0.32 lib/shimmer/utils/localizable.rb
shimmer-0.0.31 lib/shimmer/utils/localizable.rb
shimmer-0.0.30 lib/shimmer/utils/localizable.rb
shimmer-0.0.29 lib/shimmer/utils/localizable.rb
shimmer-0.0.28 lib/shimmer/utils/localizable.rb
shimmer-0.0.27 lib/shimmer/utils/localizable.rb
shimmer-0.0.26 lib/shimmer/utils/localizable.rb
shimmer-0.0.25 lib/shimmer/utils/localizable.rb
shimmer-0.0.24 lib/shimmer/utils/localizable.rb
shimmer-0.0.23 lib/shimmer/utils/localizable.rb
shimmer-0.0.22 lib/shimmer/utils/localizable.rb