Tag Parser: localehelper.cpp Source File

Tag Parser 12.5.3 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
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
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
localehelper.h
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TagParser::LocaleFormat
LocaleFormat
The LocaleFormat enum class specifies the format used by a LocaleDetail.
Definition localehelper.h:14
TagParser::LocaleFormat::ISO_639_2_T
@ ISO_639_2_T
Definition localehelper.h:18
TagParser::LocaleFormat::ISO_639_2_B
@ ISO_639_2_B
Definition localehelper.h:19
TagParser::LocaleFormat::Unknown
@ Unknown
Definition localehelper.h:15
TagParser::LocaleDetail
The LocaleDetail struct specifies a language and/or country.
Definition localehelper.h:26
TagParser::LocaleDetail::getEmpty
static const LocaleDetail & getEmpty()
Returns an empty LocaleDetail.
Definition localehelper.cpp:48
TagParser::LocaleDetail::LocaleDetail
LocaleDetail()
Constructs an empty LocaleDetail.
Definition localehelper.h:37
TagParser::Locale::abbreviatedName
const LocaleDetail & abbreviatedName(LocaleFormat format) const
Returns the abbreviated name of the specified format.
Definition localehelper.cpp:62
TagParser::Locale::fullOrSomeAbbreviatedName
const std::string & fullOrSomeAbbreviatedName() const
Returns the full name if possible and otherwise falls back to the abbreviated name.
Definition localehelper.cpp:124
TagParser::Locale::toString
std::string toString() const
Returns all details as comma-separated string.
Definition localehelper.cpp:135
TagParser::Locale::fullName
const std::string & fullName() const
Returns the full name of the locale, e.g.
Definition localehelper.cpp:110
TagParser::Locale::someAbbreviatedName
const LocaleDetail & someAbbreviatedName(LocaleFormat preferredFormat=LocaleFormat::BCP_47) const
Returns some abbreviated name, preferably of the specified preferredFormat.
Definition localehelper.cpp:85