Zeus
Zeus

Reputation: 571

Read .strings file in Objective-C?

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 in Objective-C. I'm looking for a simpler method.

This is my .string file content:

"LABEL_001" = "Start MyApp";
"LABEL_002" = "Stop MyApp";
"LABEL_003" = "My AppFolder";
...

I have already looked at http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LoadingResources/Strings/Strings.html.

This is my code:

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

But the string tt gives "LABEL_001", I want "Start MyApp"

What am I doing wrong?

Upvotes: 11

Views: 9352

Answers (4)

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11227

Simple Code

Just create a method as follows

- (void)localisationStrings
{
    NSString* path = [[NSBundle mainBundle] pathForResource:@"localisation" ofType:@"strings"];
    NSDictionary *localisationDict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"\n %@",[localisationDict objectForKey:@"hello"]);
}

Upvotes: 0

Ahmed
Ahmed

Reputation: 11

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.

Upvotes: 1

Prasad G
Prasad G

Reputation: 6718

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Labels" ofType:@"strings"];
    NSData *plistData = [NSData dataWithContentsOfFile:path];
    NSString *error; NSPropertyListFormat format;

    NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:plistData
                                                          mutabilityOption:NSPropertyListImmutable
                                                                    format:&format
                                                          errorDescription:&error];
    NSString *stringname = [dictionary objectForKey:@"LABEL_001"];

I think it will be helpful to you.

Upvotes: 2

user529758
user529758

Reputation:

One. You have to name your file Localizable.strings in the <LANGUAGENAME>.lproj directory in the app bundle.

Two. Use the NSLocalizedString macro:

NSString *loc = NSLocalizedString(@"LABEL_001", nil);

Three. If nothing works, you can initialize an NSDictionary using a strings file, as a strings file is a special type of plist:

NSString *fname = [[NSBundle mainBundle] pathForResource:@"whatever" ofType:@"strings"];
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:fname];
NSString *loc = [d objectForKey:@"LABEL_001"];

Upvotes: 30

Related Questions