15 #include <CoreFoundation/CoreFoundation.h>
16#elif defined(__unix__) || defined(__linux__)
46template <
typename... Args>
47struct is_tuple<std::tuple<Args...>> : std::true_type {};
66template <
typename Base,
typename Derived>
78template<LocaleInterface T>
116 template <DerivedFrom<T>... T_Child>
119 (this->setSupportedLocale<T_Child>(), ...);
135 template <IsTuple T_Tuple>
138 std::apply([
this](
auto... locals) {
139 (this->setSupportedLocale<std::decay_t<
decltype(locals)>>(), ...);
154 if (_supportedLocales.empty())
157 if (!_systemCode.empty() &&
setLocale(_systemCode))
162 _locale = _supportedLocales.begin()->second.get();
172 auto it = _supportedLocales.find(code);
174 if (it != _supportedLocales.end()) {
175 _locale = it->second.get();
191 std::string _systemCode;
192 T* _locale =
nullptr;
193 std::unordered_map<std::string, std::unique_ptr<T>> _supportedLocales;
211 void setSystemCode() {
212 #if defined(__APPLE__)
213 CFLocaleRef locale = CFLocaleCopyCurrent();
214 if (!locale) _systemCode =
"en";
216 CFStringRef identifier = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleIdentifier);
218 char buffer[16] = {0};
219 if (CFStringGetCString(identifier, buffer,
sizeof(buffer), kCFStringEncodingUTF8)) {
221 _systemCode = std::string(buffer, 2);
224 #elif defined(__unix__) || defined(__linux__)
227 std::string name = loc.name();
228 if (!name.empty() && name !=
"C" && name !=
"POSIX")
229 _systemCode = name.substr(0, 2);
244 template <DerivedFrom<T> T_Child>
245 void setSupportedLocale() {
246 auto newInstance = std::make_unique<T_Child>();
247 std::string key = newInstance->languageCode();
248 _supportedLocales[key] = std::move(newInstance);
Internationalization manager for a specific locale type.
Definition I18n.hpp:37
void setSupportedLocales()
Register a list of supported locales using template parameter pack.
Definition I18n.hpp:117
T * getLocale() const
Get the currently selected locale instance.
Definition I18n.hpp:186
void setDefault()
Sets the default locale to use if no other locale is selected.
Definition I18n.hpp:106
I18n & operator=(const I18n &)=delete
delete Move constructor
bool setLocale(const std::string &code)
Select a specific locale by code.
Definition I18n.hpp:124
static I18n< T > & getInstance()
Get the I18n singleton instance.
Definition I18n.hpp:88
I18n(const I18n &)=delete
delete Copy constructor
DerivedFrom is accept supportedLocal class only.
Definition I18n.hpp:67
Using is_tuple specialisation this concept verify if it's a tuple.
Definition I18n.hpp:58
Detect whether a type is a std::tuple or not.
Definition TypeTraits.hpp:79