Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the localization concern, moving to around_action #241

Merged
merged 8 commits into from Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,7 @@
describe 'localization concern' do
subject { file('app/controllers/concerns/localization.rb') }

it 'contains the around_action' do
expect(subject).to contain('around_action :switch_locale')
end
end
21 changes: 21 additions & 0 deletions .template/spec/base/config/environments/production_spec.rb
Expand Up @@ -8,6 +8,14 @@
it 'configures the mailer default url options' do
expect(subject).to contain(mailer_default_url_config)
end

it 'adds the i18n configuration' do
expect(subject).to contain(i18n_config)
end

it 'removes the config.i18n.fallbacks = true' do
expect(subject).not_to contain("config.i18n.fallbacks = true")
end

it 'allows Rails serve static file' do
expect(subject).to contain('config.public_file_server.enabled = true')
Expand All @@ -23,4 +31,17 @@ def mailer_default_url_config
}
EOT
end

def i18n_config
<<~EOT
# eg: AVAILABLE_LOCALES = 'en,th'
config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(',')

# eg: DEFAULT_LOCALE = 'en'
config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE')

# eg: FALLBACK_LOCALES = 'en,th'
config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(',')
EOT
end
end
14 changes: 11 additions & 3 deletions .template/variants/api/app/controllers/concerns/localization.rb
Expand Up @@ -2,12 +2,20 @@ module Localization
extend ActiveSupport::Concern

included do
before_action :set_locale
around_action :switch_locale

protected

def set_locale
I18n.locale = request.headers[:'Accept-Language'] || I18n.default_locale
def switch_locale(&action)
locale = extract_locale_from_header || I18n.default_locale

I18n.with_locale(locale, &action)
end

def extract_locale_from_header
return request.headers[:'Accept-Language'] if I18n.locale_available?(request.headers[:'Accept-Language'])

nil
end
end
end
14 changes: 11 additions & 3 deletions .template/variants/web/app/controllers/concerns/localization.rb
Expand Up @@ -2,12 +2,20 @@ module Localization
extend ActiveSupport::Concern

included do
before_action :set_locale
around_action :switch_locale

protected

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
def switch_locale(&action)
locale = extract_locale_from_param || I18n.default_locale

I18n.with_locale(locale, &action)
end

def extract_locale_from_param
return params[:locale] if I18n.locale_available?(params[:locale])

nil
end

private
Expand Down
3 changes: 3 additions & 0 deletions config/application.yml.tt
Expand Up @@ -6,6 +6,9 @@ default: &default
MAILER_DEFAULT_HOST: "localhost"
MAILER_DEFAULT_PORT: "3000"
MAILER_SENDER: "Test <noreply@nimblehq.co>"
AVAILABLE_LOCALES: "en"
byhbt marked this conversation as resolved.
Show resolved Hide resolved
DEFAULT_LOCALE: "en"
FALLBACK_LOCALES: "en"

development:
<<: *default
Expand Down
19 changes: 19 additions & 0 deletions config/environments/production.rb
Expand Up @@ -12,3 +12,22 @@
}
EOT
end

# Remove the incorrect fallback configuration (was generated by Rails)
# https://github.com/ruby-i18n/i18n/releases/tag/v1.1.0
gsub_file('config/environments/production.rb', 'config.i18n.fallbacks = true', '')

# Adding the correct fallback configuration along with default locale and available locales
environment do
<<~EOT
# eg: AVAILABLE_LOCALES = 'en,th'
config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(',')

# eg: DEFAULT_LOCALE = 'en'
config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE')

# eg: FALLBACK_LOCALES = 'en,th'
config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(',')

EOT
end