@@ -27,14 +27,14 @@ def self.cache_key
2727 # Returns the stored translation of a post or topic.
2828 # If the translation does not exist yet, it will be translated first via the API then stored.
2929 # 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 )
30+ # @param translatable [Post|Topic]
31+ def self . translate ( translatable )
32+ return if text_for_translation ( translatable ) . blank?
33+ detected_lang = detect ( translatable )
3434
35- return detected_lang , get_text ( topic_or_post ) if ( detected_lang &.to_s == I18n . locale . to_s )
35+ return detected_lang , get_text ( translatable ) if ( detected_lang &.to_s == I18n . locale . to_s )
3636
37- existing_translation = get_translation ( topic_or_post )
37+ existing_translation = get_translation ( translatable )
3838 return detected_lang , existing_translation if existing_translation . present?
3939
4040 unless translate_supported? ( detected_lang , I18n . locale )
@@ -46,27 +46,27 @@ def self.translate(topic_or_post)
4646 ) ,
4747 )
4848 end
49- [ detected_lang , translate! ( topic_or_post ) ]
49+ [ detected_lang , translate! ( translatable ) ]
5050 end
5151
5252 # Subclasses must implement this method to translate the text of a post or topic
5353 # 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 )
54+ # @param translatable [Post|Topic]
55+ def self . translate! ( translatable )
5656 raise "Not Implemented"
5757 end
5858
5959 # Returns the stored detected locale of a post or topic.
6060 # 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 )
61+ # @param translatable [Post|Topic]
62+ def self . detect ( translatable )
63+ return if text_for_detection ( translatable ) . blank?
64+ get_detected_locale ( translatable ) || detect! ( translatable )
6565 end
6666
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]
67+ # Subclasses must implement this method to detect the text of a post or topic
68+ # then use the save_detected_locale method to store the detected locale .
69+ # @param translatable [Post|Topic]
7070 def self . detect! ( post )
7171 raise "Not Implemented"
7272 end
@@ -75,52 +75,33 @@ def self.access_token
7575 raise "Not Implemented"
7676 end
7777
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 ]
78+ def self . get_translation ( translatable )
79+ translatable . translation_for ( I18n . locale )
8280 end
8381
84- def self . save_translation ( topic_or_post )
85- translated_custom_field =
86- topic_or_post . custom_fields [ DiscourseTranslator ::TRANSLATED_CUSTOM_FIELD ] || { }
87- translated_text = translated_custom_field [ I18n . locale ]
88-
89- if translated_text . nil?
90- translated_text = yield
91-
92- topic_or_post . custom_fields [
93- DiscourseTranslator ::TRANSLATED_CUSTOM_FIELD
94- ] = translated_custom_field . merge ( I18n . locale => translated_text )
95-
96- topic_or_post . save!
97- end
98-
99- translated_text
82+ def self . save_translation ( translatable )
83+ translation = yield
84+ translatable . set_translation ( I18n . locale , translation )
85+ translation
10086 end
10187
102- def self . get_detected_locale ( topic_or_post )
103- topic_or_post . custom_fields [ DiscourseTranslator :: DETECTED_LANG_CUSTOM_FIELD ]
88+ def self . get_detected_locale ( translatable )
89+ translatable . detected_locale
10490 end
10591
106- def self . save_detected_locale ( topic_or_post )
92+ def self . save_detected_locale ( translatable )
10793 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
94+ translatable . set_detected_locale ( detected_locale )
11495
11596 detected_locale
11697 end
11798
118- def self . get_text ( topic_or_post )
119- case topic_or_post . class . name
99+ def self . get_text ( translatable )
100+ case translatable . class . name
120101 when "Post"
121- topic_or_post . cooked
102+ translatable . cooked
122103 when "Topic"
123- topic_or_post . title
104+ translatable . title
124105 end
125106 end
126107
@@ -143,15 +124,12 @@ def self.strip_tags_for_detection(detection_text)
143124 html_doc . to_html
144125 end
145126
146- def self . text_for_detection ( topic_or_post )
147- strip_tags_for_detection ( get_text ( topic_or_post ) ) . truncate (
148- DETECTION_CHAR_LIMIT ,
149- omission : nil ,
150- )
127+ def self . text_for_detection ( translatable )
128+ strip_tags_for_detection ( get_text ( translatable ) ) . truncate ( DETECTION_CHAR_LIMIT , omission : nil )
151129 end
152130
153- def self . text_for_translation ( topic_or_post )
154- get_text ( topic_or_post ) . truncate ( SiteSetting . max_characters_per_translation , omission : nil )
131+ def self . text_for_translation ( translatable )
132+ get_text ( translatable ) . truncate ( SiteSetting . max_characters_per_translation , omission : nil )
155133 end
156134 end
157135end
0 commit comments