locale: reorder segments to match standard format
Following #14998, reorder the locale segments to `language`, `script`, `region` to match the standard format. This does not require changes outside the `Locale` class because `Locale` instances are always constructed with the `parse` method. Signed-off-by: Ruoyu Zhong <zhongruoyu@outlook.com>
This commit is contained in:
parent
1dd2e0cd7f
commit
b4dba7a42a
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user