diff --git a/packages/apple-llm/ios/utils/AppleUtils.mm b/packages/apple-llm/ios/utils/AppleUtils.mm index 5b656b70b..7221a0203 100644 --- a/packages/apple-llm/ios/utils/AppleUtils.mm +++ b/packages/apple-llm/ios/utils/AppleUtils.mm @@ -29,8 +29,19 @@ + (NSString *)moduleName { return std::make_shared(params); } +// A BCP 47 code such as en-US or zh-Hans-CN. The locale identifier can carry +// keywords such as "@rg=plzzzz" (when language and region differ), which +// AVSpeechSynthesisVoice and other APIs taking a language code do not accept - (NSString *)getCurrentLocale { - return [NSLocale currentLocale].localeIdentifier; + NSLocale *locale = [NSLocale currentLocale]; + NSMutableArray *parts = [NSMutableArray arrayWithObject:locale.languageCode]; + if (locale.scriptCode != nil) { + [parts addObject:locale.scriptCode]; + } + if (locale.countryCode != nil) { + [parts addObject:locale.countryCode]; + } + return [parts componentsJoinedByString:@"-"]; } @end