Skip to content

Commit

Permalink
Do not expand languages that starts with the primary subtag 'x' or 'i'
Browse files Browse the repository at this point in the history
Any language that starts with the  primary language subtag 'x' is
defined by RFC 5646 section 2.2.1 as being defined by private
agreement. Therefore, 'x' isn't a language. Similarly, language tags
starting with the primarty subtag 'i' such as "i-klingon" are not of
the form "language-country" either.

Bug: chromium:1179073
Test: unit tests
Change-Id: I67d0b090df6941339fbbd0d4337d8db0288d74f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2699487
Auto-Submit: Yves Arrouye <drcrash@chromium.org>
Commit-Queue: Bence Béky <bnc@chromium.org>
Reviewed-by: Bence Béky <bnc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#854754}
  • Loading branch information
Yves Arrouye authored and Chromium LUCI CQ committed Feb 17, 2021
1 parent 0d04442 commit f0783fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions net/http/http_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,17 @@ std::string HttpUtil::ExpandLanguageList(const std::string& language_prefs) {
const std::string& language = languages[i];
builder.AddLanguageCode(language);

// Extract the base language
// Extract the primary language subtag.
const std::string& base_language = GetBaseLanguageCode(language);

// Look ahead and add the base language if the next language is not part
// of the same family.
// Skip 'x' and 'i' as a primary language subtag per RFC 5646 section 2.1.1.
if (base_language == "x" || base_language == "i")
continue;

// Look ahead and add the primary language subtag as a language if the next
// language is not part of the same family. This may not be perfect because
// an input of "en-US,fr,en" will yield "en-US,en,fr,en" and later make "en"
// a higher priority than "fr" despite the original preference.
const size_t j = i + 1;
if (j >= size || GetBaseLanguageCode(languages[j]) != base_language) {
builder.AddLanguageCode(base_language);
Expand Down
6 changes: 6 additions & 0 deletions net/http/http_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,12 @@ TEST(HttpUtilTest, ExpandLanguageList) {
HttpUtil::ExpandLanguageList("en-US,fr-CA,it,fr,es-AR,it-IT"));
// Trims a whitespace.
EXPECT_EQ("en-US,en,fr", HttpUtil::ExpandLanguageList("en-US, fr"));

// Do not expand the single character subtag 'x' as a language.
EXPECT_EQ("x-private-agreement-subtags",
HttpUtil::ExpandLanguageList("x-private-agreement-subtags"));
// Do not expand the single character subtag 'i' as a language.
EXPECT_EQ("i-klingon", HttpUtil::ExpandLanguageList("i-klingon"));
}

} // namespace net

0 comments on commit f0783fc

Please sign in to comment.