mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
# Pull Request Template ## Description This Pull Request will provide a language selector in the Profile Settings for each user, and allows them to change the UI language per agent, defaulting back to the account locale. Fixes # #678 This does PR addresses the Dashboard view but does not change the language of the agents emails ## Type of change Please delete options that are not relevant. - [X ] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? 1. Go to an Agents Profile settings page 2. Select a language from the Language drop down 3. the UI will update to the new i18n locale 4. navigate through the UI to make sure the appropriate language is being used 5. Refresh the page to test that the locale persists 270 - [X] My code follows the style guidelines of this project - [X] I have performed a self-review of my code - [X] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [X] My changes generate no new warnings - [X] I have added tests that prove my fix is effective or that my feature works - [X] New and existing unit tests pass locally with my changes - [X] Any dependent changes have been merged and published in downstream modules Checklist:.724.2708 --------- Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Pranav <pranav@chatwoot.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
80 lines
2.0 KiB
Ruby
80 lines
2.0 KiB
Ruby
module SwitchLocale
|
|
extend ActiveSupport::Concern
|
|
|
|
private
|
|
|
|
def switch_locale(&)
|
|
# Priority is for locale set in query string (mostly for widget/from js sdk)
|
|
locale ||= params[:locale]
|
|
|
|
# Use the user's locale if available
|
|
locale ||= locale_from_user
|
|
|
|
# Use the locale from a custom domain if applicable
|
|
locale ||= locale_from_custom_domain
|
|
|
|
# if locale is not set in account, let's use DEFAULT_LOCALE env variable
|
|
locale ||= ENV.fetch('DEFAULT_LOCALE', nil)
|
|
|
|
set_locale(locale, &)
|
|
end
|
|
|
|
def switch_locale_using_account_locale(&)
|
|
# Get the locale from the user first
|
|
locale = locale_from_user
|
|
|
|
# Fallback to the account's locale if the user's locale is not set
|
|
locale ||= locale_from_account(@current_account)
|
|
|
|
set_locale(locale, &)
|
|
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 locale_from_user
|
|
return unless @user
|
|
|
|
@user.ui_settings&.dig('locale')
|
|
end
|
|
|
|
def set_locale(locale, &)
|
|
safe_locale = validate_and_get_locale(locale)
|
|
# Ensure locale won't bleed into other requests
|
|
# https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests
|
|
I18n.with_locale(safe_locale, &)
|
|
end
|
|
|
|
def validate_and_get_locale(locale)
|
|
return I18n.default_locale.to_s if locale.blank?
|
|
|
|
available_locales = I18n.available_locales.map(&:to_s)
|
|
locale_without_variant = locale.split('_')[0]
|
|
|
|
if available_locales.include?(locale)
|
|
locale
|
|
elsif available_locales.include?(locale_without_variant)
|
|
locale_without_variant
|
|
else
|
|
I18n.default_locale.to_s
|
|
end
|
|
end
|
|
|
|
def locale_from_account(account)
|
|
return unless account
|
|
|
|
account.locale
|
|
end
|
|
end
|