File tree Expand file tree Collapse file tree 5 files changed +28
-26
lines changed
app/services/discourse_translator Expand file tree Collapse file tree 5 files changed +28
-26
lines changed Original file line number Diff line number Diff line change @@ -63,12 +63,11 @@ def self.get_text(topic_or_post)
6363 end
6464 end
6565
66- def self . language_supported? ( detected_lang , site_lang )
66+ def self . language_supported? ( detected_lang )
6767 raise NotImplementedError unless self . const_defined? ( :SUPPORTED_LANG_MAPPING )
68- site_lang_sym = site_lang . to_sym
6968 supported_lang = const_get ( :SUPPORTED_LANG_MAPPING )
70- return false if supported_lang [ site_lang_sym ] . nil?
71- detected_lang . to_sym != supported_lang [ site_lang_sym ] . to_sym
69+ return false if supported_lang [ I18n . locale ] . nil?
70+ detected_lang != supported_lang [ I18n . locale ]
7271 end
7372
7473 private
Original file line number Diff line number Diff line change 66module DiscourseTranslator
77 class DiscourseAi < Base
88 MAX_DETECT_LOCALE_TEXT_LENGTH = 1000
9- def self . language_supported? ( _ , _ )
10- true
9+ def self . language_supported? ( detected_lang )
10+ locale_without_region = I18n . locale . to_s . split ( "_" ) . first
11+ detected_lang != locale_without_region
1112 end
1213
1314 def self . detect ( topic_or_post )
Original file line number Diff line number Diff line change @@ -22,24 +22,17 @@ def can_detect_language?(post)
2222 def can_translate? ( post )
2323 return false if !user_group_allow_translate?
2424
25- # we will deal with strings (not syms) when comparing locales below
25+ # we will deal with regionalized_strings (not syms) when comparing locales
26+ # e.g. "en_GB"
27+ # not "en-GB"
28+ # nor :en_GB (I18n.locale)
2629 detected_lang =
2730 post . custom_fields [ ::DiscourseTranslator ::DETECTED_LANG_CUSTOM_FIELD ] . to_s . sub ( "-" , "_" )
2831 return false if detected_lang . blank?
2932
30- locale_without_region = I18n . locale . to_s . split ( "_" ) . first
31- site_locale =
32- (
33- if SiteSetting . normalize_language_variants_map . include? ( locale_without_region )
34- locale_without_region
35- else
36- I18n . locale . to_s
37- end
38- )
39- detected_lang != site_locale &&
33+ detected_lang != I18n . locale . to_s &&
4034 "DiscourseTranslator::#{ SiteSetting . translator } " . constantize . language_supported? (
4135 detected_lang ,
42- site_locale ,
4336 )
4437 end
4538end
Original file line number Diff line number Diff line change @@ -12,21 +12,24 @@ class EmptyTranslator < DiscourseTranslator::Base
1212
1313 describe ".language_supported?" do
1414 it "raises an error when the method is not implemented" do
15- expect { EmptyTranslator . language_supported? ( "en" , "en" ) } . to raise_error ( NotImplementedError )
15+ expect { EmptyTranslator . language_supported? ( "en" ) } . to raise_error ( NotImplementedError )
1616 end
1717
1818 it "returns false when the locale is not supported" do
19- expect ( TestTranslator . language_supported? ( "en" , "xx" ) ) . to eq ( false )
19+ I18n . stubs ( :locale ) . returns ( :xx )
20+ expect ( TestTranslator . language_supported? ( "en" ) ) . to eq ( false )
2021 end
2122
2223 it "returns true when the detected language is not the current locale" do
23- expect ( TestTranslator . language_supported? ( "en" , "pt" ) ) . to eq ( true )
24- expect ( TestTranslator . language_supported? ( "ar" , "pt" ) ) . to eq ( true )
25- expect ( TestTranslator . language_supported? ( "es-MX" , "pt" ) ) . to eq ( true )
24+ I18n . locale = :pt
25+ expect ( TestTranslator . language_supported? ( "en" ) ) . to eq ( true )
26+ expect ( TestTranslator . language_supported? ( "ar" ) ) . to eq ( true )
27+ expect ( TestTranslator . language_supported? ( "es-MX" ) ) . to eq ( true )
2628 end
2729
2830 it "returns false when the detected language is the detected locale" do
29- expect ( TestTranslator . language_supported? ( "pt" , "pt" ) ) . to eq ( false )
31+ I18n . locale = :pt
32+ expect ( TestTranslator . language_supported? ( "pt" ) ) . to eq ( false )
3033 end
3134 end
3235
Original file line number Diff line number Diff line change 1515 end
1616
1717 describe ".language_supported?" do
18- it "returns true for any language" do
19- expect ( described_class . language_supported? ( "any-language" , "??" ) ) . to eq ( true )
18+ it "returns true when detected language is different from i18n locale" do
19+ I18n . stubs ( :locale ) . returns ( :xx )
20+ expect ( described_class . language_supported? ( "any-language" ) ) . to eq ( true )
21+ end
22+
23+ it "returns false when detected language is same base language as i18n locale" do
24+ I18n . stubs ( :locale ) . returns ( :en_GB )
25+ expect ( described_class . language_supported? ( "en" ) ) . to eq ( false )
2026 end
2127 end
2228
You can’t perform that action at this time.
0 commit comments