libosmscout 1.1.1
Loading...
Searching...
No Matches
Locale.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_LOCALE_H
2#define OSMSCOUT_LOCALE_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2019 Lukáš Karas
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
25
27#include <osmscout/log/Logger.h>
28
29#include <string>
30
31namespace osmscout {
32
33 class OSMSCOUT_API Locale CLASS_FINAL
34 {
35 private:
36 DistanceUnitSystem distanceUnits{DistanceUnitSystem::Metrics};
37 std::string decimalSeparator{"."};
38 std::string thousandsSeparator;
39
40 // use tiny, unbreakable space between value and unit.
41 // U+202F unicode is encoded as 0xE280AF in utf-8
42 // because unicode support in C++ is poor and platform dependent,
43 // we will use utf-8 bytes directly
44 std::string unitsSeparator{"\xE2\x80\xAF"};
45
46 public:
53 Locale() = default;
54
55 Locale(const DistanceUnitSystem &distanceUnits,
56 const std::string &decimalSeparator,
57 const std::string &thousandsSeparator);
58
59 Locale(const Locale&) = default;
60 Locale(Locale&&) = default;
61 ~Locale() = default;
62 Locale &operator=(const Locale &) = default;
63 Locale &operator=(Locale &&) = default;
64
66 {
67 return distanceUnits;
68 }
69
71 {
72 this->distanceUnits = units;
73 }
74
75 std::string GetDecimalSeparator() const
76 {
77 return decimalSeparator;
78 }
79
80 void SetDecimalSeparator(const std::string &separator)
81 {
82 this->decimalSeparator = separator;
83 }
84
85 std::string GetThousandsSeparator() const
86 {
87 return thousandsSeparator;
88 }
89
90 void SetThousandsSeparator(const std::string &separator)
91 {
92 this->thousandsSeparator = separator;
93 }
94
95 std::string GetUnitsSeparator() const
96 {
97 return unitsSeparator;
98 }
99
100 void SetUnitsSeparator(const std::string &separator)
101 {
102 this->unitsSeparator = separator;
103 }
104
105 public:
106
109 static Locale FromStdLocale(std::locale locale);
110
116 {
117 return FromStdLocale(std::locale(""));
118 }
119
125 {
126 try {
127 return ByEnvironment();
128 } catch (const std::runtime_error &e) {
129 log.Warn() << "Failed to get environment locale: " << e.what();
130 return Locale();
131 }
132 }
133 };
134}
135
136#endif // OSMSCOUT_LOCALE_H
#define OSMSCOUT_API
Definition CoreImportExport.h:45
Definition Area.h:88
DistanceUnitSystem GetDistanceUnits() const
Definition Locale.h:65
void SetDistanceUnits(const DistanceUnitSystem &units)
Definition Locale.h:70
std::string GetThousandsSeparator() const
Definition Locale.h:85
Locale(const DistanceUnitSystem &distanceUnits, const std::string &decimalSeparator, const std::string &thousandsSeparator)
Locale(Locale &&)=default
static Locale FromStdLocale(std::locale locale)
void SetDecimalSeparator(const std::string &separator)
Definition Locale.h:80
Locale & operator=(const Locale &)=default
void SetUnitsSeparator(const std::string &separator)
Definition Locale.h:100
void SetThousandsSeparator(const std::string &separator)
Definition Locale.h:90
std::string GetUnitsSeparator() const
Definition Locale.h:95
static Locale ByEnvironmentSafe()
Definition Locale.h:124
std::string GetDecimalSeparator() const
Definition Locale.h:75
Locale(const Locale &)=default
static Locale ByEnvironment()
Definition Locale.h:115
Locale & operator=(Locale &&)=default
OSMSCOUT_API Log log
Definition LoggerImpl.h:95
Definition Area.h:39
DistanceUnitSystem
Definition Distance.h:36