module RouteTranslator::Host
def lambdas
def lambdas @lambdas ||= {} end
def lambdas_for_locale(locale)
def lambdas_for_locale(locale) lambdas[locale] ||= ->(req) { locale == RouteTranslator::Host.locale_from_host(req.host) } end
def locale_from_host(host)
def locale_from_host(host) available_locales = I18n.available_locales RouteTranslator.config.host_locales.find do |pattern, locale| host&.match?(regex_for(pattern)) && available_locales.include?(locale&.to_sym) end&.last&.to_sym end
def regex_for(host_string)
def regex_for(host_string) escaped = Regexp.escape(host_string).gsub('\*', '.*?').gsub('\.', '\.?') Regexp.new("^#{escaped}$", Regexp::IGNORECASE) end