v1.2.38 Fix for locale issues on non-en-US locales.

This commit is contained in:
Robin Davies
2024-08-19 00:27:28 -04:00
parent 6358682eb1
commit fa3d8815cd
16 changed files with 194 additions and 64 deletions
+4 -1
View File
@@ -133,6 +133,7 @@ endif()
set (PIPEDAL_SOURCES
WebServerConfig.cpp WebServerConfig.hpp
Locale.hpp Locale.cpp
Finally.hpp
ZipFile.cpp ZipFile.hpp
TemporaryFile.cpp TemporaryFile.hpp
@@ -204,7 +205,6 @@ set (PIPEDAL_SOURCES
AdminClient.hpp AdminClient.cpp
MidiBinding.hpp MidiBinding.cpp
PiPedalMath.hpp
Locale.hpp Locale.cpp
Lv2EventBufferWriter.hpp Lv2EventBufferWriter.cpp
IpSubnet.hpp
PiPedalAlsa.hpp PiPedalAlsa.cpp
@@ -235,6 +235,9 @@ set (PIPEDAL_INCLUDES
)
set(PIPEDAL_LIBS libpipedald zip
icui18n
icuuc
icudata
pthread atomic stdc++fs asound avahi-common avahi-client systemd
${VST3_LIBRARIES}
${LILV_0_LIBRARIES}
-1
View File
@@ -607,7 +607,6 @@ void SetVarPermissions(
if (fs::is_directory(path)) {
fs::permissions(path, directoryPermissions, fs::perm_options::replace);
for (const auto& entry : fs::recursive_directory_iterator(path)) {
cout << " " << entry.path() << endl;
if (chown(entry.path().c_str(),uid,gid) != 0)
{
+117 -26
View File
@@ -23,43 +23,134 @@
#include <stdlib.h>
#include "Lv2Log.hpp"
#include <unicode/utypes.h>
#include <unicode/ucol.h>
#include <unicode/unistr.h>
#include <unicode/sortkey.h>
#include <stdexcept>
#include "ss.hpp"
#include <mutex>
// Must be UNICODE. Should reflect system locale. (.e.g en-US.UTF8, de-de.UTF8)
// This is correct for libstdc++; most probably not correct for Windows or CLANG. :-(
using namespace pipedal;
static const char*getLocale(const char*localeEnvironmentVariable)
std::string getCurrentLocale() {
std::string locale = setlocale(LC_ALL, nullptr);
if (locale.empty() || locale == "C") {
// If setlocale fails, try getting it from environment variables
const char* lang = getenv("LC_ALL");
if (lang) {
locale = lang;
} else {
// If LANG is not set, fall back to a default
lang = getenv("LC_COLLATE");
if (lang)
{
locale = lang;
} else{
lang = getenv("LANG");
if (lang) {
locale = lang;
} else {
locale = "en_US";
}
}
}
}
// Extract just the language and country code
size_t dot_pos = locale.find('.');
if (dot_pos != std::string::npos) {
locale = locale.substr(0, dot_pos);
}
return locale;
}
Collator::~Collator() {
}
class LocaleImpl;
class CollatorImpl : public Collator {
public:
CollatorImpl(std::shared_ptr<LocaleImpl> LocaleImpl,icu::Locale &locale);
~CollatorImpl();
virtual int Compare(const std::string &left, const std::string&right);
private:
icu::Collator* collator = nullptr;
std::shared_ptr<LocaleImpl> localeImpl;
};
CollatorImpl::~CollatorImpl()
{
const char*result = getenv(localeEnvironmentVariable);
if (result == nullptr)
{
result = getenv("LC_ALL");
}
delete collator;
localeImpl = nullptr;
}
CollatorImpl::CollatorImpl(std::shared_ptr<LocaleImpl> localeImpl, icu::Locale &locale)
:localeImpl(localeImpl)
{
UErrorCode status = U_ZERO_ERROR;
if (result == nullptr) {
result = "en_US.UTF-8";
this->collator = icu::Collator::createInstance(locale, status);
if (U_FAILURE(status)) {
throw std::runtime_error(SS("Failed to create collator: " << u_errorName(status)));
}
std::stringstream s;
s << "Locale: " << result;
Lv2Log::error(s.str());
return result;
}
int CollatorImpl::Compare(const std::string &left, const std::string&right) {
return collator->compare(left.c_str(),right.c_str());
}
Locale::ptr g_instance;
class LocaleImpl: public Locale, public std::enable_shared_from_this<LocaleImpl> {
public:
LocaleImpl();
virtual const std::string &CurrentLocale() const ;
virtual Collator::ptr GetCollator();
private:
std::unique_ptr<icu::Locale> locale;
std::string currentLocale;
};
LocaleImpl::LocaleImpl()
{
currentLocale = getCurrentLocale();
locale = std::make_unique<icu::Locale>(currentLocale.c_str());
}
const std::string &LocaleImpl::CurrentLocale() const
{
return currentLocale;
}
void Locale::setDefaultLocale() {
const char* locale = getLocale("LC_ALL");
try {
setlocale(LC_ALL,locale);
} catch (const std::exception&)
{
std::stringstream s;
s << "Failed to set default locale (" << locale << "). Defaulting to en_US.";
Lv2Log::error(s.str());
std::setlocale(LC_ALL,"en_US.UTF-8");
}
Collator::ptr LocaleImpl::GetCollator(){
auto pThis = shared_from_this();
return std::shared_ptr<Collator>(new CollatorImpl(pThis,*locale));
}
const std::collate<char>& Locale::collation() { return std::use_facet<std::collate<char> >(std::locale()); }
static std::mutex createMutex;
Locale::~Locale()
{
}
Locale::ptr Locale::g_instance;
Locale::ptr Locale::GetInstance()
{
std::lock_guard lock { createMutex};
if (g_instance)
{
return g_instance;
}
g_instance = std::make_shared<LocaleImpl>();
return g_instance;
}
+25 -5
View File
@@ -19,14 +19,34 @@
#pragma once
#include <locale>
#include <memory>
#include <string>
namespace pipedal {
class Locale {
class Collator {
public:
static void setDefaultLocale();
using ptr = std::shared_ptr<Collator>;
virtual int Compare(const std::string &left, const std::string&right) = 0;
virtual ~Collator();
};
class Locale {
protected:
Locale() { }
public:
virtual ~Locale();
// no copy, no move
Locale(const Locale&) = delete;
Locale(Locale&&) = delete;
Locale&operator =(const Locale&) = delete;
Locale&operator =(Locale&&) = delete;
using ptr = std::shared_ptr<Locale>;
static const std::collate<char>& collation();
static ptr GetInstance();
virtual const std::string &CurrentLocale() const = 0;
virtual Collator::ptr GetCollator() = 0;
private:
static ptr g_instance;
};
}
+6 -6
View File
@@ -21,7 +21,7 @@
#include "PluginHost.hpp"
#include <lilv/lilv.h>
#include <stdexcept>
#include <locale>
#include "Locale.hpp"
#include <string_view>
#include "Lv2Log.hpp"
#include <functional>
@@ -461,16 +461,16 @@ void PluginHost::Load(const char *lv2Path)
Lv2Log::info("lilv: " + s);
}
const auto &collation = std::use_facet<std::collate<char>>(std::locale());
auto compare = [&collation](
auto collator = Locale::GetInstance()->GetCollator();
auto compare = [&collator](
const std::shared_ptr<Lv2PluginInfo> &left,
const std::shared_ptr<Lv2PluginInfo> &right)
{
const char *pb1 = left->name().c_str();
const char *pb2 = right->name().c_str();
return collation.compare(
pb1, pb1 + left->name().size(),
pb2, pb2 + right->name().size()) < 0;
return collator->Compare(
left->name(),right->name())
< 0;
};
std::sort(this->plugins_.begin(), this->plugins_.end(), compare);
+11 -7
View File
@@ -18,6 +18,7 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "pch.h"
#include "Locale.hpp"
#include "Storage.hpp"
#include "AudioConfig.hpp"
#include "PiPedalException.hpp"
@@ -40,7 +41,6 @@ const char *BANKS_FILENAME = "index.banks";
#define USER_SETTINGS_FILENAME "userSettings.json";
Storage::Storage()
: locale("en_US.UTF-8")
{
SetConfigRoot("~/var/Config");
SetDataRoot("~/var/PiPedal");
@@ -1525,9 +1525,10 @@ std::vector<std::string> Storage::GetFileList(const UiFileProperty &fileProperty
}
// sort lexicographically
auto collator = Locale::GetInstance()->GetCollator();
std::sort(result.begin(), result.end(), [this](const std::string&left,const std::string&right) {
return this->locale(left,right) < 0;
std::sort(result.begin(), result.end(), [&collator](const std::string&left,const std::string&right) {
return collator->Compare(left,right) < 0;
});
return result;
}
@@ -1606,12 +1607,14 @@ std::vector<FileEntry> Storage::GetFileList2(const std::string &relativePath,con
// sort lexicographically
std::sort(result.begin(), result.end(),[this](const FileEntry&l, const FileEntry&r) {
auto collator = Locale::GetInstance()->GetCollator();
std::sort(result.begin(), result.end(),[&collator](const FileEntry&l, const FileEntry&r) {
if (l.isDirectory_ != r.isDirectory_)
{
return l.isDirectory_ > r.isDirectory_;
}
return this->locale(l.filename_,r.filename_);
return collator->Compare(l.filename_,r.filename_) < 0;
});
return result;
@@ -1813,10 +1816,11 @@ void Storage::FillSampleDirectoryTree(FilePropertyDirectoryTree*node, const std:
node->children_.push_back(std::move(childTree));
}
}
auto collator = Locale::GetInstance()->GetCollator();
std::sort(node->children_.begin(),node->children_.end(),
[this](const FilePropertyDirectoryTree::ptr&left,const FilePropertyDirectoryTree::ptr&right)
[&collator](const FilePropertyDirectoryTree::ptr&left,const FilePropertyDirectoryTree::ptr&right)
{
return this->locale(left->directoryName_,right->directoryName_);
return collator->Compare(left->directoryName_,right->directoryName_) < 0;
});
}
FilePropertyDirectoryTree::ptr Storage::GetFilePropertydirectoryTree(const UiFileProperty&uiFileProperty) const
-2
View File
@@ -30,7 +30,6 @@
#include "WifiDirectConfigSettings.hpp"
#include "FileEntry.hpp"
#include <map>
#include <locale>
#include "FilePropertyDirectoryTree.hpp"
@@ -67,7 +66,6 @@ struct PluginPresetValues {
class Storage {
private:
std::locale locale;
std::filesystem::path dataRoot;
std::filesystem::path configRoot;
BankIndex bankIndex;
+12 -1
View File
@@ -19,7 +19,7 @@
#include "pch.h"
#include "util.hpp"
#include "Locale.hpp"
#include "AudioConfig.hpp"
#include "WebServer.hpp"
#include <iostream>
@@ -230,6 +230,17 @@ int main(int argc, char *argv[])
try
{
{
auto locale = Locale::GetInstance();
Lv2Log::info(SS("Locale: " << locale->CurrentLocale()));
try {
auto collator = locale->GetCollator();
} catch (std::exception&e)
{
Lv2Log::error(e.what());
return EXIT_SUCCESS; //tell systemd not to auto-restart.
}
}
PiPedalModel model;
model.Init(configuration);