Skip to content

Commit

Permalink
Merge pull request #49 from ishikawa999/support-redmine5.0
Browse files Browse the repository at this point in the history
Support redmine5.0
  • Loading branch information
ishikawa999 authored Apr 12, 2022
2 parents 6cab7fa + 8141918 commit 1e7ed9b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ This plugin changes the translation of the wording on the screen defined in "con
```
$ cd /your/path/redmine
$ git clone https://github.com/ishikawa999/redmine_message_customize.git plugins/redmine_message_customize
$ # When Redmine 4.1 or lower versions
$ cp plugins/redmine_message_customize/35_change_load_order_locales.rb config/initializers/35_change_load_order_locales.rb
$ # redmine restart
```

: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.
If you don't have redmine/config/initializers/35_change_load_order_locales.rb, you can customize only messages other than plugins.
: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.
If redmine/config/initializers/35_change_load_order_locales.rb is missing, only non-plugin messages can be customized.

## Usage

Expand Down
3 changes: 3 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require File.expand_path('../lib/message_customize/locale', __FILE__)
require File.expand_path('../lib/message_customize/hooks', __FILE__)

p = Redmine::Plugin.register :redmine_message_customize do
name 'Redmine message customize plugin'
Expand Down
13 changes: 13 additions & 0 deletions lib/message_customize/hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module MessageCustomize
class Hooks < Redmine::Hook::ViewListener
# The language file for redmine_message_customize should be given the highest priority because it overrides other plugin languages.
# Set the language file to be loaded with the highest priority after all plugins have finished loading.
def after_plugins_loaded(_context)
p = Redmine::Plugin.find(:redmine_message_customize)
custom_locales = Dir.glob(File.join(p.directory, 'config', 'locales', 'custom_messages', '*.rb'))
Rails.application.config.i18n.load_path = (Rails.application.config.i18n.load_path - custom_locales + custom_locales)
end
end
end
12 changes: 8 additions & 4 deletions lib/message_customize/locale.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
# frozen_string_literal: true

module MessageCustomize
module Locale
@available_messages = {}
CHANGE_LOAD_ORDER_LOCALES_FILE_PATH = 'config/initializers/35_change_load_order_locales.rb'

class << self
def available_locales
@locales ||= I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
@available_locales ||= Rails.application.config.i18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
end

def reload!(*languages)
available_languages = self.find_language(languages.flatten)
paths = I18n.load_path.select {|path| available_languages.include?(File.basename(path, '.*').to_s)}
paths = Rails.application.config.i18n.load_path.select {|path| available_languages.include?(File.basename(path, '.*').to_s)}
I18n.backend.load_translations(paths)
if customizable_plugin_messages?
available_languages.each{|lang| @available_messages[:"#{lang}"] = I18n.backend.send(:translations)[:"#{lang}"] || {}}
else
available_languages.each do |lang|
redmine_root_locale_path = Rails.root.join('config', 'locales', "#{lang}.yml")
if File.exist?(redmine_root_locale_path)
@available_messages[:"#{lang}"] = (I18n.backend.send(:load_yml, redmine_root_locale_path)[lang] || {}).deep_symbolize_keys
loaded_yml = I18n.backend.send(:load_yml, redmine_root_locale_path)
loaded_yml = loaded_yml.first if loaded_yml.is_a?(Array)
@available_messages[:"#{lang}"] = (loaded_yml[lang] || loaded_yml[lang.to_sym] || {}).deep_symbolize_keys
end
end
end
Expand All @@ -41,7 +45,7 @@ def available_messages(lang)
end

def customizable_plugin_messages?
@customizable_plugin_messages ||= File.exist?(Rails.root.join(CHANGE_LOAD_ORDER_LOCALES_FILE_PATH))
Rails.application.config.i18n.load_path.last.include?('redmine_message_customize')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/functional/custom_message_settings_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CustomMessageSettingsControllerTest < defined?(Redmine::ControllerTest) ?
def setup
@request.session[:user_id] = 1 # admin
MessageCustomize::Locale.reload!('en')
I18n.load_path = (I18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
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
end

# custom_message_settings/edit
Expand Down
2 changes: 1 addition & 1 deletion test/unit/custom_message_setting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomMessageSettingTest < ActiveSupport::TestCase
def setup
@custom_message_setting = CustomMessageSetting.find(1)
MessageCustomize::Locale.reload!('en')
I18n.load_path = (I18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
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
end

def test_validate_with_unused_keys_should_invalid
Expand Down
12 changes: 9 additions & 3 deletions test/unit/lib/message_customize/locale_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LocaleTest < ActiveSupport::TestCase

def setup
MessageCustomize::Locale.reload!('en')
I18n.load_path = (I18n.load_path + Dir.glob(Rails.root.join('plugins', 'redmine_message_customize', 'config', 'locales', 'custom_messages', '*.rb'))).uniq
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
end

def test_reload!
Expand Down Expand Up @@ -67,7 +67,13 @@ def test_available_messages_should_return_messages_without_plugin_messages_if_no
end

def test_customizable_plugin_messages?
expect = File.exist?(Rails.root.join(MessageCustomize::Locale::CHANGE_LOAD_ORDER_LOCALES_FILE_PATH))
assert_equal expect, MessageCustomize::Locale.customizable_plugin_messages?
original_load_path = Rails.application.config.i18n.load_path
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
assert_equal true, MessageCustomize::Locale.customizable_plugin_messages?

Rails.application.config.i18n.load_path += [Rails.root.join('plugins', 'dummy', 'config', 'locales', 'en.yml').to_s]
assert_equal false, MessageCustomize::Locale.customizable_plugin_messages?
# cleaning
Rails.application.config.i18n.load_path = original_load_path
end
end

0 comments on commit 1e7ed9b

Please sign in to comment.