Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions lib/message_customize/application_controller_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is still being used in the following locations.

params[:lang].presence || @setting.custom_messages.keys.first || current_user_language

As a result, when no message customization settings are registered, visiting the message customization settings screen (/custom_message_settings/edit) causes the following error:

NameError (undefined local variable or method `current_user_language' for an instance of CustomMessageSettingsController):
plugins/redmine_message_customize/app/controllers/custom_message_settings_controller.rb:57:in `set_lang'

How about fixing it like this?

diff --git a/lib/message_customize/application_controller_patch.rb b/lib/message_customize/application_controller_patch.rb
index 10b8abe..666210d 100644
--- a/lib/message_customize/application_controller_patch.rb
+++ b/lib/message_customize/application_controller_patch.rb
@@ -20,7 +20,7 @@ module MessageCustomize
       private
 
       def current_user_language
-        User.current.language.presence || Setting.default_language
+        I18n.locale
       end
     end
   end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hidakatsuya
Thanks for the feedback.

How about fixing it like this?
...

You're right, that makes sense. I'll go with current_user_language as it's clearer and easier to maintain.
f8d6844 fixes this problem.

MessageCustomize::Locale.reload!([I18n.locale])
end
end
end
Expand Down
22 changes: 21 additions & 1 deletion test/integration/application_controller_patch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

# 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
Expand Down