【问题标题】:NSLocalizedStringFromTable doesn't workNSLocalizedStringFromTable 不起作用
【发布时间】:2014-02-06 18:22:25
【问题描述】:

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

Xcode 中的 Profile.strings:

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

波兰语:

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

"emailAddress.placeholder" = "Adres email";

"phoneNumber.placeholder" = "Numer telefonu";

英语:

"fullName.placeholder" = "Full name";

"emailAddress.placeholder" = "Email address";

"phoneNumber.placeholder" = "Phone number";

为了获得我调用的价值:

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

无论何时我调用它,我都会从 Profile.strings (Polish) 获得价值

我做错了什么?

【问题讨论】:

  • 通过从设备/模拟器中删除应用程序并重新启动 xcode 重试
  • 只是为了更多的理解阅读这个cocoawithlove.com/2011/04/…
  • 我知道NSLocalizedString() 是如何工作的,但我不知道为什么NSLocalizedStringFromTable() 不起作用。
  • @freenalcer,谢谢! Reset Content and Settings。请在单独的帖子中添加答案,我会标记它。

标签: ios xcode localization nslocalizedstring localizable.strings


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    也许您没有正确更改语言。尝试在代码中进行。像这样:

    - (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"];
    }
    

    然后你就可以了

    [self setLanguage:@"en"];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多