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

Fix problem with reload not working correctly when auto is set for language #68

Merged
merged 2 commits into from
Sep 17, 2024

Conversation

ishikawa999
Copy link
Collaborator

Issue:
There is a problem where, if a user's language setting is set to auto and the browser's language differs from the default language, the customized settings are not loaded correctly.

Reproduction steps:

  • Set the user's language preference to auto.
  • Ensure that the browser's language is different from the redmine setting's default language.
  • Customize the browser language message.
  • Observe that the user's customized settings do not load as expected.

Fix:
Use I18n.locale instead of User.current.language.presence || Setting.default_language.
I18n.locale has terms set with appropriate priority by ApplicationController#set_localication and others. https://github.com/redmica/redmica/blob/master/app/controllers/application_controller.rb#L247-L261

Comment on lines 15 to 23
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
Contributor

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.

Copy link
Contributor

@hidakatsuya hidakatsuya left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -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
Contributor

Choose a reason for hiding this comment

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

👍🏼

@ishikawa999 ishikawa999 merged commit 17faa39 into master Sep 17, 2024
6 checks passed
@ishikawa999 ishikawa999 deleted the support-auto-language branch September 17, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants