I'm creating an application where i need to list all the the supported languages from library into tableview. So that there is no need for the user to go to settings and change the language. if he wishes, he can change it directly from app. I have searched a lot but don't know whether its possible or not. So please can anyone help me with this.

Thanks in advance

有帮助吗?

解决方案

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];

This will give u languages preferred by Apple. But I dont know about supported languages. May be this is the list you are searching for

for (NSString *language in languages) {
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:language] ;
    displayname = [locale displayNameForKey:NSLocaleIdentifier value:language];
}

其他提示

You can generate pList files for different languages for application.

I'm explaining you sample code for localize language.

NSDictionary *dictLang;

-(void)localization_language{

    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *rootPath;

    if (lang == 1) {
       rootPath = [[NSBundle mainBundle] pathForResource:@"FRA" 
                                               ofType:@"plist"];
    }
    else if(lang == 0){
       rootPath = [[NSBundle mainBundle] pathForResource:@"ENG" 
                                               ofType:@"plist"];
    }

    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:rootPath];    
    dictLang = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
    NSLog(@"PLIST retrive:%@",plistXML);    

    if (!dictLang) {
       NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }
}

This code finds the pList file according to lang variable. 0 for France & 1 for English.

Then, set values from dictionary.

lbl1.text =[dictLang objectForKey:@"Key1"];
lbl2.text =[dictLang objectForKey:@"Key2"];

And it'll display values as per stored in pList file.

Hopefully it'll work for Language Globalization. Thanks.

Make a Language.plist file and make a dictionary of languages keep your default text as key

then define this macro in Defines.h

#define LocalizedString(key)     [[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Language" ofType:@"plist"]] valueForKey:[NSString stringWithFormat:@"%d",LANGUAGE_ID]] valueForKey:key]

Set your language id which is global programmatically and use LocalizedString(@"Hi") like this

according to lang Id selected translated value for key hi will be displayed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top