【问题标题】:Change iOS app language without rebooting the device无需重启设备即可更改 iOS 应用程序语言
【发布时间】:2014-12-24 20:48:36
【问题描述】:

我为我的 iOS 应用程序进行了本地化。根据客户订单,我需要进行修改,即应用程序语言应通过按钮操作进行更改。我以前的本地化是基于设备语言的。现在我必须修改应用程序以更改语言而无需重新启动设备。

【问题讨论】:

  • 你需要在按钮操作中更改语言
  • 您可以随时创建自定义本地化程序,可以通过应用程序内的按钮进行操作。你在哪里实施这样的解决方案?

标签: ios objective-c localization


【解决方案1】:

您无法在应用程序中更改设备的语言,但您可以通过更改NSUserDefaults 中的AppleLanguages 属性来仅为您的应用程序更改它。但请注意,我相信这仍然需要重新启动应用程序本身,但不需要重新启动设备。

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];

Here's另一个类似的问题,也许也能帮助你

【讨论】:

  • 重点不是以编程方式更改设备上的语言,而是通过点击按钮更改应用程序内部的语言。 first 是无法通过公共 API 实现的,second 是完全合理的,可以不受任何限制地完成。
【解决方案2】:

创建 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(@"rent");

对于您想要支持的每种语言,您都需要一个名为 Localizable.strings 的文件。这与 Apples 本地化文档中的描述完全一样。

// TABS

"buy" = "شراء";
"rent" = "إيجار";
"addListing" = "إضافة إعلان" ;
"calculator" = "دلالي" ;
"news" = "أخبار" ;

【讨论】:

  • 感谢它运行良好。但是如果我再次打开应用程序,我将面临一个问题,这意味着需要更改语言。如何保存/获取当前选择的语言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2013-10-09
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
相关资源
最近更新 更多