在我的工作,我們是在一個情況下按照以下獨立代碼,關於CFLocaleCopyCurrent API不正確的返回值
#include <CoreFoundation/CoreFoundation.h>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <boost/cast.hpp>
// Reference release
struct reference_close
{
void operator()(const void *ref) const
{
CFRelease(static_cast<CFTypeRef>(ref));
}
}; // end of reference_close structure
typedef std::unique_ptr<const void, reference_close> reference_uptr;
std::string get_user_locale()
{
reference_uptr ref_ptr(CFLocaleCopyCurrent());
CFLocaleRef locale_ref(static_cast<CFLocaleRef>(ref_ptr.get()));
if (locale_ref == nullptr)
{
return std::string();
}
const size_t default_size(128);
std::vector<char> buff(default_size);
CFStringRef str_ref(CFLocaleGetIdentifier(locale_ref));
// CFStringRef str_ref((CFStringRef)CFLocaleGetValue(locale_ref,kCFLocaleLanguageCode));
if (str_ref != nullptr)
{
CFIndex len(CFStringGetLength(str_ref) + 1);
if (len > boost::numeric_cast<CFIndex>(default_size))
{
buff.resize(len);
}
buff[0] = 0;
if (!CFStringGetCString(str_ref, &buff[0], len, kCFStringEncodingISOLatin1))
{
return std::string();
}
}
return std::string(&buff[0]);
} // end of get_user_locale()
int main()
{
std::cout << "get_user_locale() : "<<get_user_locale() << std::endl;
return 0;
}
使我們對OS X 10.12和10.13測試版不同的輸出。
這就是我們簡單介紹的。
在10.12機
1)設置的偏好語言作爲Ru和區域作爲RU
2)重新啓動機器
3)獲取的輸出 「默認讀-g AppleLocale」 到確保輸出是 { ru_RU }
4)編譯代碼,運行exe。我們得到的輸出爲{ru_RU}。
然後,我們在OS X 10.13(beta)機器上重複步驟1)到3),然後在10.13上運行相同的exe(在10.12創建,您可能會問爲什麼,因爲我們的內部構建系統限制)機器和我們得到的輸出是「en_RU」,這是不正確的。
我們在這裏錯過了什麼嗎?或者這是OS X 10.13(測試版)中的一個已知問題?如果是這樣,我們如何解決這個問題?
UPDATE
我們還寫了以下的Objective-C代碼使用NSLocale接口和太給了我們同樣的結果,即ru_RU在10.12和en_RU在10.13(測試版)
#import <Foundation/Foundation.h>
int main()
{
@autoreleasepool
{
NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);
}
}