i18n
Loading...
Searching...
No Matches
SystemCode.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <string>
16
17#if defined(__APPLE__)
18 #include <CoreFoundation/CoreFoundation.h>
19#elif defined(__unix__) || defined(__linux__)
20 #include <locale>
21#endif
22
23std::string getSystemCode() {
24 #if defined(__APPLE__)
25 CFLocaleRef locale = CFLocaleCopyCurrent();
26 if (!locale)
27 return "en"; // fallback
28
29 CFStringRef identifier = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleIdentifier);
30
31 char buffer[16] = {0};
32 if (CFStringGetCString(identifier, buffer, sizeof(buffer), kCFStringEncodingUTF8)) {
33 CFRelease(locale);
34 return std::string(buffer, 2); // first 2 letters
35 }
36 CFRelease(locale);
37 #elif defined(__unix__) || defined(__linux__)
38 try {
39 std::locale loc(""); // system locale
40 std::string name = loc.name(); // e.g., "fr_FR.UTF-8"
41 if (!name.empty() && name != "C" && name != "POSIX")
42 return name.substr(0, 2);
43 } catch (...) {}
44 #endif
45
46 return "en";
47}
std::string getSystemCode()
Definition SystemCode.hpp:23