summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe Moon <joe@xoxomoon.com>2018-05-08 10:28:21 -0700
committerJoe Wilm <jwilm@users.noreply.github.com>2018-05-14 12:37:13 -0700
commit24e8a19e7d981fbc8853891ad4a17a9de2ac36bd (patch)
treeb93ac77c09e27b0aa02c2dbc3904294d91834ea1
parent75529be729188062c4326046a38b763519b17e74 (diff)
handle older macOS versions
add logic to fallback to `localeIdentifier` if `currentLocale.languageCode` and `currentLocale.countryCode` are not available.
-rw-r--r--src/locale.rs37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/locale.rs b/src/locale.rs
index 4786b4db..c9184e88 100644
--- a/src/locale.rs
+++ b/src/locale.rs
@@ -23,20 +23,35 @@ use std::env;
use objc::runtime::{Class, Object};
pub fn set_locale_environment() {
- let (language_code, country_code) = unsafe {
+ let locale_id = unsafe {
let locale_class = Class::get("NSLocale").unwrap();
let locale: *const Object = msg_send![locale_class, currentLocale];
- msg_send![locale_class, release];
- let language_code: *const Object = msg_send![locale, languageCode];
- let country_code: *const Object = msg_send![locale, countryCode];
- msg_send![locale, release];
- let language_code_str = nsstring_as_str(language_code).to_owned();
- msg_send![language_code, release];
- let country_code_str = nsstring_as_str(country_code).to_owned();
- msg_send![country_code, release];
- (language_code_str, country_code_str)
+ let _ : () = msg_send![locale_class, release];
+ // `localeIdentifier` returns extra metadata with the locale (including currency and
+ // collator) on newer versions of macOS. This is not a valid locale, so we use
+ // `languageCode` and `countryCode`, if they're available (macOS 10.12+):
+ // https://developer.apple.com/documentation/foundation/nslocale/1416263-localeidentifier?language=objc
+ // https://developer.apple.com/documentation/foundation/nslocale/1643060-countrycode?language=objc
+ // https://developer.apple.com/documentation/foundation/nslocale/1643026-languagecode?language=objc
+ let is_language_code_supported: bool = msg_send![locale, respondsToSelector:sel!(languageCode)];
+ let is_country_code_supported: bool = msg_send![locale, respondsToSelector:sel!(countryCode)];
+ let locale_id = if is_language_code_supported && is_country_code_supported {
+ let language_code: *const Object = msg_send![locale, languageCode];
+ let country_code: *const Object = msg_send![locale, countryCode];
+ let language_code_str = nsstring_as_str(language_code).to_owned();
+ let _ : () = msg_send![language_code, release];
+ let country_code_str = nsstring_as_str(country_code).to_owned();
+ let _ : () = msg_send![country_code, release];
+ format!("{}_{}.UTF-8", &language_code_str, &country_code_str)
+ } else {
+ let identifier: *const Object = msg_send![locale, localeIdentifier];
+ let identifier_str = nsstring_as_str(identifier).to_owned();
+ let _ : () = msg_send![identifier, release];
+ identifier_str
+ };
+ let _ : () = msg_send![locale, release];
+ locale_id
};
- let locale_id = format!("{}_{}.UTF-8", &language_code, &country_code);
// check if locale_id is valid
let locale_c_str = CString::new(locale_id.to_owned()).unwrap();
let locale_ptr = locale_c_str.as_ptr();