@@ -24,19 +24,64 @@ def self.cache_key
2424 "#{ key_prefix } #{ access_token_key } "
2525 end
2626
27- def self . translate ( post )
27+ # Returns the stored translation of a post or topic.
28+ # If the translation does not exist yet, it will be translated first via the API then stored.
29+ # If the detected language is the same as the target language, the original text will be returned.
30+ # @param topic_or_post [Post|Topic]
31+ def self . translate ( topic_or_post )
32+ return if text_for_translation ( topic_or_post ) . blank?
33+ detected_lang = detect ( topic_or_post )
34+
35+ return detected_lang , get_text ( topic_or_post ) if ( detected_lang &.to_s == I18n . locale . to_s )
36+
37+ existing_translation = get_translation ( topic_or_post )
38+ return detected_lang , existing_translation if existing_translation . present?
39+
40+ unless translate_supported? ( detected_lang , I18n . locale )
41+ raise TranslatorError . new (
42+ I18n . t (
43+ "translator.failed" ,
44+ source_locale : detected_lang ,
45+ target_locale : I18n . locale ,
46+ ) ,
47+ )
48+ end
49+ [ detected_lang , translate! ( topic_or_post ) ]
50+ end
51+
52+ # Subclasses must implement this method to translate the text of a post or topic
53+ # then use the save_translation method to store the translated text.
54+ # @param topic_or_post [Post|Topic]
55+ def self . translate! ( topic_or_post )
2856 raise "Not Implemented"
2957 end
3058
31- def self . detect ( post )
59+ # Returns the stored detected locale of a post or topic.
60+ # If the locale does not exist yet, it will be detected first via the API then stored.
61+ # @param topic_or_post [Post|Topic]
62+ def self . detect ( topic_or_post )
63+ return if text_for_detection ( topic_or_post ) . blank?
64+ get_detected_locale ( topic_or_post ) || detect! ( topic_or_post )
65+ end
66+
67+ # Subclasses must implement this method to translate the text of a post or topic
68+ # then use the save_translation method to store the translated text.
69+ # @param topic_or_post [Post|Topic]
70+ def self . detect! ( post )
3271 raise "Not Implemented"
3372 end
3473
3574 def self . access_token
3675 raise "Not Implemented"
3776 end
3877
39- def self . from_custom_fields ( topic_or_post )
78+ def self . get_translation ( topic_or_post )
79+ translated_custom_field =
80+ topic_or_post . custom_fields [ DiscourseTranslator ::TRANSLATED_CUSTOM_FIELD ] || { }
81+ translated_custom_field [ I18n . locale ]
82+ end
83+
84+ def self . save_translation ( topic_or_post )
4085 translated_custom_field =
4186 topic_or_post . custom_fields [ DiscourseTranslator ::TRANSLATED_CUSTOM_FIELD ] || { }
4287 translated_text = translated_custom_field [ I18n . locale ]
@@ -54,6 +99,22 @@ def self.from_custom_fields(topic_or_post)
5499 translated_text
55100 end
56101
102+ def self . get_detected_locale ( topic_or_post )
103+ topic_or_post . custom_fields [ DiscourseTranslator ::DETECTED_LANG_CUSTOM_FIELD ]
104+ end
105+
106+ def self . save_detected_locale ( topic_or_post )
107+ detected_locale = yield
108+ topic_or_post . custom_fields [ DiscourseTranslator ::DETECTED_LANG_CUSTOM_FIELD ] = detected_locale
109+
110+ if !topic_or_post . custom_fields_clean?
111+ topic_or_post . save_custom_fields
112+ topic_or_post . publish_change_to_clients! ( :revised ) if topic_or_post . class . name == "Post"
113+ end
114+
115+ detected_locale
116+ end
117+
57118 def self . get_text ( topic_or_post )
58119 case topic_or_post . class . name
59120 when "Post"
@@ -70,6 +131,10 @@ def self.language_supported?(detected_lang)
70131 detected_lang != supported_lang [ I18n . locale ]
71132 end
72133
134+ def self . translate_supported? ( detected_lang , target_lang )
135+ true
136+ end
137+
73138 private
74139
75140 def self . strip_tags_for_detection ( detection_text )
0 commit comments