Localization of IOS applications

Source: Internet
Author: User

Why the application itself? Because normal localization is based on the current settings of the device. In practice, some applications often require localization that is not related to device settings. For example, in a game, a player can select the language of the game, but does not change the language of the device.

To meet this kind of requirement, we need to try to implement it in the near future. We can still use it after testing and write it down for the time being. If you have a better or simpler method, I hope you will not be enlightened.

In this example, xcode 4.2 is used.

A simple application localization class:

#import <Foundation/Foundation.h>@interface XUI_language_Base : NSObject{    NSBundle    *language_bundle;    NSString    *language_path;    NSArray     *language_array;}@property (nonatomic,retain)    NSBundle    *language_bundle;@property (nonatomic,retain)    NSString    *language_path;@property (nonatomic,retain)    NSArray     *language_array;-(void)initialize;-(void)setAppLanguage:(NSString *)language_temp;-(NSString *)getAppLanguageString:(NSString *)string_key;-(UIImage *)getAppLanguageImage:(NSString *)imageName;@end

#import "XUI_language_Base.h"@implementation XUI_language_Base@synthesize language_array;@synthesize language_path;@synthesize language_bundle;-(void)dealloc{    [language_bundle    release];    [language_array     release];    [language_path     release];        [super  dealloc];}-(void)initialize{    self.language_array = [ [NSArray alloc] initWithObjects:                                                      @"en",                           @"zh-Hans",                                                      nil];        BOOL isLanguageSetted = YES;        //todo get app config find the switch    //end do        NSString *language_select;        if (isLanguageSetted) {        // todo get the language of config                language_select = [language_array objectAtIndex:0];                //end do                    } else {        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];         NSArray *languages = [defaults objectForKey:@"AppleLanguages"];         language_select = [languages objectAtIndex:0];                BOOL isSupported = NO;        //check the language of locale isSupported                for(NSString *lan_temp in language_array){            if ( [lan_temp isEqualToString:language_select] ) {                isSupported = YES;                break;            }        }                if ( NO == isSupported ) {            language_select = [language_array objectAtIndex:0];        }    }        self.language_path = [ [NSBundle mainBundle] pathForResource:language_select ofType:@"lproj"];        self.language_bundle = [NSBundle bundleWithPath:self.language_path];}-(void)setAppLanguage:(NSString *)language_select{    BOOL isSupported = NO;    //check the language of locale isSupported        for(NSString *lan_temp in language_array){        if ( [lan_temp isEqualToString:language_select] ) {            isSupported = YES;            break;        }    }        if ( NO == isSupported ) {        language_select = [language_array objectAtIndex:0];    }        self.language_path = [ [NSBundle mainBundle] pathForResource:language_select ofType:@"lproj"];          self.language_bundle = [NSBundle bundleWithPath:self.language_path];          //todo set the app language and the switch value is yes        //end do}-(NSString *)getAppLanguageString:(NSString *)string_key{    return [self.language_bundle localizedStringForKey:string_key                                                 value:string_key table:nil];}-(UIImage *)getAppLanguageImage:(NSString *)imageName{    NSString *image_path = [ self.language_bundle pathForResource:imageName ofType:@"png"];            // need the name with type    // NSString *image_path = [ NSString stringWithFormat:@"%@/%@",self.language_path,imageName];            NSLog(@"img path %@",image_path);            return [UIImage imageWithContentsOfFile:image_path];}@end

Some functions can be implemented using the class method without the instance method, but I personally think it is more comfortable to store some paths. You can create an object in application initialization and call the initialize method. Related configurations are stored in local files of the application. The specific implementation methods are also various. My project is a program, and some access settings call the interfaces provided by others, that is, the above various todo. In addition, after the language is set during the runtime, the current displayed content needs to be processed in real time.

Reprinted please indicate from: http://blog.csdn.net/zhao_yin

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.