Skip to content

Commit

Permalink
safe method for creating locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Karry committed Dec 23, 2023
1 parent ddd5c98 commit e78e0d9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public slots:
int textPixelSize=14;
int textPadding=4;

Locale locale=Locale::ByEnvironment();
Locale locale=Locale::ByEnvironmentSafe();
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public slots:
private:
QThread *thread;
SettingsRef settings;
DistanceUnitSystem units{Locale::ByEnvironment().GetDistanceUnits()}; // TODO: make possible to override
DistanceUnitSystem units{Locale::ByEnvironmentSafe().GetDistanceUnits()}; // TODO: make possible to override
DBThreadRef dbThread;
QTimer timer;
std::optional<Bearing> lastBearing;
Expand Down
2 changes: 1 addition & 1 deletion libosmscout-map/src/osmscoutmap/MapParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace osmscout {
warnObjectCountLimit(0),
warnCoordCountLimit(0),
showAltLanguage(false),
locale{Locale::ByEnvironment()}
locale{Locale::ByEnvironmentSafe()}
{
// no code
}
Expand Down
29 changes: 28 additions & 1 deletion libosmscout/include/osmscout/util/Locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <osmscout/system/Compiler.h>

#include <osmscout/util/Distance.h>
#include <osmscout/log/Logger.h>

#include <string>

Expand Down Expand Up @@ -102,7 +103,33 @@ namespace osmscout {
}

public:
static Locale ByEnvironment(std::locale locale = std::locale(""));

/** Creates Locale from provided std::locale
*/
static Locale FromStdLocale(std::locale locale);

/** Creates Locale defined by current environment
*
* @throw std::runtime_error when locale defined by environment is undefined
*/
static Locale ByEnvironment()
{
return FromStdLocale(std::locale(""));
}

/** Creates Locale defined by current environment,
* it is not throwing exception when environment locale is incorrect,
* it just return default Locale instead.
*/
static Locale ByEnvironmentSafe()
{
try {
return ByEnvironment();
} catch (const std::runtime_error &e) {
log.Warn() << "Failed to get environment locale: " << e.what();
return Locale();
}
}
};
}

Expand Down
4 changes: 2 additions & 2 deletions libosmscout/include/osmscout/util/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ namespace osmscout {
*
* Returned string is locale aware, UTF-8 encoded
*/
extern OSMSCOUT_API std::string ByteSizeToString(FileOffset size, const Locale &locale = Locale::ByEnvironment());
extern OSMSCOUT_API std::string ByteSizeToString(double size, const Locale &locale = Locale::ByEnvironment());
extern OSMSCOUT_API std::string ByteSizeToString(FileOffset size, const Locale &locale = Locale::ByEnvironmentSafe());
extern OSMSCOUT_API std::string ByteSizeToString(double size, const Locale &locale = Locale::ByEnvironmentSafe());

/**
* \ingroup Util
Expand Down
2 changes: 1 addition & 1 deletion libosmscout/src/osmscout/util/Locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace osmscout {
}
}

Locale Locale::ByEnvironment(std::locale cppLocale)
Locale Locale::FromStdLocale(std::locale cppLocale)
{
Locale locale;

Expand Down

0 comments on commit e78e0d9

Please sign in to comment.