2012-08-17 36 views
6

Tôi đang tạo ứng dụng Mac và tôi muốn bản địa hóa Nhãn của mình. Tôi nghĩ rằng một tập tin .strings sẽ là một lựa chọn tốt hơn. Nhưng tôi gặp sự cố khi đọc tệp .strings trong Mục tiêu-C. Tôi đang tìm một phương pháp đơn giản hơn.Đọc tập tin .strings trong Objective-C?

Đây là nội dung .string tập tin của tôi:

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

Tôi đã nhìn http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LoadingResources/Strings/Strings.html.

Đây là mã của tôi:

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

Nhưng chuỗi tt cho "LABEL_001", tôi muốn "Start MyApp"

Tôi đang làm gì sai?

+0

Bạn đã nhìn NSLocalizedString? – Luke

Trả lời

23

Một. Bạn phải đặt tên tệp là Localizable.strings trong thư mục <LANGUAGENAME>.lproj trong gói ứng dụng.

Hai. Sử dụng macro NSLocalizedString:

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

Ba. Nếu không có gì hoạt động, bạn có thể khởi tạo một NSDictionary sử dụng một tập tin chuỗi, như một tập tin chuỗi là một kiểu đặc biệt của plist:

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

hoạt động tuyệt vời của nó – 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"]; 

Tôi nghĩ rằng nó sẽ rất hữu ích cho bạn.

1

Ở đây mã của bạn

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

Vấn đề ở đây là một trong 2 Param "strFilePath", thay đổi nó để @ "Nhãn" vì vậy các mã trên sẽ trở thành,

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

Để tham khảo, dòng sau được sao chép từ Apple Documents về tên bảng, "Khi chỉ định giá trị cho thông số này, hãy bao gồm tên tệp không có đuôi mở rộng .strings."

hy vọng điều này sẽ hữu ích.

0

Simple Mã

Chỉ cần tạo ra một phương pháp như sau

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