File tree Expand file tree Collapse file tree 4 files changed +64
-6
lines changed
app/services/discourse_translator Expand file tree Collapse file tree 4 files changed +64
-6
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,9 @@ def self.detect_locale(post)
77
88 translator = DiscourseTranslator ::Provider ::TranslatorProvider . get
99 detected_locale = translator . detect! ( post )
10- post . update! ( locale : detected_locale )
11- detected_locale
10+ locale = LocaleNormalizer . normalize_to_i18n ( detected_locale )
11+ post . update! ( locale :)
12+ locale
1213 end
1314 end
1415end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module DiscourseTranslator
4+ class LocaleNormalizer
5+ # Normalizes locale string, matching the list of I18n.locales where possible
6+ # @param locale [String,Symbol] the locale to normalize
7+ # @return [String] the normalized locale
8+ def self . normalize_to_i18n ( locale )
9+ return nil if locale . blank?
10+ locale = locale . to_s . gsub ( "-" , "_" )
11+
12+ i18n_pairs . each { |downcased , value | return value if locale . downcase == downcased }
13+
14+ locale
15+ end
16+
17+ private
18+
19+ def self . i18n_pairs
20+ # they should look like this for the input to match against:
21+ # {
22+ # "lowercased" => "actual",
23+ # "en" => "en",
24+ # "zh_cn" => "zh_CN",
25+ # "zh" => "zh_CN",
26+ # }
27+ @locale_map ||=
28+ begin
29+ output = { }
30+ I18n . available_locales . each do |sym |
31+ locale = sym . to_s
32+ output [ locale . downcase ] = locale
33+ if locale . include? ( "_" )
34+ short = locale . split ( "_" ) . first
35+ output [ short ] = locale if output [ short ] . blank?
36+ end
37+ end
38+
39+ output
40+ end
41+ end
42+ end
43+ end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ describe DiscourseTranslator ::LocaleNormalizer do
4+ it "matches input locales to i18n locales" do
5+ expect ( described_class . normalize_to_i18n ( "en-GB" ) ) . to eq ( "en_GB" )
6+ expect ( described_class . normalize_to_i18n ( "en" ) ) . to eq ( "en" )
7+ expect ( described_class . normalize_to_i18n ( "zh" ) ) . to eq ( "zh_CN" )
8+ expect ( described_class . normalize_to_i18n ( "tr" ) ) . to eq ( "tr_TR" )
9+ end
10+
11+ it "converts dashes to underscores" do
12+ expect ( described_class . normalize_to_i18n ( "a-b" ) ) . to eq ( "a_b" )
13+ end
14+ end
Original file line number Diff line number Diff line change 1313 end
1414
1515 it "calls detect! on the provider with the post" do
16- translator . expects ( :detect! ) . with ( post ) . returns ( "ja " )
17- expect ( described_class . detect_locale ( post ) ) . to eq ( "ja " )
16+ translator . expects ( :detect! ) . with ( post ) . returns ( "zh " )
17+ expect ( described_class . detect_locale ( post ) ) . to eq ( "zh_CN " )
1818 end
1919
2020 it "updates the post locale with the detected locale" do
21- translator . stubs ( :detect! ) . with ( post ) . returns ( "ja " )
21+ translator . stubs ( :detect! ) . with ( post ) . returns ( "zh " )
2222 expect { described_class . detect_locale ( post ) } . to change { post . reload . locale } . from ( nil ) . to (
23- "ja " ,
23+ "zh_CN " ,
2424 )
2525 end
2626 end
You can’t perform that action at this time.
0 commit comments