Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Locale.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_Locale_h_
5#define _Stroika_Foundation_Common_Locale_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <locale>
10#include <vector>
11
13#include "Stroika/Foundation/Common/Common.h"
14#include "Stroika/Foundation/Execution/Exceptions.h"
15
16/**
17 * \file
18 *
19 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
20 *
21 * TODO:
22 * @todo I think GetPlatformDefaultLocale() can be optimized to use a static copy of locale("") since
23 * I believe C++ now guarantees multithreaded safe static construction and safe read only
24 * access to objects (even from multiple reader threads).
25 *
26 * @todo verify and document if for windows this is LOCALE_USER_DEFAULT or LOCALE_SYSTEM_DEFAULT.
27 * SB USER!
28 *
29 * @todo reconsider if GetPlatformDefaultLocale SB inlined, since calls inline function that seems relatively
30 * big and I think maybe OK to cache value (static).
31 *
32 * @todo Enhance FindLocaleName() to make second param - optional (territory)
33 *
34 * @todo Consider the idea of a thread-local current locale (locale()). This would be handy
35 * for server applications like HealthFrameWorks Server which might get a locale
36 * from the client connection, for use in generating reports etc.
37 *
38 * @todo rewrite GetAvailableLocales and FindLocaleName() to look at source code for
39 * locale -a, and see what it does on POSIX systems, and to use EnumSystemLocales()
40 * and mapping to locale names, on windows platform (or better strategy if I can find it).
41 *
42 */
43
45
46 /**
47 */
48 class LocaleNotFoundException : public Execution::RuntimeErrorException<> {
49 public:
50 LocaleNotFoundException (const optional<Characters::String>& iso2LetterLanguageCode = {},
51 const optional<Characters::String>& iso2LetterTerritoryCode = {});
52
53 public:
54 static const LocaleNotFoundException kThe;
55 };
56 inline const LocaleNotFoundException LocaleNotFoundException::kThe;
57
58 /**
59 * In C++, the default locale is "C" (aka locale::classic ()), not the one
60 * inherited from the OS.
61 *
62 * Its not hard to get/set the one from the OS, but I've found it not well documented,
63 * so this is intended to make it a little easier/more readable.
64 */
66
67 /**
68 * \brief Set the operating system locale into the current C++ locale used by locale
69 * functions (and most locale-dependent stroika functions).
70 *
71 * In C++, the default locale is "C", not the one inherited from the OS.
72 * Its not hard to get/set the one from the OS, but I've found it not well documented,
73 * so this is intended to make it a little easier/more readable.
74 *
75 */
77
78 /**
79 * \brief List all installed locale names (names which can be passed to std::locale::CTOR)
80 *
81 * I'm quite surprised this appears so hard to to in stdC++. I must be missing something...
82 *
83 * @todo NYI really - hacked
84 */
85 vector<Characters::String> GetAvailableLocales ();
86
87 /**
88 * \brief Not all systems appear to follow the same naming conventions for locales, so help lookup
89 *
90 * Not all systems appear to follow the same naming conventions for locales, so provide a handy
91 * lookup function.
92 *
93 * This will throw an exception if no matching locale is found
94 */
95 Characters::String FindLocaleName (const Characters::String& iso2LetterLanguageCode, const Characters::String& iso2LetterTerritoryCode);
96
97 /**
98 * \brief Not all systems appear to follow the same naming conventions for locales, so help lookup
99 *
100 * Not all systems appear to follow the same naming conventions for locales, so provide a handy
101 * lookup function.
102 *
103 * This will return nullopt if no matching locale is found
104 */
105 optional<Characters::String> FindLocaleNameQuietly (const Characters::String& iso2LetterLanguageCode, const Characters::String& iso2LetterTerritoryCode);
106
107 /**
108 * \brief Find the locale matching these properties (for exception trying)
109 *
110 * This will return a valid locale object with the prescribed properties, or it will raise
111 * an exception.
112 */
113 locale FindNamedLocale (const Characters::String& iso2LetterLanguageCode, const Characters::String& iso2LetterTerritoryCode);
114
115 /**
116 */
117 optional<locale> FindNamedLocaleQuietly (const Characters::String& iso2LetterLanguageCode, const Characters::String& iso2LetterTerritoryCode);
118
119 /**
120 * Temporarily use the given argument locale.
121 *
122 * \code
123 * ScopedUseLocale useLocaleTmp{FindNamedLocaleQuietly("en", "us")}; // use the english US locale, if its available
124 * do stuff with us locale (if available)
125 * \endcode
126 *
127 * \code
128 * ScopedUseLocale useLocaleTmp{FindNamedLocale("en", "us")}; // use the english US locale, or throw
129 * do stuff with us locale
130 * \endcode
131 */
133 private:
134 optional<locale> fPrev_;
135
136 public:
137 /**
138 * if locale l provided, its used for the lifetime of the ScopedUseLocale locale (and on destructor the old locale is restored)
139 */
140 ScopedUseLocale () = delete;
141 ScopedUseLocale (const ScopedUseLocale&) = delete;
142 ScopedUseLocale (const optional<locale>& l);
144 };
145
146}
147
148/*
149 ********************************************************************************
150 ***************************** Implementation Details ***************************
151 ********************************************************************************
152 */
153#include "Locale.inl"
154
155#endif /*_Stroika_Foundation_Common_Locale_h_*/
Characters::String FindLocaleName(const Characters::String &iso2LetterLanguageCode, const Characters::String &iso2LetterTerritoryCode)
Not all systems appear to follow the same naming conventions for locales, so help lookup.
Definition Locale.cpp:80
optional< Characters::String > FindLocaleNameQuietly(const Characters::String &iso2LetterLanguageCode, const Characters::String &iso2LetterTerritoryCode)
Not all systems appear to follow the same naming conventions for locales, so help lookup.
Definition Locale.cpp:88
vector< Characters::String > GetAvailableLocales()
List all installed locale names (names which can be passed to std::locale::CTOR)
Definition Locale.cpp:65
locale GetPlatformDefaultLocale()
Definition Locale.inl:13
locale FindNamedLocale(const Characters::String &iso2LetterLanguageCode, const Characters::String &iso2LetterTerritoryCode)
Find the locale matching these properties (for exception trying)
Definition Locale.cpp:143
void UsePlatformDefaultLocaleAsDefaultLocale()
Set the operating system locale into the current C++ locale used by locale functions (and most locale...
Definition Locale.cpp:38