Tag Parser 12.4.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
Loading...
Searching...
No Matches
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
7using namespace std::literals;
8
9namespace TagParser {
10
12static const auto &languageNames_ISO_639_2_b()
13{
14#include "resources/iso_language_codes.h"
15 return languageNames_iso_639_2_b;
16}
18
22inline static bool isLanguageDefined_ISO_639_2(const std::string &languageSpecification)
23{
24 return !languageSpecification.empty() && languageSpecification != "und" && languageSpecification != "XXX";
25}
26
31static 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
49{
50 static const auto empty = LocaleDetail();
51 return empty;
52}
53
63{
64 for (const auto &detail : *this) {
65 if (!detail.empty() && detail.format == format && isLanguageDefined_ISO_639_2(detail)) {
66 return detail;
67 }
68 }
70}
71
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
110const 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
124const std::string &Locale::fullOrSomeAbbreviatedName() const
125{
126 if (const auto &name = fullName(); !name.empty()) {
127 return name;
128 }
129 return someAbbreviatedName();
130}
131
135std::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.
The LocaleDetail struct specifies a language and/or country.
static const LocaleDetail & getEmpty()
Returns an empty LocaleDetail.
LocaleDetail()
Constructs an empty LocaleDetail.
const LocaleDetail & abbreviatedName(LocaleFormat format) const
Returns the abbreviated name of the specified format.
const std::string & fullOrSomeAbbreviatedName() const
Returns the full name if possible and otherwise falls back to the abbreviated name.
std::string toString() const
Returns all details as comma-separated string.
const std::string & fullName() const
const LocaleDetail & someAbbreviatedName(LocaleFormat preferredFormat=LocaleFormat::BCP_47) const
Returns some abbreviated name, preferably of the specified preferredFormat.