|
2 | 2 |
|
3 | 3 | module DiscourseTranslator |
4 | 4 | class InlineTranslation |
| 5 | + def self.effective_locale |
| 6 | + if LocaleMatcher.user_locale_is_default? || LocaleMatcher.user_locale_in_target_languages? |
| 7 | + I18n.locale |
| 8 | + else |
| 9 | + SiteSetting.default_locale |
| 10 | + end |
| 11 | + end |
| 12 | + |
5 | 13 | def inject(plugin) |
| 14 | + # since locales are eager loaded but translations may not, |
| 15 | + # always return early if topic and posts are in the user's effective_locale. |
| 16 | + # this prevents the need to load translations. |
| 17 | + |
6 | 18 | plugin.register_modifier(:basic_post_serializer_cooked) do |cooked, serializer| |
7 | 19 | if !SiteSetting.experimental_inline_translation || |
8 | | - serializer.scope&.request&.params&.[]("show") == "original" || |
9 | | - serializer.object.detected_locale == I18n.locale.to_s.gsub("_", "-") |
| 20 | + serializer.object.locale_matches?(InlineTranslation.effective_locale) || |
| 21 | + serializer.scope&.request&.params&.[]("show") == "original" |
10 | 22 | cooked |
11 | 23 | else |
12 | | - serializer.object.translation_for(I18n.locale).presence |
| 24 | + serializer.object.translation_for(InlineTranslation.effective_locale).presence |
13 | 25 | end |
14 | 26 | end |
15 | 27 |
|
16 | 28 | plugin.register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer| |
17 | 29 | if !SiteSetting.experimental_inline_translation || |
18 | | - serializer.scope&.request&.params&.[]("show") == "original" || |
19 | | - serializer.object.locale_matches?(I18n.locale) |
| 30 | + serializer.object.locale_matches?(InlineTranslation.effective_locale) || |
| 31 | + serializer.scope&.request&.params&.[]("show") == "original" |
20 | 32 | fancy_title |
21 | 33 | else |
22 | | - serializer.object.translation_for(I18n.locale).presence&.then { |t| Topic.fancy_title(t) } |
| 34 | + serializer |
| 35 | + .object |
| 36 | + .translation_for(InlineTranslation.effective_locale) |
| 37 | + .presence |
| 38 | + &.then { |t| Topic.fancy_title(t) } |
23 | 39 | end |
24 | 40 | end |
25 | 41 |
|
26 | 42 | plugin.register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer| |
27 | 43 | if !SiteSetting.experimental_inline_translation || |
28 | | - serializer.scope&.request&.params&.[]("show") == "original" || |
29 | | - serializer.object.topic.locale_matches?(I18n.locale) |
| 44 | + serializer.object.topic.locale_matches?(InlineTranslation.effective_locale) || |
| 45 | + serializer.scope&.request&.params&.[]("show") == "original" |
30 | 46 | fancy_title |
31 | 47 | else |
32 | 48 | serializer |
33 | 49 | .object |
34 | 50 | .topic |
35 | | - .translation_for(I18n.locale) |
| 51 | + .translation_for(InlineTranslation.effective_locale) |
36 | 52 | .presence |
37 | 53 | &.then { |t| Topic.fancy_title(t) } |
38 | 54 | end |
39 | 55 | end |
| 56 | + |
| 57 | + plugin.add_to_serializer(:basic_post, :is_translated) do |
| 58 | + SiteSetting.experimental_inline_translation && |
| 59 | + !object.locale_matches?(InlineTranslation.effective_locale) && |
| 60 | + object.translation_for(InlineTranslation.effective_locale).present? |
| 61 | + end |
| 62 | + |
| 63 | + plugin.add_to_serializer(:topic_view, :is_translated) do |
| 64 | + SiteSetting.experimental_inline_translation && |
| 65 | + !object.topic.locale_matches?(InlineTranslation.effective_locale) && |
| 66 | + object.topic.translation_for(InlineTranslation.effective_locale).present? |
| 67 | + end |
40 | 68 | end |
41 | 69 | end |
42 | 70 | end |
0 commit comments