Friday, October 5, 2012

NSLocalisedString - Read from user created file.

static NSBundle *myBundle;

+ (NSString *)getLocalizedString:(NSString *)str{
  
    return NSLocalizedStringFromTableInBundle(str, nil, [MyBundle getMyResourceBundle],nil);
  
}

/** set the resource file as the file which is created by user in the location document/ . We just create a folder en.lproj and create a file 'Localizable.strings' inside the folder **/
+ (NSBundle *)getMyResourceBundle{
   
        if(myBundle == nil){
       
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *documentsDirectory = [FileUtils applicationDocumentsDirectory];
        documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"en.lproj/Localizable.strings"];
       

        BOOL isDire = NO;
       
       
        if([fileManager fileExistsAtPath:documentsDirectory isDirectory:&isDire])
        {
           
            myBundle =  [[NSBundle alloc] initWithPath:[documentsDirectory stringByDeletingLastPathComponent]];
        }
        if(myBundle == nil){
          
                myBundle = [NSBundle mainBundle];
          
        }
       
       
    }
   
    return myBundle;

}

/** if need to set english (or any specific) resource file always. The corresponding resource file should exist in our project **/
+ (NSBundle *)getMyResourceBundle{
   
    if(myBundle == nil){
                   NSString* path= [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];//set english
                    myBundle = [NSBundle bundleWithPath:path];
                   if(myBundle == nil){
     
                          myBundle = [NSBundle mainBundle];
                    }
     }
    
return myBundle;
}

No comments:

Post a Comment