2012-08-17 12 views
6

Sto creando un'app per Mac e desidero localizzare le mie etichette. Ho pensato che un file .strings sarebbe una scelta migliore. Ma ho problemi a leggere il file .strings in Objective-C. Sto cercando un metodo più semplice.Leggi il file .strings in Objective-C?

Questo è il mio contenuto .string del file:

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

ho già guardato http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LoadingResources/Strings/Strings.html.

Questo è il mio codice:

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

Ma il TT stringa dà "LABEL_001", voglio "Start MyApp"

Che cosa sto facendo di sbagliato?

+0

Hai guardato NSLocalizedString? – Luke

risposta

23

Uno. Devi nominare il tuo file Localizable.strings nella directory <LANGUAGENAME>.lproj nel pacchetto dell'app.

Due. Utilizzare la macro NSLocalizedString:

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

Tre. Se non funziona, è possibile inizializzare un NSDictionary utilizzando un file stringhe, come un file stringhe è un particolare tipo di plist:

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

funziona perfettamente – svmrajesh

2
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"]; 

penso che sarà utile a voi.

1

Qui il vostro codice

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

Il problema qui è il 2 ° Param "strFilePath", modificarlo a @ "Etichette" in modo che il codice di cui sopra sarebbe diventato,

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

Per riferimento, la seguente riga copiata da Apple Docs per quanto riguarda il nome della tabella, "Quando si specifica un valore per questo parametro, includere il nome file senza l'estensione .strings."

spero che questo aiuti.

0

Codice semplice

Basta creare un metodo come segue

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