Skip to content

Commit 8ef5e3c

Browse files
committed
Add MessageCustomize::Hooks after_plugins_loaded #44
In Redmine 4.2 and later, override language with after_plugins_loaded hook instead of 35_change_load_order_locales.rb
1 parent 756ddf3 commit 8ef5e3c

File tree

7 files changed

+34
-9
lines changed

7 files changed

+34
-9
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ This plugin changes the translation of the wording on the screen defined in "con
88
```
99
$ cd /your/path/redmine
1010
$ git clone https://github.com/ishikawa999/redmine_message_customize.git plugins/redmine_message_customize
11+
$ # When Redmine 4.1 or lower versions
1112
$ cp plugins/redmine_message_customize/35_change_load_order_locales.rb config/initializers/35_change_load_order_locales.rb
1213
$ # redmine restart
1314
```
1415

15-
:warning: In order to customize messages of other plugins, it is necessary to copy redmine_message_customize/35_change_load_order_locales.rb into redmine/config/initializers.
16-
If you don't have redmine/config/initializers/35_change_load_order_locales.rb, you can customize only messages other than plugins.
16+
:warning To customize messages for other plugins in **Redmine 4.1 or lower versions**, redmine_message_customize/35_change_load_order_locales.rb It is necessary to copy the file to redmine/config/initializers.
17+
If redmine/config/initializers/35_change_load_order_locales.rb is missing, only non-plugin messages can be customized.
1718

1819
## Usage
1920

init.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# frozen_string_literal: true
2+
13
require File.expand_path('../lib/message_customize/locale', __FILE__)
4+
require File.expand_path('../lib/message_customize/hooks', __FILE__)
25

36
p = Redmine::Plugin.register :redmine_message_customize do
47
name 'Redmine message customize plugin'

lib/message_customize/hooks.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module MessageCustomize
4+
class Hooks < Redmine::Hook::ViewListener
5+
# The language file for redmine_message_customize should be given the highest priority because it overrides other plugin languages.
6+
# Set the language file to be loaded with the highest priority after all plugins have finished loading.
7+
def after_plugins_loaded()
8+
p = Redmine::Plugin.find(:redmine_message_customize)
9+
custom_locales = Dir.glob(File.join(p.directory, 'config', 'locales', 'custom_messages', '*.rb'))
10+
Rails.application.config.i18n.load_path = (Rails.application.config.i18n.load_path - custom_locales + custom_locales)
11+
end
12+
end
13+
end

lib/message_customize/locale.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# frozen_string_literal: true
2+
13
module MessageCustomize
24
module Locale
35
@available_messages = {}
46
CHANGE_LOAD_ORDER_LOCALES_FILE_PATH = 'config/initializers/35_change_load_order_locales.rb'
57

68
class << self
79
def available_locales
8-
@locales ||= I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
10+
@available_locales ||= I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
911
end
1012

1113
def reload!(*languages)
@@ -43,7 +45,7 @@ def available_messages(lang)
4345
end
4446

4547
def customizable_plugin_messages?
46-
@customizable_plugin_messages ||= File.exist?(Rails.root.join(CHANGE_LOAD_ORDER_LOCALES_FILE_PATH))
48+
Rails.application.config.i18n.load_path.last.include?('redmine_message_customize')
4749
end
4850
end
4951
end

test/functional/custom_message_settings_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CustomMessageSettingsControllerTest < defined?(Redmine::ControllerTest) ?
88
def setup
99
@request.session[:user_id] = 1 # admin
1010
MessageCustomize::Locale.reload!('en')
11-
I18n.load_path = (I18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
11+
Rails.application.config.i18n.load_path = (Rails.application.config.i18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
1212
end
1313

1414
# custom_message_settings/edit

test/unit/custom_message_setting_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CustomMessageSettingTest < ActiveSupport::TestCase
77
def setup
88
@custom_message_setting = CustomMessageSetting.find(1)
99
MessageCustomize::Locale.reload!('en')
10-
I18n.load_path = (I18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
10+
Rails.application.config.i18n.load_path = (Rails.application.config.i18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
1111
end
1212

1313
def test_validate_with_unused_keys_should_invalid

test/unit/lib/message_customize/locale_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class LocaleTest < ActiveSupport::TestCase
66

77
def setup
88
MessageCustomize::Locale.reload!('en')
9-
I18n.load_path = (I18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
9+
Rails.application.config.i18n.load_path = (Rails.application.config.i18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
1010
end
1111

1212
def test_reload!
@@ -67,7 +67,13 @@ def test_available_messages_should_return_messages_without_plugin_messages_if_no
6767
end
6868

6969
def test_customizable_plugin_messages?
70-
expect = File.exist?(Rails.root.join(MessageCustomize::Locale::CHANGE_LOAD_ORDER_LOCALES_FILE_PATH))
71-
assert_equal expect, MessageCustomize::Locale.customizable_plugin_messages?
70+
original_load_path = Rails.application.config.i18n.load_path
71+
Rails.application.config.i18n.load_path = (original_load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', 'ja.rb'))).uniq
72+
assert_equal true, MessageCustomize::Locale.customizable_plugin_messages?
73+
74+
Rails.application.config.i18n.load_path += [Rails.root.join('plugins', 'dummy', 'config', 'locales', 'en.yml').to_s]
75+
assert_equal false, MessageCustomize::Locale.customizable_plugin_messages?
76+
# cleaning
77+
Rails.application.config.i18n.load_path = original_load_path
7278
end
7379
end

0 commit comments

Comments
 (0)