From 73bc4647429cc0374a1bd2fd1b3400a9a8884534 Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 7 Oct 2020 12:12:28 +0700 Subject: [PATCH 1/8] Improve the localization concern, moving to around_action --- .../app/controllers/concerns/localization.rb | 18 +++++++++++++++--- .../api/config/environments/production.rb | 13 +++++++++++++ .template/variants/api/config/template.rb | 1 + .template/variants/api/template.rb | 2 ++ .../app/controllers/concerns/localization.rb | 12 +++++++++--- .../web/config/environments/production.rb | 13 +++++++++++++ .template/variants/web/config/template.rb | 1 + config/application.yml.tt | 3 +++ 8 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 .template/variants/api/config/environments/production.rb create mode 100644 .template/variants/api/config/template.rb create mode 100644 .template/variants/web/config/environments/production.rb diff --git a/.template/variants/api/app/controllers/concerns/localization.rb b/.template/variants/api/app/controllers/concerns/localization.rb index 96138ba5..586e10d6 100644 --- a/.template/variants/api/app/controllers/concerns/localization.rb +++ b/.template/variants/api/app/controllers/concerns/localization.rb @@ -2,12 +2,24 @@ 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_param || I18n.default_locale + + I18n.with_locale(locale, &action) + end + + def extract_locale_from_param + I18n.locale_available?(params[:locale]) ? params[:locale].to_sym : nil + end + + private + + def default_url_options + { locale: I18n.locale == I18n.default_locale ? nil : I18n.locale } end end end diff --git a/.template/variants/api/config/environments/production.rb b/.template/variants/api/config/environments/production.rb new file mode 100644 index 00000000..e8226602 --- /dev/null +++ b/.template/variants/api/config/environments/production.rb @@ -0,0 +1,13 @@ +# 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 + config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') + config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE') + config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') + + EOT +end diff --git a/.template/variants/api/config/template.rb b/.template/variants/api/config/template.rb new file mode 100644 index 00000000..00e222d3 --- /dev/null +++ b/.template/variants/api/config/template.rb @@ -0,0 +1 @@ +apply 'config/environments/production.rb' diff --git a/.template/variants/api/template.rb b/.template/variants/api/template.rb index 2b34cfe8..e65ac331 100644 --- a/.template/variants/api/template.rb +++ b/.template/variants/api/template.rb @@ -1,3 +1,5 @@ use_source_path __dir__ apply 'app/template.rb' + +apply 'config/environments/production.rb' diff --git a/.template/variants/web/app/controllers/concerns/localization.rb b/.template/variants/web/app/controllers/concerns/localization.rb index 45abb77d..586e10d6 100644 --- a/.template/variants/web/app/controllers/concerns/localization.rb +++ b/.template/variants/web/app/controllers/concerns/localization.rb @@ -2,12 +2,18 @@ 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 + I18n.locale_available?(params[:locale]) ? params[:locale].to_sym : nil end private diff --git a/.template/variants/web/config/environments/production.rb b/.template/variants/web/config/environments/production.rb new file mode 100644 index 00000000..e8226602 --- /dev/null +++ b/.template/variants/web/config/environments/production.rb @@ -0,0 +1,13 @@ +# 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 + config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') + config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE') + config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') + + EOT +end diff --git a/.template/variants/web/config/template.rb b/.template/variants/web/config/template.rb index af686cbd..8504069a 100644 --- a/.template/variants/web/config/template.rb +++ b/.template/variants/web/config/template.rb @@ -3,3 +3,4 @@ copy_file 'config/i18n-js.yml' apply 'config/environments/test.rb' +apply 'config/environments/production.rb' diff --git a/config/application.yml.tt b/config/application.yml.tt index 516eabe1..e0153b04 100644 --- a/config/application.yml.tt +++ b/config/application.yml.tt @@ -6,6 +6,9 @@ default: &default MAILER_DEFAULT_HOST: "localhost" MAILER_DEFAULT_PORT: "3000" MAILER_SENDER: "Test " + AVAILABLE_LOCALES: "en" + DEFAULT_LOCALE: "en" + FALLBACK_LOCALES: "en" development: <<: *default From df20b777c9db44f8766a3058690297d44b0f41d5 Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 7 Oct 2020 14:21:20 +0700 Subject: [PATCH 2/8] Adding unit test --- .../controllers/concerns/localization_spec.rb | 7 +++++++ .../base/config/environments/production_spec.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 .template/spec/base/app/controllers/concerns/localization_spec.rb diff --git a/.template/spec/base/app/controllers/concerns/localization_spec.rb b/.template/spec/base/app/controllers/concerns/localization_spec.rb new file mode 100644 index 00000000..8c7b1254 --- /dev/null +++ b/.template/spec/base/app/controllers/concerns/localization_spec.rb @@ -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 diff --git a/.template/spec/base/config/environments/production_spec.rb b/.template/spec/base/config/environments/production_spec.rb index 7fee9da5..094048ef 100644 --- a/.template/spec/base/config/environments/production_spec.rb +++ b/.template/spec/base/config/environments/production_spec.rb @@ -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') @@ -23,4 +31,12 @@ def mailer_default_url_config } EOT end + + def i18n_config + <<~EOT + config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') + config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE') + config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') + EOT + end end From c4956d3c5508f2155aa4c5c6fa2f458448fb3a29 Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 7 Oct 2020 14:41:08 +0700 Subject: [PATCH 3/8] Minor refactor --- .../variants/api/config/environments/production.rb | 13 ------------- .template/variants/api/config/template.rb | 1 - .../variants/web/config/environments/production.rb | 13 ------------- .template/variants/web/config/template.rb | 1 - config/environments/production.rb | 14 ++++++++++++++ 5 files changed, 14 insertions(+), 28 deletions(-) delete mode 100644 .template/variants/api/config/environments/production.rb delete mode 100644 .template/variants/api/config/template.rb delete mode 100644 .template/variants/web/config/environments/production.rb diff --git a/.template/variants/api/config/environments/production.rb b/.template/variants/api/config/environments/production.rb deleted file mode 100644 index e8226602..00000000 --- a/.template/variants/api/config/environments/production.rb +++ /dev/null @@ -1,13 +0,0 @@ -# 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 - config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') - config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE') - config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') - - EOT -end diff --git a/.template/variants/api/config/template.rb b/.template/variants/api/config/template.rb deleted file mode 100644 index 00e222d3..00000000 --- a/.template/variants/api/config/template.rb +++ /dev/null @@ -1 +0,0 @@ -apply 'config/environments/production.rb' diff --git a/.template/variants/web/config/environments/production.rb b/.template/variants/web/config/environments/production.rb deleted file mode 100644 index e8226602..00000000 --- a/.template/variants/web/config/environments/production.rb +++ /dev/null @@ -1,13 +0,0 @@ -# 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 - config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') - config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE') - config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') - - EOT -end diff --git a/.template/variants/web/config/template.rb b/.template/variants/web/config/template.rb index 8504069a..af686cbd 100644 --- a/.template/variants/web/config/template.rb +++ b/.template/variants/web/config/template.rb @@ -3,4 +3,3 @@ copy_file 'config/i18n-js.yml' apply 'config/environments/test.rb' -apply 'config/environments/production.rb' diff --git a/config/environments/production.rb b/config/environments/production.rb index 1f281dd1..d2415baf 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -12,3 +12,17 @@ } 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 + config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') + config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE') + config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') + + EOT +end From 8b1429e2a2675590fa437775302e2a2e81b24959 Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 7 Oct 2020 15:59:23 +0700 Subject: [PATCH 4/8] Minor refactor --- .template/variants/api/template.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/.template/variants/api/template.rb b/.template/variants/api/template.rb index e65ac331..2b34cfe8 100644 --- a/.template/variants/api/template.rb +++ b/.template/variants/api/template.rb @@ -1,5 +1,3 @@ use_source_path __dir__ apply 'app/template.rb' - -apply 'config/environments/production.rb' From a3355a14d305b15b9af0fe66f3174392da2e0fc1 Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 14 Oct 2020 10:23:04 +0700 Subject: [PATCH 5/8] Adding comment to configuration --- .../spec/base/config/environments/production_spec.rb | 9 +++++++-- config/environments/production.rb | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.template/spec/base/config/environments/production_spec.rb b/.template/spec/base/config/environments/production_spec.rb index 094048ef..9a1d71a0 100644 --- a/.template/spec/base/config/environments/production_spec.rb +++ b/.template/spec/base/config/environments/production_spec.rb @@ -34,9 +34,14 @@ def mailer_default_url_config def i18n_config <<~EOT - config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') + # 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') - config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') + + # eg: FALLBACK_LOCALES = 'en,th' + config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(',') EOT end end diff --git a/config/environments/production.rb b/config/environments/production.rb index d2415baf..92555b19 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -20,9 +20,14 @@ # Adding the correct fallback configuration along with default locale and available locales environment do <<~EOT - config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ') + # 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') - config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ') + + # eg: FALLBACK_LOCALES = 'en,th' + config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(',') EOT end From 0e06c497f7d26bb382450f349771b6564ba93356 Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 14 Oct 2020 17:49:21 +0700 Subject: [PATCH 6/8] Using return first --- .../variants/api/app/controllers/concerns/localization.rb | 4 +++- .../variants/web/app/controllers/concerns/localization.rb | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.template/variants/api/app/controllers/concerns/localization.rb b/.template/variants/api/app/controllers/concerns/localization.rb index 586e10d6..4787a245 100644 --- a/.template/variants/api/app/controllers/concerns/localization.rb +++ b/.template/variants/api/app/controllers/concerns/localization.rb @@ -13,7 +13,9 @@ def switch_locale(&action) end def extract_locale_from_param - I18n.locale_available?(params[:locale]) ? params[:locale].to_sym : nil + return params[:locale] if I18n.locale_available?(params[:locale]) + + nil end private diff --git a/.template/variants/web/app/controllers/concerns/localization.rb b/.template/variants/web/app/controllers/concerns/localization.rb index 586e10d6..4787a245 100644 --- a/.template/variants/web/app/controllers/concerns/localization.rb +++ b/.template/variants/web/app/controllers/concerns/localization.rb @@ -13,7 +13,9 @@ def switch_locale(&action) end def extract_locale_from_param - I18n.locale_available?(params[:locale]) ? params[:locale].to_sym : nil + return params[:locale] if I18n.locale_available?(params[:locale]) + + nil end private From 1cb5bc7b734584b070666284eae456d00584991a Mon Sep 17 00:00:00 2001 From: An Duong Date: Wed, 14 Oct 2020 17:52:52 +0700 Subject: [PATCH 7/8] Using request.headers[:'Accept-Language'] --- .../variants/api/app/controllers/concerns/localization.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.template/variants/api/app/controllers/concerns/localization.rb b/.template/variants/api/app/controllers/concerns/localization.rb index 4787a245..acdd0562 100644 --- a/.template/variants/api/app/controllers/concerns/localization.rb +++ b/.template/variants/api/app/controllers/concerns/localization.rb @@ -7,13 +7,13 @@ module Localization protected def switch_locale(&action) - locale = extract_locale_from_param || I18n.default_locale + locale = extract_locale_from_header || I18n.default_locale I18n.with_locale(locale, &action) end - def extract_locale_from_param - return params[:locale] if I18n.locale_available?(params[:locale]) + def extract_locale_from_header + return request.headers[:'Accept-Language'] if I18n.locale_available?(request.headers[:'Accept-Language']) nil end From 39ea1f87d09352c5bc8e5f1bda9b50ce16a6f3c0 Mon Sep 17 00:00:00 2001 From: An Duong Date: Thu, 15 Oct 2020 11:16:07 +0700 Subject: [PATCH 8/8] Remove unnecessary code --- .../variants/api/app/controllers/concerns/localization.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.template/variants/api/app/controllers/concerns/localization.rb b/.template/variants/api/app/controllers/concerns/localization.rb index acdd0562..4dc726a9 100644 --- a/.template/variants/api/app/controllers/concerns/localization.rb +++ b/.template/variants/api/app/controllers/concerns/localization.rb @@ -17,11 +17,5 @@ def extract_locale_from_header nil end - - private - - def default_url_options - { locale: I18n.locale == I18n.default_locale ? nil : I18n.locale } - end end end