Tag Parser  10.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
localehelper.cpp
Go to the documentation of this file.
1 #include "./localehelper.h"
2 
3 #include <c++utilities/conversion/stringconversion.h>
4 
5 #include <unordered_map>
6 
7 using namespace std::literals;
8 
9 namespace TagParser {
10 
12 static const auto &languageNames_ISO_639_2_b()
13 {
14 #include "resources/iso_language_codes.h"
15  return languageNames_iso_639_2_b;
16 }
18 
22 inline static bool isLanguageDefined_ISO_639_2(const std::string &languageSpecification)
23 {
24  return !languageSpecification.empty() && languageSpecification != "und" && languageSpecification != "XXX";
25 }
26 
31 static const std::string &languageName_ISO_639_2(const std::string &isoCode)
32 {
33  static const std::string empty;
34  if (!isLanguageDefined_ISO_639_2(isoCode)) {
35  return empty;
36  }
37  const auto &mapping = languageNames_ISO_639_2_b();
38  const auto i = mapping.find(isoCode);
39  if (i == mapping.cend()) {
40  return empty;
41  }
42  return i->second;
43 }
44 
48 const LocaleDetail &LocaleDetail::getEmpty()
49 {
50  static const auto empty = LocaleDetail();
51  return empty;
52 }
53 
62 const LocaleDetail &Locale::abbreviatedName(LocaleFormat format) const
63 {
64  for (const auto &detail : *this) {
65  if (!detail.empty() && detail.format == format && isLanguageDefined_ISO_639_2(detail)) {
66  return detail;
67  }
68  }
69  return LocaleDetail::getEmpty();
70 }
71 
85 const LocaleDetail &Locale::someAbbreviatedName(LocaleFormat preferredFormat) const
86 {
87  auto format = LocaleFormat::Unknown;
88  const LocaleDetail *mostRelevantDetail = nullptr;
89  for (const auto &detail : *this) {
90  if (!detail.empty()
91  && static_cast<std::underlying_type_t<LocaleFormat>>(detail.format) >= static_cast<std::underlying_type_t<LocaleFormat>>(format)) {
92  if (detail.format == preferredFormat) {
93  return detail;
94  }
95  format = detail.format;
96  mostRelevantDetail = &detail;
97  }
98  }
99  if (!mostRelevantDetail || !isLanguageDefined_ISO_639_2(*mostRelevantDetail)) {
100  return LocaleDetail::getEmpty();
101  }
102  return *mostRelevantDetail;
103 }
104 
110 const std::string &TagParser::Locale::fullName() const
111 {
112  for (const auto &detail : *this) {
113  if (detail.format == LocaleFormat::ISO_639_2_B || detail.format == LocaleFormat::ISO_639_2_T) {
114  return languageName_ISO_639_2(detail);
115  }
116  }
117  return LocaleDetail::getEmpty();
118 }
119 
124 const std::string &Locale::fullOrSomeAbbreviatedName() const
125 {
126  if (const auto &name = fullName(); !name.empty()) {
127  return name;
128  }
129  return someAbbreviatedName();
130 }
131 
135 std::string Locale::toString() const
136 {
137  return CppUtilities::joinStrings<std::vector<LocaleDetail>, std::string>(*this, LocaleDetail(", "sv, LocaleFormat::Unknown), true);
138 }
139 
140 } // namespace TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
LocaleFormat
The LocaleFormat enum class specifies the format used by a LocaleDetail.
Definition: localehelper.h:14
The LocaleDetail struct specifies a language and/or country.
Definition: localehelper.h:26
const std::string & fullName() const
Returns the full name of the locale, e.g.