Skip to content

Instantly share code, notes, and snippets.

@narikbi
Last active February 2, 2023 03:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save narikbi/352e93e446e8b1faf283 to your computer and use it in GitHub Desktop.
Save narikbi/352e93e446e8b1faf283 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSBundle (Language)
+(void)setLanguage:(NSString*)language;
@end
#import "NSBundle+Language.h"
#import <objc/runtime.h>
static const char _bundle=0;
@interface BundleEx : NSBundle
@end
@implementation BundleEx
- (NSString*)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
NSBundle* bundle=objc_getAssociatedObject(self, &_bundle);
return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}
@end
@implementation NSBundle (Language)
+(void)setLanguage:(NSString*)languaged
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
object_setClass([NSBundle mainBundle],[BundleEx class]);
});
objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@hck424
Copy link

hck424 commented May 29, 2019

Tank you.
LocalizedString is very good.
But how Localized image convert?
help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment