Resolved How to localize plists?

Discussion in 'iOS Programming' started by Icarox, Oct 14, 2012.

  1. Icarox, Oct 14, 2012
    Last edited: Oct 16, 2012

    Icarox macrumors newbie

    Joined:
    Oct 13, 2012
    #1
    Hi I'm trying to localize an app; database is stored on external server www.XXXXX.com/MyAnnotations_es.plist. Actually I've got two plists MyAnnotations_es.plist and MyAnnotations_en.plist



    In Localizable.strings (Spanish) I've got:



    Code:
    [COLOR="Navy"]"MyAnnotations_es.plist" = "MyAnnotations_es.plist";[/COLOR]
    
    and in Localizable.strings (English) I've got:



    Code:
    [COLOR="Navy"]"MyAnnotations_en.plist" = "MyAnnotations_en.plist";[/COLOR]
    

    On the ViewController:



    Code:
    [COLOR="Navy"]- (void)downloadPlist {
        NSLog(@"Download in progress...");
        
        [refreshPlist release];
        
        
        deletePlist.enabled = NO;
        progressView.alpha = 1.0;
        pushToOpen.hidden = YES;
        pushToOpenText.alpha = 0.0;
        
        // Here we're downloading the .plist file from a server to the app's Documents Directory.
        // Create file manager
        fileManager = [NSFileManager defaultManager];
        
        // Point to Document directory
        documentsDirectory = [NSHomeDirectory()
                              stringByAppendingPathComponent:@"Documents"];
        
        documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        
        NSString *filename = NSLocalizedString(@"MyAnnotations_es.plist", @"filename for annotations");
        
        
        filePath = [documentsDirectory stringByAppendingPathComponent:filename];
        
        NSString *urlStr = [@"http://www.XXXXX.com/" stringByAppendingString:filename];
        
        
        NSURL *url;
        url = [NSURL URLWithString:urlStr];
        
        request = [ASIHTTPRequest requestWithURL:url];
        [request setShowAccurateProgress:YES];
        [request setDownloadDestinationPath:filePath];
        [request setDownloadProgressDelegate:progressView];
        [request setDelegate:self];
        [request startAsynchronous];
    }[/COLOR]
    
    With this, when I switch to Spanish language on my device shows MyAnnotations_es.plist database. And when English language is switched it shows MyAnnotations_es.plist.

    I'd like to show MyAnnotations_en.plist



    How can I do it?



    Thanks in advance ;)
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You need to use the same key for both Localizable.strings.
     
  3. Icarox thread starter macrumors newbie

    Joined:
    Oct 13, 2012
    #3
    Hi Dejo,

    I tried so, using the same key

    Localizable.strings (Spanish):

    Code:
    "MyAnnotations_es.plist" = "MyAnnotations_en.plist";
    Localizable.strings (English):

    Code:
    "MyAnnotations_es.plist" = "MyAnnotations_en.plist";

    And in sànish language shows MyAnnotations_es.plist data and switching my device into English shows nothing (blank list) it should show the other plist but it doesn't.

    Antother thing is that in the view controller where the list and pin maps is shown has this code:

    Code:
    // Fetching annotations from our .plist file.
    - (void)loadAnnotations {
        // Retrieve path of plist file and populate relevant types with its information.
        
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        
        myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"MyAnnotations_es.plist"];
        // Uncomment the following line if you want to load annotations from a plist file, in your app bundle instead of a server, and then, comment the line above.
         //myPathDocs = [[NSBundle mainBundle] pathForResource:@"MyAnnotations" ofType:@"plist"];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs]) {
            NSLog(@"No such file.");
        }      
    
    and

    Code:
    // Loading the .plist file that has been downloaded in MainViewController.
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        
        myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"MyAnnotations_es.plist"];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
        {
            NSLog(@"File doesn't exist.");
        }  
        
    
    Do you thing that I should write the
    Code:
    NSLocalizedString(@"MyAnnotations_es.plist", @"filename for annotations")
    in the whole project where "MyAnnotations_es.plist" appears?

    Thanks
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Do your ASIHTTPRequest delegate methods report any errors?

    Anywhere where you want the string returned to be dependent on the locale, yes.
     
  5. Icarox thread starter macrumors newbie

    Joined:
    Oct 13, 2012
    #5
    That was the key ;)

    I forgot to put
    Code:
    NSLocalizedString(@"MyAnnotations_es.plist", @"filename for annotations")
    in anywhere to change teh string from one language to another.

    And.... as you said,
    Now plists are shown in their proper language ;)

    Thanks for all your help Dejo.
    Now my app will be localizable :D

    [​IMG]
    [​IMG]

    I guess we can close this thread. Solved!!
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Just mark it Resolved.
     

Share This Page