gpt4 book ai didi

iOS - 应用程序中的语言切换器 |未将语言设置为英语(美国)时不显示内容

转载 作者:行者123 更新时间:2023-11-28 21:51:07 32 4
gpt4 key购买 nike

我有一个语言切换器,它工作得很好,直到我收到用户的邮件说我的应用程序无法加载任何内容。

经过一番交谈,用户告诉我他们将 iDevice 语言设置为英语(英国)。我告诉他们将其更改为英语(美国),然后该应用程序按预期运行。

现在

默认情况下,当设备语言不是英语或意大利语时,我的应用会返回英语(和内容),但我真的不明白为什么它在设置英语(美国)以外的语言时不起作用.

这是我用来加载“设置”页面的代码(一个 plist 文件)

+ (NSArray*)readSettingsPlist{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Settings.plist"];

NSBundle *bundle;

UALog(@"%@",[NSLocale preferredLanguages]);

if ([[NSLocale preferredLanguages] containsObject:@"en"] || [[NSLocale preferredLanguages] containsObject:@"it"]){
bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]];
}
else{
bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]];
}


if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [bundle pathForResource:@"Settings" ofType:@"plist"];
}

return [NSArray arrayWithContentsOfFile:plistPath];

}

在我的设备上,甚至在模拟器上,它都运行良好,但我想知道是否有任何方法可以改进这段代码,以允许设备语言不是英语(美国)的人正确加载内容。

提前致谢。

最佳答案

两个问题:

1) plist 如何在 NSDocumentDirectory 中结束?找到后复制到那里?为什么不直接使用 Main Bundle 中的文件?该文件将始终具有优先权,即使在应用启动后选择了不同的语言也是如此。

2) 如果 [[NSLocale preferredLanguages] objectAtIndex:0] 不是 @"en"@"it" 会发生什么?那么你可能有一个 nil 的包路径(除了 @"en"@"it" 之外,你没有提到其他 .lproj 目录。

我可能会这样做

for (NSString* lang in [NSLocale preferredLanguages])
{
bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]];
if (bundle) break;
}
//fallback to @"en" if bundle is still nil
if (!bundle)
{
bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]];
}

关于iOS - 应用程序中的语言切换器 |未将语言设置为英语(美国)时不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28298501/

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