Index: runtime/bin/platform_macos.cc |
diff --git a/runtime/bin/platform_macos.cc b/runtime/bin/platform_macos.cc |
index 0088956c210b0dfe7025b2869a9add8bd3f52959..782a83c29188717e7dbeeac921180a16d7f62d92 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,59 @@ const char* Platform::LibraryExtension() { |
} |
+static const char* GetLocaleName() { |
+ CFLocaleRef locale = CFLocaleCopyCurrent(); |
+ CFStringRef locale_string = CFLocaleGetIdentifier(locale); |
+ 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; |
} |