fix: Locale not correct in root url when accessing help center with custom domain (#9110)

- Ensuring that SwitchLocale concern handles the case of custom domain for portals and set locale according to that

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Shivam Mishra
2024-03-19 18:48:59 +05:30
committed by GitHub
parent a8b930e351
commit bd97226c95
6 changed files with 24 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
module DomainHelper
def self.chatwoot_domain?(domain = request.host)
[URI.parse(ENV.fetch('FRONTEND_URL', '')).host, URI.parse(ENV.fetch('HELPCENTER_URL', '')).host].include?(domain)
end
end

View File

@@ -6,6 +6,7 @@ module SwitchLocale
def switch_locale(&) def switch_locale(&)
# priority is for locale set in query string (mostly for widget/from js sdk) # priority is for locale set in query string (mostly for widget/from js sdk)
locale ||= locale_from_params locale ||= locale_from_params
locale ||= locale_from_custom_domain
# if locale is not set in account, let's use DEFAULT_LOCALE env variable # if locale is not set in account, let's use DEFAULT_LOCALE env variable
locale ||= locale_from_env_variable locale ||= locale_from_env_variable
set_locale(locale, &) set_locale(locale, &)
@@ -16,6 +17,20 @@ module SwitchLocale
set_locale(locale, &) set_locale(locale, &)
end end
# If the request is coming from a custom domain, it should be for a helpcenter portal
# We will use the portal locale in such cases
def locale_from_custom_domain(&)
return if params[:locale]
domain = request.host
return if DomainHelper.chatwoot_domain?(domain)
@portal = Portal.find_by(custom_domain: domain)
return unless @portal
@portal.default_locale
end
def set_locale(locale, &) def set_locale(locale, &)
# if locale is empty, use default_locale # if locale is empty, use default_locale
locale ||= I18n.default_locale locale ||= I18n.default_locale

View File

@@ -18,6 +18,7 @@ class DashboardController < ActionController::Base
'LOGO', 'LOGO_DARK', 'LOGO_THUMBNAIL', 'LOGO', 'LOGO_DARK', 'LOGO_THUMBNAIL',
'INSTALLATION_NAME', 'INSTALLATION_NAME',
'WIDGET_BRAND_URL', 'TERMS_URL', 'WIDGET_BRAND_URL', 'TERMS_URL',
'BRAND_URL', 'BRAND_NAME',
'PRIVACY_URL', 'PRIVACY_URL',
'DISPLAY_MANIFEST', 'DISPLAY_MANIFEST',
'CREATE_NEW_ACCOUNT_FROM_DASHBOARD', 'CREATE_NEW_ACCOUNT_FROM_DASHBOARD',

View File

@@ -47,7 +47,7 @@ class Public::Api::V1::Portals::BaseController < PublicController
@locale = if article.category.present? @locale = if article.category.present?
article.category.locale article.category.locale
else else
'en' article.portal.default_locale
end end
I18n.with_locale(@locale, &) I18n.with_locale(@locale, &)

View File

@@ -8,8 +8,7 @@ class PublicController < ActionController::Base
def ensure_custom_domain_request def ensure_custom_domain_request
domain = request.host domain = request.host
return if DomainHelper.chatwoot_domain?(domain)
return if [URI.parse(ENV.fetch('FRONTEND_URL', '')).host, URI.parse(ENV.fetch('HELPCENTER_URL', '')).host].include?(domain)
@portal = ::Portal.find_by(custom_domain: domain) @portal = ::Portal.find_by(custom_domain: domain)
return if @portal.present? return if @portal.present?

View File

@@ -83,7 +83,7 @@
class="w-24 overflow-hidden text-sm font-medium leading-tight bg-white appearance-none cursor-pointer dark:bg-slate-900 text-ellipsis whitespace-nowrap focus:outline-none focus:shadow-outline locale-switcher" class="w-24 overflow-hidden text-sm font-medium leading-tight bg-white appearance-none cursor-pointer dark:bg-slate-900 text-ellipsis whitespace-nowrap focus:outline-none focus:shadow-outline locale-switcher"
> >
<% @portal.config["allowed_locales"].each do |locale| %> <% @portal.config["allowed_locales"].each do |locale| %>
<option <%= locale == params[:locale] ? 'selected': '' %> value="<%= locale %>"><%= "#{language_name(locale)} (#{locale})" %></option> <option <%= locale == @locale ? 'selected': '' %> value="<%= locale %>"><%= "#{language_name(locale)} (#{locale})" %></option>
<% end %> <% end %>
</select> </select>
<%= render partial: 'icons/chevron-down' %> <%= render partial: 'icons/chevron-down' %>