BCP 47: format includes all components even if language is empty

Before there was a shortcut: if language is empty, only private use
was formatted as that's the only possible way to produce a valid
tag. However, the GUI uses formatting of potentially invalid tags and
then parsing them again as a test for validity. Therefore the
formatting function must always format all individual components.

Fixes #2924.
This commit is contained in:
Moritz Bunkus 2020-09-07 20:23:04 +02:00
parent 093f53e007
commit 7d24542a7d
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85
3 changed files with 19 additions and 10 deletions

View File

@ -12,6 +12,10 @@
* MKVToolNix GUI: IETF BCP 47 language tags: the "variants" combo-boxes were
not populated even when the language tag was valid and contained at a
variant. Fixes #2923.
* MKVToolNix GUI: IETF BCP 47 language tags: when no language is selected, at
least one of the other components (extended subtags, region, or variants)
has something selected and "private use" is not empty, the GUI would claim
this to be a valid tag, which it isn't. Fixes #2924.
# Version 50.0.0 "Awakenings" 2020-09-06

View File

@ -79,15 +79,6 @@ language_c::format_internal(bool force)
if (!m_valid && !force)
return {};
if (m_language.empty()) {
auto output = "x"s;
for (auto const &private_use : m_private_use)
output += fmt::format("-{}", mtx::string::to_lower_ascii(private_use));
return output;
}
auto output = mtx::string::to_lower_ascii(m_language);
for (auto const &subtag : m_extended_language_subtags)
@ -106,7 +97,11 @@ language_c::format_internal(bool force)
output += fmt::format("-{}", mtx::string::to_lower_ascii(extension));
if (!m_private_use.empty()) {
output += "-x";
if (!output.empty())
output += "-";
output += "x";
for (auto const &private_use : m_private_use)
output += fmt::format("-{}", mtx::string::to_lower_ascii(private_use));
}

View File

@ -79,6 +79,16 @@ TEST(BCP47LanguageTags, Formatting) {
EXPECT_EQ("x-weee-wooo"s, l.format_long(true));
}
TEST(BCP47LanguageTags, FormattingInvalidWithoutLanguage) {
auto l = mtx::bcp47::language_c{};
l.set_region("FR"s);
l.set_private_use({ "moo"s });
EXPECT_EQ(""s, l.format());
EXPECT_EQ("-FR-x-moo"s, l.format(true));
}
TEST(BCP47LanguageTags, CodeConversion) {
EXPECT_EQ(""s, mtx::bcp47::language_c{}.get_iso639_2_code());
EXPECT_EQ("ger"s, mtx::bcp47::language_c::parse("de").get_iso639_2_code());