IOS_分享本地化语言封装好的类

本文来自 【Null】老早就发给我了,迟迟没有发


直接上.h文件

//
//  Language.h
//  Crocodile
//
//  Created by Mac on 11-4-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface Language : NSObject {

}

+(void)initialize;
+(void)setLanguage:(NSString *)l;
+(NSString *)get:(NSString *)key alter:(NSString *)alternate;
+(NSString *)get:(NSString *)key;
+(NSString *)getLanguage;
+(NSString *)getLanguageName;



+(NSString *)getULanguage;
@end



.m文件

//
//  Language.m
//  Crocodile
//
//  Created by Mac on 11-4-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Language.h"


@implementation Language

static NSBundle *bundle = nil;
//拿到当前系统的语言,并设置当前程序语言
+(void)initialize{
	NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *current = [[defs objectForKey:@"setting"] objectForKey:@"user_lang"];//用户选择的语言
    if(current==nil){
        NSArray *languages = [defs objectForKey:@"AppleLanguages"];//语言数组
        current = [[languages objectAtIndex:0] retain];
    }
    NSString *path=[[NSBundle mainBundle] pathForResource:current ofType:@"lproj"];
	bundle = [[NSBundle bundleWithPath:path] retain];
}

+(void)setLanguage:(NSString *)l{
	NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *setting=[NSMutableDictionary dictionaryWithDictionary:[defs objectForKey:@"setting"]];
    [setting setObject:l forKey:@"user_lang"];
    [defs setObject:setting forKey:@"setting"];
    
    NSString *path=[[NSBundle mainBundle] pathForResource:l ofType:@"lproj"];
	bundle = [[NSBundle bundleWithPath:path] retain];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate{
    return [bundle localizedStringForKey:key value:alternate table:nil];
}
+(NSString *)get:(NSString *)key{
    return [Language get:key alter:nil];
}
+(NSString *)getLanguage{
    return [bundle bundlePath];
}
+(NSString *)getLanguageName{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *current = [[defs objectForKey:@"setting"] objectForKey:@"user_lang"];//用户选择的语言
    if(current==nil){
        NSArray *languages = [defs objectForKey:@"AppleLanguages"];//语言数组
        current = [[languages objectAtIndex:0] retain];
    }
    return current;
}

+(NSString *)getULanguage{
    
//      return @"cn";
    
    if ([[Language getLanguageName] isEqualToString:@"zh-Hans"]) {
            return @"cn";
    }else{
            return @"en";    
    }
}
@end


小编:

你们一个个都木有给我完整Demo,玩死我了。

你可能感兴趣的:(ios,本地化)