18 #include <CoreFoundation/CoreFoundation.h>
19#elif defined(__unix__) || defined(__linux__)
49template <
typename... Args>
50struct is_tuple<std::tuple<Args...>> : std::true_type {};
69template <
typename Base,
typename Derived>
81template<LocaleInterface T>
119 template <DerivedFrom<T>... T_Child>
122 (this->setSupportedLocale<T_Child>(), ...);
138 template <IsTuple T_Tuple>
141 std::apply([
this](
auto... locals) {
142 (this->setSupportedLocale<std::decay_t<
decltype(locals)>>(), ...);
157 if (_supportedLocales.empty())
160 if (!_systemCode.empty() &&
setLocale(_systemCode))
165 _locale = _supportedLocales.begin()->second.get();
175 auto it = _supportedLocales.find(code);
177 if (it != _supportedLocales.end()) {
178 _locale = it->second.get();
194 std::string _systemCode;
195 T* _locale =
nullptr;
196 std::unordered_map<std::string, std::unique_ptr<T>> _supportedLocales;
214 void setSystemCode() {
215 #if defined(__APPLE__)
216 CFLocaleRef locale = CFLocaleCopyCurrent();
217 if (!locale) _systemCode =
"en";
219 CFStringRef identifier = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleIdentifier);
221 char buffer[16] = {0};
222 if (CFStringGetCString(identifier, buffer,
sizeof(buffer), kCFStringEncodingUTF8)) {
224 _systemCode = std::string(buffer, 2);
227 #elif defined(__unix__) || defined(__linux__)
230 std::string name = loc.name();
231 if (!name.empty() && name !=
"C" && name !=
"POSIX")
232 _systemCode = name.substr(0, 2);
248 template <DerivedFrom<T> T_Child>
249 void setSupportedLocale() {
250 auto newInstance = std::make_unique<T_Child>();
251 std::string key = newInstance->languageCode();
252 _supportedLocales[key] = std::move(newInstance);
Internationalization manager for a specific locale type.
Definition I18n.hpp:40
void setSupportedLocales()
Register a list of supported locales using template parameter pack.
Definition I18n.hpp:120
T * getLocale() const
Get the currently selected locale instance.
Definition I18n.hpp:189
void setDefault()
Sets the default locale to use if no other locale is selected.
Definition I18n.hpp:109
I18n & operator=(const I18n &)=delete
delete Move constructor
bool setLocale(const std::string &code)
Select a specific locale by code.
Definition I18n.hpp:127
static I18n< T > & getInstance()
Get the I18n singleton instance.
Definition I18n.hpp:91
I18n(const I18n &)=delete
delete Copy constructor
DerivedFrom is accept supportedLocal class only.
Definition I18n.hpp:70
Using is_tuple specialisation this concept verify if it's a tuple.
Definition I18n.hpp:61
Detect whether a type is a std::tuple or not.
Definition TypeTraits.hpp:82