Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2172)

Unified Diff: runtime/bin/platform_macos.cc

Issue 2786183003: [dart:io] Reland: Adds Platform.localeName (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/platform_linux.cc ('k') | runtime/bin/platform_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
}
« no previous file with comments | « runtime/bin/platform_linux.cc ('k') | runtime/bin/platform_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698