Skip to content
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

DEV: Move saving of translations into base class #203

Merged
merged 6 commits into from
Feb 6, 2025

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
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.

2 participants