Files
chatwoot/spec/models/concerns/switch_locale_spec.rb
Pranav a513f152ed fix: Extend the locale without variant check for article locales as well (#11021)
We allow users to select locale variants when creating the help center
(e.g., pt_BR or en_UK). However, the selected variant may not always be
available for translation in the app. In such cases, we need to fall
back to either the base language or the default locale.

While this fallback logic was implemented for the portal locale, it was
missing for article locales.

This PR fixes that issue.
2025-03-06 18:24:46 -08:00

75 lines
2.0 KiB
Ruby

require 'rails_helper'
RSpec.describe 'SwitchLocale Concern', type: :controller do
controller(ApplicationController) do
include SwitchLocale
def index
switch_locale { render plain: I18n.locale }
end
def account_locale
switch_locale_using_account_locale { render plain: I18n.locale }
end
end
let(:account) { create(:account, locale: 'es') }
let(:portal) { create(:portal, custom_domain: 'custom.example.com', config: { default_locale: 'fr_FR' }) }
describe '#switch_locale' do
context 'when locale is provided in params' do
it 'sets locale from params' do
get :index, params: { locale: 'es' }
expect(response.body).to eq('es')
end
it 'falls back to default locale if invalid' do
get :index, params: { locale: 'invalid' }
expect(response.body).to eq('en')
end
end
context 'when request is from custom domain' do
before { request.host = portal.custom_domain }
it 'sets locale from portal' do
get :index
expect(response.body).to eq('fr')
end
it 'overrides portal locale with param' do
get :index, params: { locale: 'es' }
expect(response.body).to eq('es')
end
end
context 'when locale is not provided anywhere' do
it 'sets locale from environment variable' do
with_modified_env(DEFAULT_LOCALE: 'de_DE') do
get :index
expect(response.body).to eq('de')
end
end
it 'falls back to default locale if env locale invalid' do
with_modified_env(DEFAULT_LOCALE: 'invalid') do
get :index
expect(response.body).to eq('en')
end
end
end
end
describe '#switch_locale_using_account_locale' do
before do
routes.draw { get 'account_locale' => 'anonymous#account_locale' }
end
it 'sets locale from account' do
controller.instance_variable_set(:@current_account, account)
get :account_locale
expect(response.body).to eq('es')
end
end
end