1
+ class CustomMessageSetting < Setting
2
+
3
+ before_validation :change_format_to_hash
4
+ validate :custom_message_keys_are_available
5
+
6
+ def self . find_or_default
7
+ super ( 'plugin_redmine_message_customize' )
8
+ end
9
+
10
+ def custom_messages ( lang = nil )
11
+ if lang . present?
12
+ self . value . dig ( :custom_messages , self . class . find_language ( lang ) ) || { }
13
+ else
14
+ self . value [ :custom_messages ] || { }
15
+ end
16
+ end
17
+
18
+ def custom_messages_to_yaml
19
+ if self . custom_messages . blank?
20
+ ''
21
+ elsif self . custom_messages . is_a? ( Hash )
22
+ YAML . dump ( self . custom_messages )
23
+ else
24
+ self . custom_messages
25
+ end
26
+ end
27
+
28
+ def update_custom_messages ( messages )
29
+ self . value = { custom_messages : ( messages . present? ? messages : { } ) }
30
+ self . save
31
+ end
32
+
33
+ def self . available_messages ( lang = 'en' )
34
+ list = I18n . backend . translations [ self . find_language ( lang ) . to_sym ] || { }
35
+ self . flatten_hash ( list )
36
+ end
37
+
38
+ def self . flatten_hash ( hash = nil )
39
+ hash = self . to_hash unless hash
40
+ hash . each_with_object ( { } ) do |( key , value ) , content |
41
+ next self . flatten_hash ( value ) . each do |k , v |
42
+ content [ "#{ key } .#{ k } " . intern ] = v
43
+ end if value . is_a? Hash
44
+ content [ key ] = value
45
+ end
46
+ end
47
+
48
+ def self . to_nested_hash ( hash = nil )
49
+ hash = self . to_hash unless hash
50
+ new_hash = { }
51
+ hash . each do |key , value |
52
+ h = value
53
+ h = YAML . load ( value ) if value . first == '[' && value . last == ']'
54
+ key . to_s . split ( '.' ) . reverse_each do |k |
55
+ h = { k => h }
56
+ end
57
+ new_hash = new_hash . deep_merge ( h )
58
+ end
59
+ new_hash
60
+ end
61
+
62
+ def self . reload_translations! ( languages )
63
+ paths = ::I18n . load_path . select { |path | self . find_language ( languages ) . include? ( File . basename ( path , '.*' ) . to_s ) }
64
+ I18n . backend . load_translations ( paths )
65
+ end
66
+
67
+ def self . find_language ( language = nil )
68
+ if language . is_a? ( Array )
69
+ language . select { |l | I18n . available_locales . include? ( l . to_s . to_sym ) } . map ( &:to_s ) . compact
70
+ elsif language . present? && I18n . available_locales . include? ( language . to_s . to_sym )
71
+ language . to_s
72
+ else
73
+ nil
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def custom_message_keys_are_available
80
+ return false if self . value [ :custom_messages ] . is_a? ( Hash ) == false || self . errors . present?
81
+ custom_messages_hash = { }
82
+ custom_messages . values . each do |hash |
83
+ custom_messages_hash = self . class . flatten_hash ( custom_messages_hash . merge ( hash ) )
84
+ end
85
+ available_keys = self . class . flatten_hash ( self . class . available_messages ) . keys
86
+ unavailable_keys = custom_messages_hash . keys . reject { |k |available_keys . include? ( k . to_sym ) }
87
+ if unavailable_keys . present?
88
+ self . errors . add ( :base , l ( :error_unavailable_keys ) + "keys: [#{ unavailable_keys . join ( ', ' ) } ]" )
89
+ return false
90
+ end
91
+ end
92
+
93
+ def change_format_to_hash
94
+ begin
95
+ if self . value [ :custom_messages ] . is_a? ( Hash )
96
+ YAML . dump ( self . value [ :custom_messages ] )
97
+ else
98
+ self . value = { custom_messages : YAML . load ( self . value [ :custom_messages ] ) }
99
+ end
100
+ rescue => e
101
+ self . errors . add ( :base , e . message )
102
+ return false
103
+ end
104
+ end
105
+ end
0 commit comments