Read .strings file in Objective-C?

前端 未结 4 1047
孤城傲影
孤城傲影 2021-02-08 10:14

I\'m creating a Mac app and I want to localize my Labels. I thought a .strings file would be a better choice. But I have trouble reading .strings file

4条回答
  •  庸人自扰
    2021-02-08 10:45

    Here the your code

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *strFilePath = [[NSBundle mainBundle] pathForResource:@"Labels" ofType:@"strings"];
    NSString *tt =NSLocalizedStringFromTableInBundle(@"LABEL_001",strFilePath,bundle, nil);
    NSLog(@"STRING ::: %@",tt);
    

    The problem here is the 2nd Param "strFilePath", change it to @"Labels" so the above code would become,

    NSString *tt =NSLocalizedStringFromTableInBundle(@"LABEL_001",@"Labels",bundle, nil);
    

    For reference, the following line copied from Apple Docs regarding table name, "When specifying a value for this parameter, include the filename without the .strings extension."

    hope this helps.

提交回复
热议问题