Sha256: 0e258e683f22aec1d77a9f00c5da0116c08b9db882f5cdf7fdb7245b34bbe1db

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 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
    end
  end
end

module I18n
  UNTRANSLATED_SCOPES = ["number", "transliterate", "date", "datetime", "errors", "helpers", "support", "time", "faker"].map { |k| "#{k}." }

  thread_mattr_accessor :debug

  class << self
    alias_method :old_translate, :translate
    def translate(key, options = {})
      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

      options.reverse_merge!(default: old_translate(key, **options.merge(locale: :de))) if untranslated
      old_translate(key, **options)
    end
    alias_method :t, :translate

    def debug?
      debug
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shimmer-0.0.6 lib/shimmer/utils/localizable.rb
shimmer-0.0.5 lib/shimmer/utils/localizable.rb
shimmer-0.0.4 lib/shimmer/utils/localizable.rb