From 83fb4a024bc7b36640adf71e9b1f26793a170839 Mon Sep 17 00:00:00 2001 From: ishikawa999 Date: Wed, 11 Sep 2024 08:13:16 +0000 Subject: [PATCH 1/2] Fix problem with reload not working correctly when auto is set for language --- .../application_controller_patch.rb | 11 +++------- .../application_controller_patch_test.rb | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/message_customize/application_controller_patch.rb b/lib/message_customize/application_controller_patch.rb index 10b8abe..1100574 100644 --- a/lib/message_customize/application_controller_patch.rb +++ b/lib/message_customize/application_controller_patch.rb @@ -12,15 +12,10 @@ def self.included(base) module InstanceMethod def reload_customize_messages custom_message_setting = CustomMessageSetting.find_or_default - return if custom_message_setting.latest_messages_applied?(current_user_language) + # NOTE: ApplicationController#set_localization sets the appropriate language in I18n.locale + return if custom_message_setting.latest_messages_applied?(I18n.locale) - MessageCustomize::Locale.reload!([current_user_language]) - end - - private - - def current_user_language - User.current.language.presence || Setting.default_language + MessageCustomize::Locale.reload!([I18n.locale]) end end end diff --git a/test/integration/application_controller_patch_test.rb b/test/integration/application_controller_patch_test.rb index 89622ad..08a0888 100644 --- a/test/integration/application_controller_patch_test.rb +++ b/test/integration/application_controller_patch_test.rb @@ -26,8 +26,28 @@ def test_reload_if_messages_are_not_latest assert_equal custom_message_setting.updated_on.to_i.to_s, I18n.backend.send(:translations)[:en][:redmine_message_customize_timestamp] end + def test_reload_if_user_language_is_auto_and_browser_language_messages_are_not_latest + # Reload based on the browser language if the language in User.current is ''(auto) + User.find_by_login('admin').update(language: '') + log_user('admin', 'admin') + custom_message_setting = CustomMessageSetting.find_or_default + + custom_message_setting.update_with_custom_messages({'label_home' => 'Changed home'}, 'ja') + assert_equal 'Home2', I18n.backend.send(:translations)[:ja][:label_home] + assert_equal '1640995200', I18n.backend.send(:translations)[:ja][:redmine_message_customize_timestamp] + with_settings :default_language => 'en' do + dummy_http_headers = @request.env + dummy_http_headers['HTTP_ACCEPT_LANGUAGE'] = 'ja' + ActionDispatch::Request.any_instance.stubs(:env).returns(dummy_http_headers) + + get '/issues' + end + assert_equal 'Changed home', I18n.backend.send(:translations)[:ja][:label_home] + assert_equal custom_message_setting.updated_on.to_i.to_s, I18n.backend.send(:translations)[:ja][:redmine_message_customize_timestamp] + end + def test_reload_if_user_language_is_auto_and_default_language_messages_are_not_latest - # User.currentのlanguageが''(auto)でもSetting.default_languageを元に用語の最新化を行うこと + # Reload based on the default language if the language in User.current is ''(auto) and request.env['HTTP_ACCEPT_LANGUAGE'] is nil User.find_by_login('admin').update(language: '') log_user('admin', 'admin') custom_message_setting = CustomMessageSetting.find_or_default From f8d6844440e607ace672cb36d7d8d5490e79ba3c Mon Sep 17 00:00:00 2001 From: ishikawa999 Date: Tue, 17 Sep 2024 06:32:53 +0000 Subject: [PATCH 2/2] Fix NameError (undefined local variable or method current_user_language) --- .../application_controller_patch.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/message_customize/application_controller_patch.rb b/lib/message_customize/application_controller_patch.rb index 1100574..003b0cf 100644 --- a/lib/message_customize/application_controller_patch.rb +++ b/lib/message_customize/application_controller_patch.rb @@ -12,10 +12,16 @@ def self.included(base) module InstanceMethod def reload_customize_messages custom_message_setting = CustomMessageSetting.find_or_default - # NOTE: ApplicationController#set_localization sets the appropriate language in I18n.locale - return if custom_message_setting.latest_messages_applied?(I18n.locale) + return if custom_message_setting.latest_messages_applied?(current_user_language) - MessageCustomize::Locale.reload!([I18n.locale]) + MessageCustomize::Locale.reload!([current_user_language]) + end + + private + + # NOTE: ApplicationController#set_localization sets the appropriate language in I18n.locale + def current_user_language + I18n.locale end end end