@@ -33,7 +33,7 @@ def self.translate(translatable, target_locale_sym = I18n.locale)
3333 detected_lang = detect ( translatable )
3434
3535 if translatable . locale_matches? ( target_locale_sym )
36- return detected_lang , get_text ( translatable )
36+ return detected_lang , get_untranslated ( translatable )
3737 end
3838
3939 translation = translatable . translation_for ( target_locale_sym )
@@ -48,12 +48,18 @@ def self.translate(translatable, target_locale_sym = I18n.locale)
4848 ) ,
4949 )
5050 end
51- [ detected_lang , translate! ( translatable , target_locale_sym ) ]
51+
52+ translated = translate! ( translatable , target_locale_sym )
53+ save_translation ( translatable , target_locale_sym ) { translated }
54+ [ detected_lang , translated ]
5255 end
5356
54- # Subclasses must implement this method to translate the text of a post or topic
55- # then use the save_translation method to store the translated text.
57+ # Subclasses must implement this method to translate the text of a
58+ # post or topic and return only the translated text.
59+ # Subclasses should use text_for_translation
5660 # @param translatable [Post|Topic]
61+ # @param target_locale_sym [Symbol]
62+ # @return [String]
5763 def self . translate! ( translatable , target_locale_sym = I18n . locale )
5864 raise "Not Implemented"
5965 end
@@ -63,13 +69,16 @@ def self.translate!(translatable, target_locale_sym = I18n.locale)
6369 # @param translatable [Post|Topic]
6470 def self . detect ( translatable )
6571 return if text_for_detection ( translatable ) . blank?
66- get_detected_locale ( translatable ) || detect! ( translatable )
72+ get_detected_locale ( translatable ) ||
73+ save_detected_locale ( translatable ) { detect! ( translatable ) }
6774 end
6875
6976 # Subclasses must implement this method to detect the text of a post or topic
70- # then use the save_detected_locale method to store the detected locale.
77+ # and return only the detected locale.
78+ # Subclasses should use text_for_detection
7179 # @param translatable [Post|Topic]
72- def self . detect! ( post )
80+ # @return [String]
81+ def self . detect! ( translatable )
7382 raise "Not Implemented"
7483 end
7584
@@ -100,15 +109,6 @@ def self.save_detected_locale(translatable)
100109 detected_locale
101110 end
102111
103- def self . get_text ( translatable )
104- case translatable . class . name
105- when "Post"
106- translatable . cooked
107- when "Topic"
108- translatable . title
109- end
110- end
111-
112112 def self . language_supported? ( detected_lang )
113113 raise NotImplementedError unless self . const_defined? ( :SUPPORTED_LANG_MAPPING )
114114 supported_lang = const_get ( :SUPPORTED_LANG_MAPPING )
@@ -129,11 +129,24 @@ def self.strip_tags_for_detection(detection_text)
129129 end
130130
131131 def self . text_for_detection ( translatable )
132- strip_tags_for_detection ( get_text ( translatable ) ) . truncate ( DETECTION_CHAR_LIMIT , omission : nil )
132+ strip_tags_for_detection ( get_untranslated ( translatable ) ) . truncate (
133+ DETECTION_CHAR_LIMIT ,
134+ omission : nil ,
135+ )
133136 end
134137
135138 def self . text_for_translation ( translatable )
136- get_text ( translatable ) . truncate ( SiteSetting . max_characters_per_translation , omission : nil )
139+ max_char = SiteSetting . max_characters_per_translation
140+ get_untranslated ( translatable ) . truncate ( max_char , omission : nil )
141+ end
142+
143+ def self . get_untranslated ( translatable )
144+ case translatable . class . name
145+ when "Post"
146+ translatable . cooked
147+ when "Topic"
148+ translatable . title
149+ end
137150 end
138151 end
139152end
0 commit comments