gpt4 book ai didi

macos - 关于CFLocaleCopyCurrent API返回值不正确

转载 作者:行者123 更新时间:2023-12-03 17:09:32 26 4
gpt4 key购买 nike

在我的工作场所,我们面临的情况是遵循下面的独立代码,

#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 beta 上为我们提供了不同的输出。

简而言之,这就是我们所做的。

在 10.12 机器上

1) 设置首选语言为 ru,区域为 RU

2)重新启动机器

3) 获取“defaults read -g AppleLocale”的输出以确保输出是{ RU_RU}

4) 编译代码,运行exe。我们得到的输出为 { ru_RU }。

然后,我们在 OS X 10.13(测试版)计算机上重复步骤 1) 到 3),然后在 10.13 计算机上运行相同的 exe(在 10.12 上创建,您可能会问为什么,这是因为我们的一些内部构建系统限制)我们得到的输出是“en_RU”,这是不正确的。

我们在这里遗漏了什么吗?或者这是 OS X 10.13(测试版)中的已知问题?如果是这样,我们该如何解决这个问题?

更新

我们还编写了以下 Objective-C 代码来使用 NSLocale 接口(interface),这也给了我们相同的结果,即 10.12 上的 ru_RU 和 10.13(测试版)上的 en_RU

#import <Foundation/Foundation.h>  
int main()
{
@autoreleasepool
{
NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);
}
}

最佳答案

我们得到不正确值的原因是我们的应用没有本地化到我们期望的区域设置,例如 ru。我们通过在应用的 Contents/Resources 目录中添加一个空的 ru.lproj 目录来纠正此问题,API 开始为我们提供正确的答案。

关于macos - 关于CFLocaleCopyCurrent API返回值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45189185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com