xcode NSLocalizedStringFromTable 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21610927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:37:42  来源:igfitidea点击:

NSLocalizedStringFromTable doesn't work

iosxcodelocalizationnslocalizedstringlocalizable.strings

提问by Tomasz Szulc

I'm trying to use NSLocalizedStringFromTablebut with no results. I've got created Profile.stringsfile, clicked localized, in project settings I add Polish and English languages, so my file has 2 "files" inside and I typed the same strings with other values but when I switch languages and restart app there is still one localization used (Polish).

我正在尝试使用NSLocalizedStringFromTable但没有结果。我已经创建了Profile.strings文件,单击了本地化,在项目设置中我添加了波兰语和英语,所以我的文件里面有 2 个“文件”,我用其他值输入了相同的字符串,但是当我切换语言并重新启动应用程序时,仍然有一个使用本地化(波兰语)。

Profile.strings in Xcode:

Xcode 中的 Profile.strings:

Profile.strings
    Profile.strings (Polish)
    Profile.strings (English)

Polish:

抛光:

"fullName.placeholder" = "Imie i nazwisko";

"emailAddress.placeholder" = "Adres email";

"phoneNumber.placeholder" = "Numer telefonu";

English:

英语:

"fullName.placeholder" = "Full name";

"emailAddress.placeholder" = "Email address";

"phoneNumber.placeholder" = "Phone number";

To get value I call:

为了获得价值,我打电话:

NSLocalizedStringFromTable(@"fullName.placeholder", @"Profile", @"");

Any time I call this I've got value from Profile.strings (Polish)

任何时候我打电话给这个我都有价值 Profile.strings (Polish)

What I'm doing wrong?

我做错了什么?

回答by freelancer

try to Reset Content and Settingsof simulator it will work (:

尝试重置模拟器的内容和设置它会起作用(:

回答by Darkslave

Maybe you're not changing the language correctly. Try doing it in code. Like this:

也许您没有正确更改语言。尝试在代码中做到这一点。像这样:

- (void) setLanguage:(NSString*) l{
    for (NSString *language1 in [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]) {
        NSBundle *bundle1 = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language1 ofType:@"lproj"]];
        NSLog(@"%@: %@", language1, NSLocalizedStringFromTableInBundle(@"left", @"Localizable", bundle1, nil));
    }
    NSBundle *bundle1 = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:l ofType:@"lproj"]];
    /*
   if ([l isEqualToString:@"en"]) {
       bundle1 = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"English" ofType:@"lproj"]];
   }
    */


    if (bundle1 == nil)
        //in case the language does not exists
        [self resetLocalization];
    else
        bundle = bundle1;
    NSMutableArray *langs = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]];
    [langs removeObject:l];
    NSMutableArray *newLangs = [NSMutableArray arrayWithObject:l];
    [newLangs addObjectsFromArray:langs];

    [[NSUserDefaults standardUserDefaults] setObject: newLangs forKey:@"AppleLanguages"];
}

And then you can do

然后你可以做

[self setLanguage:@"en"];

[self setLanguage:@"en"];