@@ -25,12 +25,11 @@ def self.cache_key
2525 "#{ key_prefix } #{ access_token_key } "
2626 end
2727
28- # Returns the stored translation of a post or topic.
29- # If the translation does not exist yet, it will be translated first via the API then stored.
28+ # Translates and saves it into a PostTranslation/TopicTranslation
3029 # If the detected language is the same as the target language, the original text will be returned.
3130 # @param translatable [Post|Topic]
31+ # @return [Array] the detected language and the translated text
3232 def self . translate ( translatable , target_locale_sym = I18n . locale )
33- return if text_for_translation ( translatable ) . blank?
3433 detected_lang = detect ( translatable )
3534
3635 if translatable . locale_matches? ( target_locale_sym )
@@ -50,35 +49,46 @@ def self.translate(translatable, target_locale_sym = I18n.locale)
5049 )
5150 end
5251
53- translated = translate_translatable! ( translatable , target_locale_sym )
54- save_translation ( translatable , target_locale_sym ) { translated }
55- [ detected_lang , translated ]
56- end
52+ begin
53+ begin
54+ translated =
55+ case translatable . class . name
56+ when "Post"
57+ translate_post! ( translatable , target_locale_sym , { cooked : true } )
58+ when "Topic"
59+ translate_topic! ( translatable , target_locale_sym )
60+ end
61+ end
62+ rescue => e
63+ raise I18n . t (
64+ "translator.failed.#{ translatable . class . name . downcase } " ,
65+ source_locale : detected_lang ,
66+ target_locale : target_locale_sym ,
67+ )
68+ end
5769
58- # TODO: Deprecate this in favour of translate_<model>
59- def self . translate_translatable! ( translatable , target_locale_sym = I18n . locale )
60- raise "Not Implemented"
70+ translatable . set_translation ( target_locale_sym , translated )
71+ [ detected_lang , translated ]
6172 end
6273
6374 def self . translate_text! ( text , target_locale_sym = I18n . locale )
6475 raise "Not Implemented"
6576 end
6677
67- def self . translate_post! ( post , target_locale_sym = I18n . locale )
68- translate_translatable! ( post , target_locale_sym )
78+ def self . translate_post! ( post , target_locale_sym = I18n . locale , opts = { } )
79+ raise "Not Implemented"
6980 end
7081
7182 def self . translate_topic! ( topic , target_locale_sym = I18n . locale )
72- translate_translatable! ( topic , target_locale_sym )
83+ raise "Not Implemented"
7384 end
7485
7586 # Returns the stored detected locale of a post or topic.
7687 # If the locale does not exist yet, it will be detected first via the API then stored.
7788 # @param translatable [Post|Topic]
7889 def self . detect ( translatable )
7990 return if text_for_detection ( translatable ) . blank?
80- get_detected_locale ( translatable ) ||
81- save_detected_locale ( translatable ) { detect! ( translatable ) }
91+ translatable . detected_locale || translatable . set_detected_locale ( detect! ( translatable ) )
8292 end
8393
8494 # Subclasses must implement this method to detect the text of a post or topic
@@ -94,29 +104,6 @@ def self.access_token
94104 raise "Not Implemented"
95105 end
96106
97- def self . save_translation ( translatable , target_locale_sym = I18n . locale )
98- begin
99- translation = yield
100- rescue Timeout ::Error
101- raise TranslatorError . new ( I18n . t ( "translator.api_timeout" ) )
102- end
103- translatable . set_translation ( target_locale_sym , translation )
104- translation
105- end
106-
107- def self . get_detected_locale ( translatable )
108- translatable . detected_locale
109- end
110-
111- def self . save_detected_locale ( translatable )
112- # sometimes we may have a user post that is just an emoji
113- # in that case, we will just indicate the post is in the default locale
114- detected_locale = yield . presence || SiteSetting . default_locale
115- translatable . set_detected_locale ( detected_locale )
116-
117- detected_locale
118- end
119-
120107 def self . language_supported? ( detected_lang )
121108 raise NotImplementedError unless self . const_defined? ( :SUPPORTED_LANG_MAPPING )
122109 supported_lang = const_get ( :SUPPORTED_LANG_MAPPING )
0 commit comments