我为我的iOS应用程序进行了本地化。通过客户端订单,我需要进行修改,即应用程序语言应通过按钮操作进行更改。我以前的本地化是基于设备语言的作品。现在,我必须修改应用程序以更改语言,而无需重新启动设备。更改iOS应用程序语言,而无需重新启动设备
3
A
回答
4
您不能在应用程序中更改设备的语言,但您可以通过更改NSUserDefaults
中的AppleLanguages
属性来更改它,仅适用于您的应用程序。请注意,但我相信这仍然需要重新启动应用程序本身,但不需要重新启动设备。
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];
Here's另一个类似的问题也可能能帮助你
+1
这里的重点不是以编程方式更改设备上的语言,而是通过点击按钮来更改应用程序中的语言。 _first_不可能通过公共API,_second_一个是完全合理的,可以没有任何限制地完成。 – holex 2014-10-29 11:29:54
4
创建LocalizeHelper.h
#import <Foundation/Foundation.h>
// some macros (optional, but makes life easy)
// Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)"
#define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)]
// "language" can be (for american english): "en", "en-US", "english". Analogous for other languages.
#define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)]
@interface LocalizeHelper : NSObject
// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;
// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;
//set a new language:
- (void) setLanguage:(NSString*) lang;
@end
和.m文件
// LocalizeHelper.m
#import "LocalizeHelper.h"
// Singleton
static LocalizeHelper* SingleLocalSystem = nil;
// my Bundle (not the main bundle!)
static NSBundle* myBundle = nil;
@implementation LocalizeHelper
//-------------------------------------------------------------
// allways return the same singleton
//-------------------------------------------------------------
+ (LocalizeHelper*) sharedLocalSystem {
// lazy instantiation
if (SingleLocalSystem == nil) {
SingleLocalSystem = [[LocalizeHelper alloc] init];
}
return SingleLocalSystem;
}
//-------------------------------------------------------------
// initiating
//-------------------------------------------------------------
- (id) init {
self = [super init];
if (self) {
// use systems main bundle as default bundle
myBundle = [NSBundle mainBundle];
}
return self;
}
//-------------------------------------------------------------
// translate a string
//-------------------------------------------------------------
// you can use this macro:
// LocalizedString(@"Text");
- (NSString*) localizedStringForKey:(NSString*) key {
// this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment")
// the difference is: here we do not use the systems main bundle, but a bundle
// we selected manually before (see "setLanguage")
return [myBundle localizedStringForKey:key value:@"" table:nil];
}
//-------------------------------------------------------------
// set a new language
//-------------------------------------------------------------
// you can use this macro:
// LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de");
- (void) setLanguage:(NSString*) lang {
// path to this languages bundle
NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ];
if (path == nil) {
// there is no bundle for that language
// use main bundle instead
myBundle = [NSBundle mainBundle];
} else {
// use this bundle as my bundle from now on:
myBundle = [NSBundle bundleWithPath:path];
// to be absolutely shure (this is probably unnecessary):
if (myBundle == nil) {
myBundle = [NSBundle mainBundle];
}
}
}
@end
对于设置语言使用
LocalizationSetLanguage(@"ar");
对于获取值使用
self.Mylabel.text = LocalizedString(@ “租金”);
对于您想支持的每种语言,您需要一个名为Localizable.strings的文件。这与Apple本地化文档中描述的完全相同。
// TABS
"buy" = "شراء";
"rent" = "إيجار";
"addListing" = "إضافة إعلان" ;
"calculator" = "دلالي" ;
"news" = "أخبار" ;
相关问题
- 1. iOS:如何以编程方式更改应用程序语言而无需重新启动应用程序?
- 2. 在iOS中更改应用程序语言而无需重新启动应用程序
- 3. iOS - 通过编程更改应用程序特定的语言,无需重新启动应用程序
- 4. 如何更改应用程序语言而无需重新启动/关闭应用程序?
- 5. 如何在iOS9中更改应用程序的语言而无需重新启动应用程序?
- 6. IOS:应用程序语言独立的设备,而无需改变NSUserDefaults的
- 7. 作为设备语言更改更改应用程序语言
- 8. 更新Android应用程序而不重新启动设备
- 9. 在应用程序中更改语言 - 如何重新启动?
- 10. 在设备重新启动时启动iOS应用程序
- 11. 如何以编程方式更改应用程序语言无需重新启动我的应用程序?
- 12. 使用按钮更改iphone应用程序语言并且无需重新启动应用程序
- 13. 重新启动设备时,是否会重新启动监视重要位置更改的iOS应用程序?
- 14. [Android]应用程序重新启动设备后重新启动
- 15. android:在运行中反映UI语言更改,无需重新加载/重新启动应用程序
- 16. 无需重新启动应用程序
- 17. 改变应用程序的语言,而不改变设备
- 18. 重新启动应用程序而无需重新启动服务器?
- 19. 更改ios应用程序的语言
- 20. 带iBeacon的iOS应用程序必须重新启动设备
- 21. 我想改变应用程序的语言,无需重新启动应用程序
- 22. adb:更新/重新扫描联系人而无需重新启动android设备
- 23. 在UWP中更改语言不会更改系统功能语言 - 仅在应用程序重新启动时
- 24. iOS应用程序安装后iPhone应用程序无法启动,设备重新启动后工作
- 25. 更新配置文件,无需重新启动应用程序
- 26. 更改iOS中的应用程序语言设置不是整个设备
- 27. 重新启动应用程序,而无需担心的onSaveInstanceState
- 28. 的GlassFish - 部署应用程序而无需重新启动域
- 29. 如何在更改语言环境时防止应用程序重新启动?
- 30. 当用户更改设备语言时应用程序崩溃
您需要tom更改按钮动作中的语言 – 2014-10-29 08:48:51
您可以随时创建自定义本地化过程,可以通过应用程序中的按钮操作。你在哪里坚持实施这样的解决方案? – holex 2014-10-29 11:27:17