forked from paviliondev/discourse-multilingual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
291 lines (248 loc) · 10.5 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# frozen_string_literal: true
# name: discourse-multilingual
# about: Features to support multilingual forums
# version: 0.1.0
# url: https://github.com/paviliondev/discourse-multilingual
# authors: Angus McLeod
enabled_site_setting :multilingual_enabled
register_asset 'stylesheets/common/multilingual.scss'
register_asset 'stylesheets/mobile/multilingual.scss', :mobile
if respond_to?(:register_svg_icon)
register_svg_icon "language"
register_svg_icon "translate"
register_svg_icon "save"
end
%w[
../lib/validators/content_languages_validator.rb
../lib/validators/language_switcher_validator.rb
../lib/validators/translator_content_tag_validator.rb
].each do |path|
load File.expand_path(path, __FILE__)
end
after_initialize do
%w[
../lib/multilingual/multilingual.rb
../lib/multilingual/cache.rb
../lib/multilingual/language/content_tag.rb
../lib/multilingual/language/exclusion.rb
../lib/multilingual/language/custom.rb
../lib/multilingual/language/content.rb
../lib/multilingual/language/interface.rb
../lib/multilingual/language.rb
../lib/multilingual/translation/file.rb
../lib/multilingual/translation/locale.rb
../lib/multilingual/translation.rb
../lib/multilingual/translator.rb
../lib/multilingual/locale_loader.rb
../jobs/update_content_language_tags.rb
../config/routes.rb
../app/serializers/multilingual/basic_language_serializer.rb
../app/serializers/multilingual/language_serializer.rb
../app/serializers/multilingual/translation_file_serializer.rb
../app/controllers/multilingual/admin_controller.rb
../app/controllers/multilingual/admin_languages_controller.rb
../app/controllers/multilingual/admin_translations_controller.rb
../extensions/category_list.rb
../extensions/discourse_tagging.rb
../extensions/extra_locales_controller.rb
../extensions/i18n.rb
../extensions/js_locale_helper.rb
../extensions/post.rb
../extensions/tag_group.rb
../extensions/topic_serializer.rb
../extensions/application_controller.rb
].each do |path|
load File.expand_path(path, __FILE__)
end
Multilingual.setup if SiteSetting.multilingual_enabled
::I18n.singleton_class.prepend I18nMultilingualExtension
::JsLocaleHelper.singleton_class.prepend JsLocaleHelperMultilingualExtension
::ExtraLocalesController.singleton_class.prepend ExtraLocalesControllerMultilingualClassExtension
::ExtraLocalesController.prepend ExtraLocalesControllerMultilingualExtension
::TopicViewSerializer.prepend TopicSerializerMultilingualExtension
::TopicListItemSerializer.prepend TopicSerializerMultilingualExtension
::TagGroup.singleton_class.prepend TagGroupMultilingualExtension
::DiscourseTagging.singleton_class.prepend DiscourseTaggingMultilingualExtension
::CategoryList.prepend CategoryListMultilingualExtension
::Post.prepend MultilingualTranslatorPostExtension
::ApplicationController.prepend ApplicationControllerMultilingualExtension
register_html_builder('server:before-script-load') do
loader = Multilingual::LocaleLoader.new
result = ""
result += loader.preload_i18n
result += loader.preload_custom_locale if loader.custom_locale?
result += loader.preload_tag_translations
result
end
register_editable_user_custom_field :content_languages
register_editable_user_custom_field content_languages: []
whitelist_public_user_custom_field :content_languages
add_to_class(:site, :interface_languages) { Multilingual::InterfaceLanguage.list }
add_to_class(:site, :content_languages) { Multilingual::ContentLanguage.list }
add_to_class(:user, :effective_locale) do
if SiteSetting.allow_user_locale &&
self.locale.present? &&
Multilingual::InterfaceLanguage.enabled?(self.locale)
self.locale
else
SiteSetting.default_locale
end
end
add_to_class(:user, :content_languages) do
content_languages = self.custom_fields['content_languages'] || []
[*content_languages].select { |l| Multilingual::ContentLanguage.enabled?(l) }
end
add_to_class(:topic, :content_languages) do
if custom_fields['content_languages']
[*custom_fields['content_languages']]
else
[]
end
end
add_to_class(:guardian, :topic_requires_language_tag?) do |topic|
!topic.private_message? &&
Multilingual::ContentLanguage.enabled &&
(SiteSetting.multilingual_require_content_language_tag === 'yes' ||
(!is_staff? && SiteSetting.multilingual_require_content_language_tag === 'non-staff'))
end
add_to_class(:tag_groups_controller, :destroy_content_tags) do
guardian.is_admin?
Multilingual::ContentTag.destroy_all
render json: success_json
end
add_to_class(:tag_groups_controller, :update_content_tags) do
guardian.is_admin?
Multilingual::ContentTag.update_all
render json: success_json
end
add_class_method(:discourse_tagging, :validate_require_language_tag) do |guardian, topic, tag_names|
if guardian.topic_requires_language_tag?(topic) && (tag_names.blank? ||
!Tag.where(name: tag_names).where("id IN (
#{DiscourseTagging::TAG_GROUP_TAG_IDS_SQL}
AND tg.name = '#{Multilingual::ContentTag::GROUP}'
)").exists?)
topic.errors.add(:base,
I18n.t(
"tags.required_tags_from_group",
count: 1,
tag_group_name: Multilingual::ContentTag::GROUP
)
)
false
else
true
end
end
add_class_method(:locale_site_setting, :valid_value?) { |val| Multilingual::InterfaceLanguage.supported?(val) }
add_class_method(:locale_site_setting, :values) do
@values ||= supported_locales.reduce([]) do |result, locale|
if Multilingual::InterfaceLanguage.enabled?(locale)
lang = Multilingual::Language.all[locale] || Multilingual::Language.all[locale.split("_")[0]]
result.push(
name: lang ? lang['nativeName'] : locale,
value: locale
)
end
result
end
end
add_class_method(:js_locale_helper, :output_locale_tags) do |locale_str|
<<~JS
I18n.tag_translations = #{Multilingual::Translation.get("tag", locale_str).to_json};
JS
end
add_to_serializer(:user, :locale) do
Multilingual::InterfaceLanguage.enabled?(object.locale) ? object.locale : nil
end
add_to_serializer(:site, :serialize_languages) do |languages = []|
ActiveModel::ArraySerializer.new(languages, each_serializer: Multilingual::BasicLanguageSerializer, root: false).as_json
end
add_to_serializer(:site, :content_languages) { serialize_languages(object.content_languages) }
add_to_serializer(:site, :include_content_languages?) { Multilingual::ContentLanguage.enabled }
add_to_serializer(:site, :interface_languages) { serialize_languages(object.interface_languages) }
add_to_serializer(:topic_view, :content_language_tags) { Multilingual::ContentTag.filter(topic.tags).map(&:name) }
add_to_serializer(:topic_view, :include_content_language_tags?) { Multilingual::ContentLanguage.enabled }
add_to_serializer(:topic_list_item, :content_language_tags) { Multilingual::ContentTag.filter(topic.tags).map(&:name) }
add_to_serializer(:topic_list_item, :include_content_language_tags?) { Multilingual::ContentLanguage.enabled }
add_to_serializer(:current_user, :content_languages) do
if user_content_languages = object.content_languages
user_content_languages.map do |code|
Multilingual::BasicLanguageSerializer.new(
Multilingual::Language.get(code).first,
root: false
)
end
end
end
add_to_serializer(:basic_category, :name_translations) do
Multilingual::Translation.get("category_name", object.slug_path)
end
add_to_serializer(:basic_category, :include_name_translations?) { name_translations.present? }
add_to_serializer(:tag_group, :content_language_group) do
content_language_group_enabled || content_language_group_disabled
end
add_to_serializer(:tag_group, :content_language_group_enabled) do
object.id == Multilingual::ContentTag.enabled_group.id
end
add_to_serializer(:tag_group, :content_language_group_disabled) do
object.id == Multilingual::ContentTag.disabled_group.id
end
add_to_serializer(:tag_group, :name) do
content_language_group ?
I18n.t("multilingual.content_tag_group_name#{content_language_group_disabled ? "_disabled" : ""}") :
object.name
end
## This is necessary due to the workaround for jquery ajax added in multilingual-initializer
on(:user_updated) do |user|
if Multilingual::ContentLanguage.enabled && user.custom_fields['content_languages'].blank?
user.custom_fields['content_languages'] = []
user.save_custom_fields(true)
end
end
on(:site_setting_changed) do |setting, old_val, new_val|
if setting.to_sym == :multilingual_content_languages_enabled &&
ActiveModel::Type::Boolean.new.cast(new_val)
Multilingual::ContentTag.enqueue_update_all
end
end
on(:before_create_topic) do |topic, creator|
if Multilingual::ContentLanguage.enabled
content_language_tags = [*creator.opts[:content_language_tags]]
if !DiscourseTagging.validate_require_language_tag(
creator.guardian,
topic,
content_language_tags
)
creator.rollback_from_errors!(topic)
end
Multilingual::ContentTag.update_topic(topic, content_language_tags)
end
end
TopicQuery.add_custom_filter(:content_languages) do |result, query|
if Multilingual::ContentLanguage.enabled
content_languages = query.user ?
query.user.content_languages :
[*query.options[:content_languages]]
if content_languages.present? && content_languages.any?
result = result.joins(:tags).where("lower(tags.name) in (?)", content_languages)
end
end
result
end
tags_cb = ::PostRevisor.tracked_topic_fields[:tags]
::PostRevisor.tracked_topic_fields[:tags] = lambda do |tc, tags, fields|
if Multilingual::ContentLanguage.enabled
content_languages = tc.topic.content_languages
combined = (tags + content_languages).uniq
tc.check_result(DiscourseTagging.validate_require_language_tag(tc.guardian, tc.topic, combined))
tags_cb.call(tc, combined)
end
end
::PostRevisor.track_topic_field(:content_language_tags) do |tc, content_language_tags, fields|
if Multilingual::ContentLanguage.enabled
content_language_tags = [*content_language_tags]
tc.check_result(DiscourseTagging.validate_require_language_tag(tc.guardian, tc.topic, content_language_tags))
tc.check_result(Multilingual::ContentTag.update_topic(tc.topic, content_language_tags))
end
end
end