-
Notifications
You must be signed in to change notification settings - Fork 52
FIX: 'Show original' button only shows in topics where there are translated content #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
3326e73
92b7d48
8df7a49
024fc67
7a7310d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,41 +2,69 @@ | |
|
|
||
| module DiscourseTranslator | ||
| class InlineTranslation | ||
| def self.effective_locale | ||
| if LocaleMatcher.user_locale_is_default? || LocaleMatcher.user_locale_in_target_languages? | ||
| I18n.locale | ||
| else | ||
| SiteSetting.default_locale | ||
| end | ||
| end | ||
|
|
||
| def inject(plugin) | ||
| # since locales are eager loaded but translations may not, | ||
| # always return early if topic and posts are in the user's effective_locale. | ||
| # this prevents the need to load translations. | ||
|
Comment on lines
+14
to
+16
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All plugin api usages will check for locale first. |
||
|
|
||
| plugin.register_modifier(:basic_post_serializer_cooked) do |cooked, serializer| | ||
| if !SiteSetting.experimental_inline_translation || | ||
| serializer.scope&.request&.params&.[]("show") == "original" || | ||
| serializer.object.detected_locale == I18n.locale.to_s.gsub("_", "-") | ||
| serializer.object.locale_matches?(InlineTranslation.effective_locale) || | ||
| serializer.scope&.request&.params&.[]("show") == "original" | ||
| cooked | ||
| else | ||
| serializer.object.translation_for(I18n.locale).presence | ||
| serializer.object.translation_for(InlineTranslation.effective_locale).presence | ||
| end | ||
| end | ||
|
|
||
| plugin.register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer| | ||
| if !SiteSetting.experimental_inline_translation || | ||
| serializer.scope&.request&.params&.[]("show") == "original" || | ||
| serializer.object.locale_matches?(I18n.locale) | ||
| serializer.object.locale_matches?(InlineTranslation.effective_locale) || | ||
| serializer.scope&.request&.params&.[]("show") == "original" | ||
| fancy_title | ||
| else | ||
| serializer.object.translation_for(I18n.locale).presence&.then { |t| Topic.fancy_title(t) } | ||
| serializer | ||
| .object | ||
| .translation_for(InlineTranslation.effective_locale) | ||
| .presence | ||
| &.then { |t| Topic.fancy_title(t) } | ||
| end | ||
| end | ||
|
|
||
| plugin.register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer| | ||
| if !SiteSetting.experimental_inline_translation || | ||
| serializer.scope&.request&.params&.[]("show") == "original" || | ||
| serializer.object.topic.locale_matches?(I18n.locale) | ||
| serializer.object.topic.locale_matches?(InlineTranslation.effective_locale) || | ||
| serializer.scope&.request&.params&.[]("show") == "original" | ||
| fancy_title | ||
| else | ||
| serializer | ||
| .object | ||
| .topic | ||
| .translation_for(I18n.locale) | ||
| .translation_for(InlineTranslation.effective_locale) | ||
| .presence | ||
| &.then { |t| Topic.fancy_title(t) } | ||
| end | ||
| end | ||
|
|
||
| plugin.add_to_serializer(:basic_post, :is_translated) do | ||
| SiteSetting.experimental_inline_translation && | ||
| !object.locale_matches?(InlineTranslation.effective_locale) && | ||
| object.translation_for(InlineTranslation.effective_locale).present? | ||
| end | ||
|
|
||
| plugin.add_to_serializer(:topic_view, :is_translated) do | ||
| SiteSetting.experimental_inline_translation && | ||
| !object.topic.locale_matches?(InlineTranslation.effective_locale) && | ||
| object.topic.translation_for(InlineTranslation.effective_locale).present? | ||
| end | ||
|
Comment on lines
+57
to
+67
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two flags are used in the frontend to check if a topic is showing translated content |
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseTranslator | ||
| class LocaleMatcher | ||
| def self.user_locale_in_target_languages? | ||
| # e.g. "en|es|pt_BR" vs :en_UK | ||
| SiteSetting.automatic_translation_target_languages.split("|").include?(I18n.locale.to_s) | ||
| end | ||
|
|
||
| def self.user_locale_is_default? | ||
| # e.g. :en_UK vs "en_UK" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| I18n.locale.to_s == SiteSetting.default_locale | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When in
experimental_inline_translation, we only want to allow a user to translate a post if the user's locale is within the target languages that we support for the site (effective_locale).