Skip to content

Commit

Permalink
Fix problem with reload not working correctly when auto is set for la…
Browse files Browse the repository at this point in the history
…nguage
  • Loading branch information
ishikawa999 committed Sep 11, 2024
1 parent f1761f3 commit 83fb4a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
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
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
# 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

0 comments on commit 83fb4a0

Please sign in to comment.