feat: Ability to set installation-wide default locale (#1433)
* Dashboard locale can be set via env variable * Change account locale based on registration page * Set account locale if available Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
parent
dffc888f9c
commit
30802e0c13
8 changed files with 59 additions and 26 deletions
|
@ -4,6 +4,10 @@ SECRET_KEY_BASE=replace_with_lengthy_secure_hex
|
||||||
# Replace with the URL you are planning to use for your app
|
# Replace with the URL you are planning to use for your app
|
||||||
FRONTEND_URL=http://0.0.0.0:3000
|
FRONTEND_URL=http://0.0.0.0:3000
|
||||||
|
|
||||||
|
# If the variable is set, all non-authenticated pages would fallback to the default locale.
|
||||||
|
# Whenever a new account is created, the default language will be DEFAULT_LOCALE instead of en
|
||||||
|
# DEFAULT_LOCALE=en
|
||||||
|
|
||||||
# If you plan to use CDN for your assets, set Asset CDN Host
|
# If you plan to use CDN for your assets, set Asset CDN Host
|
||||||
ASSET_CDN_HOST=
|
ASSET_CDN_HOST=
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ class AccountBuilder
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_account
|
def create_account
|
||||||
@account = Account.create!(name: @account_name)
|
@account = Account.create!(name: @account_name, locale: I18n.locale)
|
||||||
Current.account = @account
|
Current.account = @account
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
|
||||||
protect_from_forgery with: :null_session
|
protect_from_forgery with: :null_session
|
||||||
|
|
||||||
before_action :set_current_user, unless: :devise_controller?
|
before_action :set_current_user, unless: :devise_controller?
|
||||||
around_action :switch_locale
|
include SwitchLocale
|
||||||
around_action :handle_with_exception, unless: :devise_controller?
|
around_action :handle_with_exception, unless: :devise_controller?
|
||||||
|
|
||||||
# after_action :verify_authorized
|
# after_action :verify_authorized
|
||||||
|
@ -60,28 +60,6 @@ class ApplicationController < ActionController::Base
|
||||||
render json: exception.to_hash, status: exception.http_status
|
render json: exception.to_hash, status: exception.http_status
|
||||||
end
|
end
|
||||||
|
|
||||||
def locale_from_params
|
|
||||||
I18n.available_locales.map(&:to_s).include?(params[:locale]) ? params[:locale] : nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def locale_from_account(account)
|
|
||||||
return unless account
|
|
||||||
|
|
||||||
I18n.available_locales.map(&:to_s).include?(account.locale) ? account.locale : nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def switch_locale(&action)
|
|
||||||
# priority is for locale set in query string (mostly for widget/from js sdk)
|
|
||||||
locale ||= locale_from_params
|
|
||||||
# if local is not set in param, lets try account
|
|
||||||
locale ||= locale_from_account(@current_account)
|
|
||||||
# if nothing works we rely on default locale
|
|
||||||
locale ||= I18n.default_locale
|
|
||||||
# ensure locale won't bleed into other requests
|
|
||||||
# https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests
|
|
||||||
I18n.with_locale(locale, &action)
|
|
||||||
end
|
|
||||||
|
|
||||||
def pundit_user
|
def pundit_user
|
||||||
{
|
{
|
||||||
user: Current.user,
|
user: Current.user,
|
||||||
|
|
38
app/controllers/concerns/switch_locale.rb
Normal file
38
app/controllers/concerns/switch_locale.rb
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
module SwitchLocale
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
included do
|
||||||
|
around_action :switch_locale
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def switch_locale(&action)
|
||||||
|
# priority is for locale set in query string (mostly for widget/from js sdk)
|
||||||
|
locale ||= locale_from_params
|
||||||
|
# if locale is not set in param, lets try account
|
||||||
|
locale ||= locale_from_account(@current_account)
|
||||||
|
# if locale is not set in account, let's use DEFAULT_LOCALE env variable
|
||||||
|
locale ||= locale_from_env_variable
|
||||||
|
# if nothing works we rely on default locale
|
||||||
|
locale ||= I18n.default_locale
|
||||||
|
# ensure locale won't bleed into other requests
|
||||||
|
# https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests
|
||||||
|
I18n.with_locale(locale, &action)
|
||||||
|
end
|
||||||
|
|
||||||
|
def locale_from_params
|
||||||
|
I18n.available_locales.map(&:to_s).include?(params[:locale]) ? params[:locale] : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def locale_from_account(account)
|
||||||
|
return unless account
|
||||||
|
|
||||||
|
I18n.available_locales.map(&:to_s).include?(account.locale) ? account.locale : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def locale_from_env_variable
|
||||||
|
return unless ENV.fetch('DEFAULT_LOCALE', nil)
|
||||||
|
|
||||||
|
I18n.available_locales.map(&:to_s).include?(ENV.fetch('DEFAULT_LOCALE')) ? ENV.fetch('DEFAULT_LOCALE') : nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,5 +1,6 @@
|
||||||
class DashboardController < ActionController::Base
|
class DashboardController < ActionController::Base
|
||||||
before_action :set_global_config
|
before_action :set_global_config
|
||||||
|
include SwitchLocale
|
||||||
|
|
||||||
layout 'vueapp'
|
layout 'vueapp'
|
||||||
|
|
||||||
|
|
|
@ -28,10 +28,15 @@ export default {
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.$store.dispatch('setUser');
|
this.$store.dispatch('setUser');
|
||||||
|
this.setLocale(window.chatwootConfig.selectedLocale);
|
||||||
this.initializeAccount();
|
this.initializeAccount();
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
setLocale(locale) {
|
||||||
|
Vue.config.lang = locale;
|
||||||
|
},
|
||||||
|
|
||||||
async initializeAccount() {
|
async initializeAccount() {
|
||||||
const { pathname } = window.location;
|
const { pathname } = window.location;
|
||||||
const accountId = accountIdFromPathname(pathname);
|
const accountId = accountIdFromPathname(pathname);
|
||||||
|
@ -39,7 +44,7 @@ export default {
|
||||||
if (accountId) {
|
if (accountId) {
|
||||||
await this.$store.dispatch('accounts/get');
|
await this.$store.dispatch('accounts/get');
|
||||||
const { locale } = this.getAccount(accountId);
|
const { locale } = this.getAccount(accountId);
|
||||||
Vue.config.lang = locale;
|
this.setLocale(locale);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -35,7 +35,8 @@
|
||||||
<% if ENV['VAPID_PUBLIC_KEY'] %>
|
<% if ENV['VAPID_PUBLIC_KEY'] %>
|
||||||
vapidPublicKey: new Uint8Array(<%= Base64.urlsafe_decode64(ENV['VAPID_PUBLIC_KEY']).bytes %>),
|
vapidPublicKey: new Uint8Array(<%= Base64.urlsafe_decode64(ENV['VAPID_PUBLIC_KEY']).bytes %>),
|
||||||
<% end %>
|
<% end %>
|
||||||
enabledLanguages: <%= available_locales_with_name.to_json.html_safe %>
|
enabledLanguages: <%= available_locales_with_name.to_json.html_safe %>,
|
||||||
|
selectedLocale: '<%= I18n.locale %>'
|
||||||
}
|
}
|
||||||
window.globalConfig = <%= raw @global_config.to_json %>
|
window.globalConfig = <%= raw @global_config.to_json %>
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -93,6 +93,12 @@ Provide the following value as frontend url
|
||||||
FRONTEND_URL='http://localhost:3000'
|
FRONTEND_URL='http://localhost:3000'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Configure default language
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DEFAULT_LOCALE='en'
|
||||||
|
```
|
||||||
|
|
||||||
### Configure storage
|
### Configure storage
|
||||||
|
|
||||||
Chatwoot uses [active storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) for storing attachments. The default storage option is the local storage on your server.
|
Chatwoot uses [active storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) for storing attachments. The default storage option is the local storage on your server.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue