Skip to content

Commit bd57bb7

Browse files
committed
DEV: Modularize files in lib
1 parent 6c197c5 commit bd57bb7

18 files changed

+176
-157
lines changed

app/validators/language_switcher_setting_validator.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

config/settings.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ discourse_translator:
1313
- Amazon
1414
- Yandex
1515
- LibreTranslate
16-
validator: "DiscourseTranslator::TranslatorSelectionValidator"
16+
validator: "DiscourseTranslator::Validators::TranslatorSelectionValidator"
1717
automatic_translation_target_languages:
1818
default: ""
1919
type: list
2020
client: true
2121
list_type: named
2222
allow_any: false
2323
choices: "DiscourseTranslator::TranslatableLanguagesSetting.values"
24-
validator: "DiscourseTranslator::TranslatableLanguagesValidator"
24+
validator: "DiscourseTranslator::Validators::TranslatableLanguagesValidator"
2525
automatic_translation_backfill_maximum_translations_per_hour:
2626
default: 0
2727
client: false
@@ -117,7 +117,7 @@ discourse_translator:
117117
experimental_anon_language_switcher:
118118
default: false
119119
client: true
120-
validator: "LanguageSwitcherSettingValidator"
120+
validator: "DiscourseTranslator::Validators::LanguageSwitcherSettingValidator"
121121
experimental_inline_translation:
122122
default: false
123123
client: true
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
module DiscourseTranslator
3+
module Extensions
4+
module GuardianExtension
5+
POST_DETECTION_BUFFER = 10.seconds
6+
7+
def user_group_allow_translate?
8+
return false if !current_user
9+
current_user.in_any_groups?(SiteSetting.restrict_translation_by_group_map)
10+
end
11+
12+
def poster_group_allow_translate?(post)
13+
return false if !current_user
14+
return true if SiteSetting.restrict_translation_by_poster_group_map.empty?
15+
return false if post.user.nil?
16+
post.user.in_any_groups?(SiteSetting.restrict_translation_by_poster_group_map)
17+
end
18+
19+
def can_detect_language?(post)
20+
(
21+
SiteSetting.restrict_translation_by_poster_group_map.empty? ||
22+
post&.user&.in_any_groups?(SiteSetting.restrict_translation_by_poster_group_map)
23+
) && post.raw.present? && post.post_type != Post.types[:small_action]
24+
end
25+
26+
def can_translate?(post)
27+
return false if post.user&.bot?
28+
return false if !user_group_allow_translate?
29+
30+
# we want to return false if the post is created within a short buffer ago,
31+
# this prevents the 🌐from appearing and then disappearing if the lang is same as user's lang
32+
return false if post.created_at > POST_DETECTION_BUFFER.ago && post.detected_locale.blank?
33+
34+
if SiteSetting.experimental_inline_translation
35+
locale = DiscourseTranslator::InlineTranslation.effective_locale
36+
return false if post.locale_matches?(locale)
37+
post.translation_for(locale).nil?
38+
else
39+
return false if post.locale_matches?(I18n.locale)
40+
poster_group_allow_translate?(post)
41+
end
42+
end
43+
end
44+
end
45+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
module Extensions
5+
module PostExtension
6+
extend ActiveSupport::Concern
7+
prepended { before_update :clear_translations, if: :raw_changed? }
8+
include Translatable
9+
end
10+
end
11+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
module Extensions
5+
module TopicExtension
6+
extend ActiveSupport::Concern
7+
prepended { before_update :clear_translations, if: :title_changed? }
8+
include Translatable
9+
end
10+
end
11+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
module Extensions
5+
module TopicViewSerializerExtension
6+
def posts
7+
if SiteSetting.translator_enabled?
8+
posts_query = object.posts.includes(:content_locale)
9+
# this is kind of a micro-optimization.
10+
# we do not want to eager load translations if the user is using the site's language.
11+
# we will only load them if the user is using a different language that is supported by the site.
12+
if SiteSetting.experimental_inline_translation &&
13+
!LocaleMatcher.user_locale_is_default? &&
14+
LocaleMatcher.user_locale_in_target_languages?
15+
locale = InlineTranslation.effective_locale.to_s.gsub("_", "-")
16+
posts_query =
17+
posts_query
18+
.includes(:translations)
19+
.references(:translations)
20+
.where(translations: { locale: [nil, locale] })
21+
end
22+
object.instance_variable_set(:@posts, posts_query)
23+
end
24+
super
25+
end
26+
end
27+
end
28+
end

lib/discourse_translator/guardian_extension.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.

lib/discourse_translator/post_extension.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/discourse_translator/topic_extension.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/discourse_translator/topic_view_serializer_extension.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)