-
Hi Janko, I am trying this new gem of yours to try to replace Devise in an existing app. It is much cleaner and easy to understand (and extend)! I need to have per-locale sign in and sign up routes, with Devise I used something like this: # routes.rb
all_locales_regexp = Regexp.union(
I18n.available_locales.map(&:to_s) - [I18n.default_locale.to_s],
)
scope "(:locale)", locale: all_locales_regexp do
devise_for :users
get "/about", to: "pages#about", as: :about
get "/legal", to: "pages#legal", as: :legal
end
# application_controller.rb
before_action :set_locale
def set_locale
browser_preferred_locale = http_accept_language.compatible_language_from(I18n.available_locales)
I18n.locale = \
params[:loc] ||
params[:locale] ||
current_user.try(:locale) ||
browser_preferred_locale ||
I18n.default_locale
end
end It allows me to have As Rodauth routes are handled separately from standard Rails routes I can not use this technique and I am not sure on how to do this properly. Do you have any idea on how I can replicate this behaviour without duplicating too many things? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi Renault, nice to see you again 👋🏻 I believe you should be able set up locale path prefixes as follows: class RodauthApp < Rodauth::Rails::App
configure do
# ...
prefix { "/#{I18n.locale}/users" }
# ...
end
route do |r|
# ...
all_locales = I18n.available_locales.map(&:to_s) - [I18n.default_locale.to_s]
r.on [*all_locales, true], "users" do |locale|
rails_request = ActionDispatch::Request.new(env)
rails_request.params[:locale] = locale if locale
r.rodauth
r.pass
end
# ...
end
end This uses Roda's routing to match an optional locale prefix, using an array matcher, in combination with true matcher to achieve an optional segment. The Let me know if this works for you 🙂 |
Beta Was this translation helpful? Give feedback.
-
In case somebody needs it later, I ended up implementing the following: class RodauthApp < Rodauth::Rails::App
configure do
prefix do
if I18n.locale == I18n.default_locale
"/auth"
else
"/#{I18n.locale}/auth"
end
end
end
route do |r|
rodauth.check_active_session
all_locales = I18n.available_locales.map(&:to_s) - [I18n.default_locale.to_s]
r.on [*all_locales, true], "auth" do |locale|
rails_request = ActionDispatch::Request.new(env)
rails_request.params[:locale] = locale || I18n.default_locale
r.rodauth
r.pass
end
end And as a cross-over with my other discussion, I created a module with URL helpers taking the locale into account module RodauthUrlHelpers
%i[login reset_password reset_password_request create_account].each do |method|
%i[path url].each do |suffix|
define_method "user_#{method}_#{suffix}" do |*args, **options|
I18n.with_locale(options[:locale]) do
Rodauth::Rails.rodauth.send("#{method}_#{suffix}", *args, **options.except(:locale))
end
end
end
end
end I am including it in my test helper as well as in my class ApplicationController < ActionController::Base
include RodauthUrlHelpers
helper_method RodauthUrlHelpers.public_instance_methods
end |
Beta Was this translation helpful? Give feedback.
Hi Renault, nice to see you again 👋🏻
I believe you should be able set up locale path prefixes as follows:
This uses Roda's routing to match an optional locale prefix, using an array matcher, in combination with true matcher to achieve an optional seg…