diff --git a/Library/Homebrew/locale.rb b/Library/Homebrew/locale.rb index 40647ea62c..ac6b24c6a6 100644 --- a/Library/Homebrew/locale.rb +++ b/Library/Homebrew/locale.rb @@ -17,14 +17,14 @@ class Locale LANGUAGE_REGEX = /(?:[a-z]{2,3})/.freeze private_constant :LANGUAGE_REGEX - # ISO 3166-1 or UN M.49 - REGION_REGEX = /(?:[A-Z]{2}|\d{3})/.freeze - private_constant :REGION_REGEX - # ISO 15924 SCRIPT_REGEX = /(?:[A-Z][a-z]{3})/.freeze private_constant :SCRIPT_REGEX + # ISO 3166-1 or UN M.49 + REGION_REGEX = /(?:[A-Z]{2}|\d{3})/.freeze + private_constant :REGION_REGEX + LOCALE_REGEX = /\A((?:#{LANGUAGE_REGEX}|#{REGION_REGEX}|#{SCRIPT_REGEX})(?:-|$)){1,3}\Z/.freeze private_constant :LOCALE_REGEX @@ -56,18 +56,18 @@ class Locale return unless scanner.eos? - new(language, region, script) + new(language, script, region) end - attr_reader :language, :region, :script + attr_reader :language, :script, :region - def initialize(language, region, script) + def initialize(language, script, region) raise ArgumentError, "#{self.class} cannot be empty" if language.nil? && region.nil? && script.nil? { language: language, - region: region, script: script, + region: region, }.each do |key, value| next if value.nil? @@ -84,7 +84,7 @@ class Locale return false if other.nil? end - [:language, :region, :script].all? do |var| + [:language, :script, :region].all? do |var| if other.public_send(var).nil? true else @@ -99,7 +99,7 @@ class Locale return false if other.nil? end - [:language, :region, :script].all? do |var| + [:language, :script, :region].all? do |var| public_send(var) == other.public_send(var) end end @@ -112,6 +112,6 @@ class Locale sig { returns(String) } def to_s - [@language, @region, @script].compact.join("-") + [@language, @script, @region].compact.join("-") end end diff --git a/Library/Homebrew/test/cask/dsl_spec.rb b/Library/Homebrew/test/cask/dsl_spec.rb index 61eadc8fea..0289ce608a 100644 --- a/Library/Homebrew/test/cask/dsl_spec.rb +++ b/Library/Homebrew/test/cask/dsl_spec.rb @@ -165,7 +165,7 @@ describe Cask::DSL, :cask do "zh-CN" end - language "en-US", default: true do + language "en", default: true do sha256 "xyz789" "en-US" end diff --git a/Library/Homebrew/test/locale_spec.rb b/Library/Homebrew/test/locale_spec.rb index 3b627c7705..b20d275ced 100644 --- a/Library/Homebrew/test/locale_spec.rb +++ b/Library/Homebrew/test/locale_spec.rb @@ -7,13 +7,13 @@ describe Locale do describe "::parse" do it "parses a string in the correct format" do expect(described_class.parse("zh")).to eql(described_class.new("zh", nil, nil)) - expect(described_class.parse("zh-CN")).to eql(described_class.new("zh", "CN", nil)) - expect(described_class.parse("zh-Hans")).to eql(described_class.new("zh", nil, "Hans")) - expect(described_class.parse("zh-Hans-CN")).to eql(described_class.new("zh", "CN", "Hans")) + expect(described_class.parse("zh-CN")).to eql(described_class.new("zh", nil, "CN")) + expect(described_class.parse("zh-Hans")).to eql(described_class.new("zh", "Hans", nil)) + expect(described_class.parse("zh-Hans-CN")).to eql(described_class.new("zh", "Hans", "CN")) end it "correctly parses a string with a UN M.49 region code" do - expect(described_class.parse("es-419")).to eql(described_class.new("es", "419", nil)) + expect(described_class.parse("es-419")).to eql(described_class.new("es", nil, "419")) end describe "raises a ParserError when given" do @@ -42,13 +42,13 @@ describe Locale do it "raises a ParserError when one of the arguments does not match the locale format" do expect { described_class.new("ZH", nil, nil) }.to raise_error(Locale::ParserError) - expect { described_class.new(nil, "cn", nil) }.to raise_error(Locale::ParserError) - expect { described_class.new(nil, nil, "hans") }.to raise_error(Locale::ParserError) + expect { described_class.new(nil, "hans", nil) }.to raise_error(Locale::ParserError) + expect { described_class.new(nil, nil, "cn") }.to raise_error(Locale::ParserError) end end describe "#include?" do - subject { described_class.new("zh", "CN", "Hans") } + subject { described_class.new("zh", "Hans", "CN") } it { is_expected.to include("zh") } it { is_expected.to include("zh-CN") } @@ -59,7 +59,7 @@ describe Locale do end describe "#eql?" do - subject(:locale) { described_class.new("zh", "CN", "Hans") } + subject(:locale) { described_class.new("zh", "Hans", "CN") } context "when all parts match" do it { is_expected.to eql("zh-Hans-CN") } @@ -84,9 +84,9 @@ describe Locale do let(:locale_groups) { [["zh"], ["zh-TW"]] } it "finds best matching language code, independent of order" do - expect(described_class.new("zh", "TW", nil).detect(locale_groups)).to eql(["zh-TW"]) - expect(described_class.new("zh", "TW", nil).detect(locale_groups.reverse)).to eql(["zh-TW"]) - expect(described_class.new("zh", "CN", "Hans").detect(locale_groups)).to eql(["zh"]) + expect(described_class.new("zh", nil, "TW").detect(locale_groups)).to eql(["zh-TW"]) + expect(described_class.new("zh", nil, "TW").detect(locale_groups.reverse)).to eql(["zh-TW"]) + expect(described_class.new("zh", "Hans", "CN").detect(locale_groups)).to eql(["zh"]) end end end