You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Cocoa中如何根据区域代码获取本地化语言名称?

获取NSSpellChecker语言代码对应的本地化名称

你可以直接利用NSLocale类的API实现这个需求,系统拼写检查器面板的语言名称也是通过类似逻辑获取的:

  • 核心思路是用NSLocale的displayNameForKey:value:方法,将[NSSpellChecker availableLanguages]返回的语言/区域代码,转换为当前系统语言环境下的本地化名称。

示例代码:

// 获取所有可用拼写检查语言代码
NSArray *availableSpellLanguages = [NSSpellChecker availableLanguages];
// 使用当前系统的区域设置
NSLocale *currentSystemLocale = [NSLocale currentLocale];

// 遍历转换每个语言代码
for (NSString *langCode in availableSpellLanguages) {
    NSString *localizedLangName = [currentSystemLocale displayNameForKey:NSLocaleIdentifier value:langCode];
    // 输出结果:比如德语系统下"de"会得到"Deutsch","en_GB"会得到"Englisch (Vereinigtes Königreich)"
    NSLog(@"%@ -> %@", langCode, localizedLangName);
}

如果需要针对特定语言环境生成名称(而非当前系统语言),可以手动创建指定的NSLocale实例:

// 强制使用德语环境来获取语言名称
NSLocale *germanLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSString *enGBNameInGerman = [germanLocale displayNameForKey:NSLocaleIdentifier value:@"en_GB"];
// 结果为"Englisch (Vereinigtes Königreich)"

这个方法完全依赖系统内置的本地化映射表,和拼写检查器面板的显示逻辑保持一致,无需自行维护对照表。

内容的提问来源于stack exchange,提问作者fbitterlich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 10:56:14