Index: runtime/bin/platform_macos.cc |
diff --git a/runtime/bin/platform_macos.cc b/runtime/bin/platform_macos.cc |
index 0088956c210b0dfe7025b2869a9add8bd3f52959..f15ec3cc1482ce3a965e8838d374df39fcc10540 100644 |
--- a/runtime/bin/platform_macos.cc |
+++ b/runtime/bin/platform_macos.cc |
@@ -7,6 +7,8 @@ |
#include "bin/platform.h" |
+#include <CoreFoundation/CoreFoundation.h> |
+ |
#if !HOST_OS_IOS |
#include <crt_externs.h> // NOLINT |
#endif // !HOST_OS_IOS |
@@ -98,6 +100,60 @@ const char* Platform::LibraryExtension() { |
} |
+static const char* GetLocaleName() { |
+ CFLocaleRef locale = CFLocaleCopyCurrent(); |
+ CFLocaleIdentifier locale_id = CFLocaleGetIdentifier(locale); |
+ CFStringRef locale_string = reinterpret_cast<CFStringRef>(locale_id); |
+ CFIndex len = CFStringGetLength(locale_string); |
+ CFIndex max_len = |
+ CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; |
+ char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); |
+ ASSERT(result != NULL); |
+ bool success = |
+ CFStringGetCString(locale_string, result, max_len, kCFStringEncodingUTF8); |
+ CFRelease(locale); |
+ if (!success) { |
+ return NULL; |
+ } |
+ return result; |
+} |
+ |
+ |
+static const char* GetPreferredLanguageName() { |
+ CFArrayRef languages = CFLocaleCopyPreferredLanguages(); |
+ CFIndex languages_length = CFArrayGetCount(languages); |
+ if (languages_length < 1) { |
+ CFRelease(languages); |
+ return NULL; |
+ } |
+ CFTypeRef item = |
+ reinterpret_cast<CFTypeRef>(CFArrayGetValueAtIndex(languages, 0)); |
+ CFTypeID item_type = CFGetTypeID(item); |
+ ASSERT(item_type == CFStringGetTypeID()); |
+ CFStringRef language = reinterpret_cast<CFStringRef>(item); |
+ CFIndex len = CFStringGetLength(language); |
+ CFIndex max_len = |
+ CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; |
+ char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); |
+ ASSERT(result != NULL); |
+ bool success = |
+ CFStringGetCString(language, result, max_len, kCFStringEncodingUTF8); |
+ CFRelease(languages); |
+ if (!success) { |
+ return NULL; |
+ } |
+ return result; |
+} |
+ |
+ |
+const char* Platform::LocaleName() { |
+ // First see if there is a preferred language. If not, return the |
+ // current locale name. |
+ const char* preferred_langauge = GetPreferredLanguageName(); |
+ return (preferred_langauge != NULL) ? preferred_langauge : GetLocaleName(); |
+} |
+ |
+ |
bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) { |
return gethostname(buffer, buffer_length) == 0; |
} |