metaforce/Editor/locale/locale.cpp

38 lines
1.0 KiB
C++
Raw Normal View History

2018-04-30 16:56:14 +00:00
#include "locale.hpp"
#include <cstring>
#include <clocale>
#include <algorithm>
#undef min
#undef max
2019-07-20 04:25:01 +00:00
namespace locale {
2018-12-08 05:19:15 +00:00
std::vector<std::pair<std::string_view, std::string_view>> ListLocales() {
std::vector<std::pair<std::string_view, std::string_view>> ret;
2019-07-20 04:25:01 +00:00
ret.reserve(std::size_t(ELocale::MAXLocale));
for (ELocale l = ELocale(0); l < ELocale::MAXLocale; l = ELocale(int(l) + 1))
ret.emplace_back(GetName(l), GetFullName(l));
2018-12-08 05:19:15 +00:00
return ret;
2018-04-30 16:56:14 +00:00
}
2019-07-20 04:25:01 +00:00
ELocale LookupLocale(std::string_view name) {
for (ELocale l = ELocale(0); l < ELocale::MAXLocale; l = ELocale(int(l) + 1))
2019-10-01 07:33:12 +00:00
if (name == GetName(l))
2019-07-20 04:25:01 +00:00
return l;
return ELocale::Invalid;
2018-04-30 16:56:14 +00:00
}
2019-07-20 04:25:01 +00:00
ELocale SystemLocaleOrEnglish() {
2018-12-08 05:19:15 +00:00
const char* sysLocale = std::setlocale(LC_ALL, nullptr);
size_t sysLocaleLen = std::strlen(sysLocale);
2019-07-20 04:25:01 +00:00
for (ELocale l = ELocale(0); l < ELocale::MAXLocale; l = ELocale(int(l) + 1)) {
auto name = GetName(l);
if (!name.compare(0, std::min(name.size(), sysLocaleLen), sysLocale))
return l;
2018-12-08 05:19:15 +00:00
}
2019-07-20 04:25:01 +00:00
return ELocale::en_US;
2018-04-30 16:56:14 +00:00
}
2019-07-20 04:25:01 +00:00
} // namespace locale