-
Notifications
You must be signed in to change notification settings - Fork 52
DEV: Move saving of translations into base class #203
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c5d8e7
DEV: Move saving of translations into base class
nattsw 6cd3449
Remove unnecessary method
nattsw 9760887
Add some docs
nattsw 7607b46
eql to == when comparing strings
nattsw 8c24ecf
Use correct method - completion error
nattsw 0e06bab
Add further base class tests
nattsw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,54 +74,33 @@ def self.access_token | |
| raise ProblemCheckedTranslationError.new("NotFound: Google Api Key not set.") | ||
| end | ||
|
|
||
| def self.detect(topic_or_post) | ||
| topic_or_post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= result( | ||
| DETECT_URI, | ||
| q: text_for_detection(topic_or_post), | ||
| )[ | ||
| "detections" | ||
| ][ | ||
| 0 | ||
| ].max { |a, b| a.confidence <=> b.confidence }[ | ||
| "language" | ||
| ] | ||
| 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 | ||
|
|
||
| def self.translate_supported?(source, target) | ||
| res = result(SUPPORT_URI, target: SUPPORTED_LANG_MAPPING[target]) | ||
| res["languages"].any? { |obj| obj["language"] == source } | ||
| end | ||
|
|
||
| def self.translate(topic_or_post) | ||
| detected_lang = detect(topic_or_post) | ||
|
|
||
| # 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) | ||
|
Comment on lines
-98
to
-106
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. This is moved into base.rb so that all subclasses won't face the same issue. |
||
|
|
||
| unless translate_supported?(detected_lang, I18n.locale) | ||
| raise I18n.t("translator.failed", source_locale: detected_lang, target_locale: I18n.locale) | ||
| def self.translate!(topic_or_post) | ||
| detected_locale = detect(topic_or_post) | ||
| save_translation(topic_or_post) do | ||
| res = | ||
| result( | ||
| TRANSLATE_URI, | ||
| q: text_for_translation(topic_or_post), | ||
| source: detected_locale, | ||
| target: SUPPORTED_LANG_MAPPING[I18n.locale], | ||
| ) | ||
| res["translations"][0]["translatedText"] | ||
| end | ||
|
|
||
| translated_text = | ||
| from_custom_fields(topic_or_post) do | ||
| res = | ||
| result( | ||
| TRANSLATE_URI, | ||
| q: text_for_translation(topic_or_post), | ||
| source: detected_lang, | ||
| target: SUPPORTED_LANG_MAPPING[I18n.locale], | ||
| ) | ||
| res["translations"][0]["translatedText"] | ||
| end | ||
|
|
||
| [detected_lang, translated_text] | ||
| end | ||
|
|
||
| def self.result(url, body) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is moved to base.rb's
save_detected_locale