我正在制作一个只使用自定义图形和叙述的互动儿童故事。
我有英文和西班牙文的所有图片。
我没有任何非自定义按钮,没有文本字段,没有单一的"标准" Apple app UI元素随处可见。一切都是自定义的PNG文件。
我只是在寻找一种允许用户从英语变为西班牙语的方法,反之亦然。
我已经创建了一个自定义英语和西班牙语按钮,按下后会突出显示。
应用程序将(从我读过的内容需要重新启动)然后应用程序将显示其西班牙语页面(以及嵌入在PNG中的西班牙语文本)。西班牙的叙述将随之而来。
用户可以随时选择点击英语语言按钮,如果需要,可以返回英语。
Tabtale"美人鱼公主"应用程序做得非常好,并且远远超过我的需要,但它是我所追求的一个很好的例子。您单击按钮,更改区域,重新启动应用程序,以及所选区域的所有自定义文本和旁白结果。
我只有两个区域(语言),所以我的需求要简单得多。
但是世界上究竟如何实现这一目标呢?
干杯!
答案 0 :(得分:3)
基本上我正在交换NSBundle的方法,该方法将由class Plan
has_many :plan_skills
has_many :activities, through: :plan_skills
end
class Skill
has_many :activities, through: plan_skills
has_many :plan_skills
end
class PlanSkill
belongs_to :plan
belongs_to :skill
has_many :activities
end
class Activity
belongs_to :plan_skill
end
通过使用NSBundle上的类别和一种名为 isa-swizzeling
一个NSBundle + Language.h
NSLocalizedString(…)
一个NSBundle + Language.m
#import <Foundation/Foundation.h>
@interface NSBundle (Language)
+(void)setLanguage:(NSString*)language;
@end
AppDelegate将收听#import "NSBundle+Language.h"
#import <objc/runtime.h>
static const char associatedLanguageBundle=0;
@interface PrivateBundle : NSBundle
@end
@implementation PrivateBundle
-(NSString*)localizedStringForKey:(NSString *)key
value:(NSString *)value
table:(NSString *)tableName
{
NSBundle* bundle=objc_getAssociatedObject(self, &associatedLanguageBundle);
return bundle ? [bundle localizedStringForKey:key
value:value
table:tableName] : [super localizedStringForKey:key
value:value
table:tableName];
}
@end
@implementation NSBundle (Language)
+(void)setLanguage:(NSString*)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass([NSBundle mainBundle],[PrivateBundle class]);
});
objc_setAssociatedObject([NSBundle mainBundle], &associatedLanguageBundle, language ?
[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
次通知,设置语言并广播通知LANGUAGE_WILL_CHANGE
AppDelegate.m
LANGUAGE_DID_CHANGE
BaseViewController将发布#import "AppDelegate.h"
#import "NSBundle+Language.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageWillChange:) name:@"LANGUAGE_WILL_CHANGE" object:nil];
NSString *targetLang = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedLanguage"];
[NSBundle setLanguage:targetLang?:@"en"];
return YES;
}
-(void)languageWillChange:(NSNotification *) noti
{
NSString *targetLang = [noti object];
[[NSUserDefaults standardUserDefaults] setObject:targetLang forKey:@"selectedLanguage"];
[NSBundle setLanguage:targetLang];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LANGUAGE_DID_CHANGE" object:targetLang];
}
@end
并收听LANGUAGE_WILL_CHANGE
BaseViewController.h
LANGUAGE_DID_CHANGE
BaseViewController.m
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
-(void) languageDidChange;
- (IBAction)switchLanguage:(id)sender;
@end
现在,子类#import "BaseViewController.h"
@interface BaseViewController ()
@property (weak, nonatomic) IBOutlet UIButton *englishButton;
@property (weak, nonatomic) IBOutlet UIButton *spanishButton;
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageDidChangeNotification:) name:@"LANGUAGE_DID_CHANGE" object:nil];
}
- (IBAction)switchLanguage:(id)sender {
NSString *localString;
if (self.englishButton == sender) {
localString = @"en";
} else if(self.spanishButton == sender){
localString = @"es";
}
if (localString) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"LANGUAGE_WILL_CHANGE" object:localString];
}
}
-(void)languageDidChangeNotification:(NSNotification *)notification
{
[self languageDidChange];
}
-(void)languageDidChange
{
}
@end
的任何视图控制器都可以实现BaseViewController
来调用languageDidChange
。
ViewController.m
NSLocalizedString
您看到广告,我只是将图片名称本地化,我将图片#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self languageDidChange];
}
-(void)languageDidChange
{
self.label.text = NSLocalizedString(@"Hello World", nil);
self.imageView.image = [UIImage imageNamed:NSLocalizedString(@"image.png", nil)];
}
@end
和en_image.png
添加到图片素材资源包并将其映射到可本地化的字符串
es_image.png
和
"image.png" = "en_image.png";
结果
您可以在此处找到此示例代码:https://github.com/vikingosegundo/ImmidiateLanguageChange