Skip to content

Conversation

@nattsw
Copy link
Contributor

@nattsw nattsw commented Feb 5, 2025

Context

Since a long long time ago, we do the following in the Job::DetectTranslation:

"DiscourseTranslator::#{SiteSetting.translator}".constantize.detect(post)
if !post.custom_fields_clean?
post.save_custom_fields
post.publish_change_to_clients! :revised
end

  1. The first line, the method .detect has a side effect of assigning the detected language e.g. "en" into a custom field.
  2. The subsequent lines in the job then saves the custom field.

The above is slightly confusing as there should be just one place that sets and saves the value. Additionally, the knowledge of the usage custom fields are in every subclass of the base translator.

Change

The following changes allow the saving of translation metadata to be in the single base class instead of sprinkled in all the subclasses. This is to prepare for the move from custom fields to proper tables.

This PR does two things:

  1. introduce a detect! (and also translate!) in translator subclasses (Amazon, Google, Microsoft, etc) which will set the value from the API all the time. The base class invokes detect!.
    def self.detect!(topic_or_post)
    save_detected_locale(topic_or_post) do
    result(DETECT_URI, q: text_for_detection(topic_or_post))["detections"][0].max do |a, b|
    a.confidence <=> b.confidence
    end[
    "language"
    ]
    end
    end
  2. update detect to return the stored value or invoke the ! variant to get the value if it does not exist.
    # Returns the stored detected locale of a post or topic.
    # If the locale does not exist yet, it will be detected first via the API then stored.
    # @param topic_or_post [Post|Topic]
    def self.detect(topic_or_post)
    return if text_for_detection(topic_or_post).blank?
    get_detected_locale(topic_or_post) || detect!(topic_or_post)
    end

There is already test coverage for this refactor.

Comment on lines -29 to -32
if !post.custom_fields_clean?
post.save_custom_fields
post.publish_change_to_clients!(:revised)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved to base.rb's save_detected_locale

Comment on lines -98 to -106
# the translate button appears if a given post is in a foreign language.
# however the title of the topic may be in a different language, and may be in the user's language.
# if this is the case, when this is called for a topic, the detected_lang will be the user's language,
# so the user's language and the detected language will be the same. For example, both could be "en"
# google will choke on this and return an error instead of gracefully handling it by returning the original
# string.
# ---
# here we handle that situation by returning the original string if the source and target lang are the same.
return detected_lang, get_text(topic_or_post) if (detected_lang&.to_s.eql? I18n.locale.to_s)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved into base.rb so that all subclasses won't face the same issue.

@nattsw nattsw merged commit a43c603 into main Feb 6, 2025
4 checks passed
@nattsw nattsw deleted the detect-translate-no-custom branch February 6, 2025 12:59
nattsw added a commit that referenced this pull request May 23, 2025
In #203 we introduced a bug for the amazon provider, which was fixed in #297.

This commit is a migration to purge the incorrect translations that have been stored. The translation will look like the following as the full response is saved instead of the specific attribute.

```
{translated_text: ...
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants