关于CFLocaleCopyCurrent API不正确的返回值

问题描述:

在我的工作,我们是在一个情况下按照以下独立代码,关于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]); 
    } 
} 

的我们得到不正确的值的原因是因为我们的应用没有被本地化到我们预期的地区,例如ru。 我们通过将一个空的ru.lproj目录添加到我们的应用程序的Contents/Resources目录中并且API开始为我们提供正确答案进行了更正。