简体   繁体   English

如何在运行时更改iPhone应用程序语言?

[英]How to change iPhone app language during runtime?

Is there a way to change the application language during runtime? 有没有办法在运行时更改应用程序语言?

So, after the change NSLocalizedString immediately returns the string for the new language. 因此,更改后, NSLocalizedString立即返回新语言的字符串。

What I'm doing now is changing the language using the code below: 我现在正在使用以下代码更改语言:

- (void)onChangeLanguage: (id)sender 
{
    NSArray *lang = [NSArray arrayWithObjects:((InfoWhatever *)sender).language, nil];
    [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLanguage = [languages objectAtIndex:0];

    NSLog(@"Current language: %@", currentLanguage);
}

The language will change but only after restarting the app. 语言将更改,但仅在重新启动应用程序之后。

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* languages = [userDefaults objectForKey:@"AppleLanguages"];
[languages insertObject:@"de" atIndex:0]; // ISO639-1
[[NSUserDefaults standardUserDefaults] synchronize];

You Can do it . 你能行的 。 Here is the way http://aggressive-mediocrity.blogspot.com/2010/03/custom-localization-system-for-your.html 这是http://aggressive-mediocrity.blogspot.com/2010/03/custom-localization-system-for-your.html的方式

  1. In Brief Download and add 2 files to the project 简要下载并将2个文件添加到项目中

    http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.h http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.h

    http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.m http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.m

2 2
#import "LocalizationSystem.h"

3 3

- (IBAction)btnEnglishClicked:(id)sender {
     LocalizationSetLanguage(@"en");
}

4 After you set the language as above 4如上设置语言后

AMLocalizedString(@"Key", nil)

Thats it . 就是这样

The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language , 通过从应用程序中选择特定语言的技巧是强制NSLocalizedString根据所选语言使用特定捆绑软件,

here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html 这是我为此http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html写的帖子

and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps 这是一个示例应用程序的代码https://github.com/object2dot0/Advance-Localization-in-ios-apps

I doubt you can do this, even the Settings app cannot do it. 我怀疑您可以执行此操作,即使“设置”应用也无法执行此操作。

(When you change the language in the Settings app, the screen goes black, and displays "setting language..." and a progress wheel. After a long wait, you are back in Springboard. It almost looks like the phone reboots.) (当您在“设置”应用中更改语言时,屏幕将变黑,并显示“设置语言...”和进度轮。漫长的等待之后,您又回到了Springboard。几乎就像手机在重启一样。)

I came up with a solution that allows you to use NSLocalizedString . 我想出了一个允许您使用NSLocalizedString的解决方案。 I create a category of NSBundle call NSBundle+RunTimeLanguage . 我创建了一个NSBundle类别,称为NSBundle+RunTimeLanguage The interface is like this. 界面是这样的。

// NSBundle+RunTimeLanguage.h
#import <Foundation/Foundation.h>
@interface NSBundle (RunTimeLanguage)
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil]
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName;
@end

The implementation is like this. 实现是这样的。

// NSBundle+RunTimeLanguage.m
#import "NSBundle+RunTimeLanguage.h"
#import "AppDelegate.h"

@implementation NSBundle (RunTimeLanguage)

- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *path= [[NSBundle mainBundle] pathForResource:[appDelegate languageCode] ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:path];
    NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil];
    return localizedString;
}
@end

Than just add import NSBundle+RunTimeLanguage.h into the files that use NSLocalizedString . 比只是将import NSBundle+RunTimeLanguage.h添加到使用NSLocalizedString的文件中。

As you can see I store my languageCode in a property of AppDelegate . 如您所见,我将languageCode存储在AppDelegate的属性中。 This could be stored anywhere you'd like. 它可以存储在您想要的任何位置。

This only thing I don't like about it is a Warning that NSLocalizedString marco redefined. 我唯一不喜欢的是警告NSLocalizedString marco重新定义。 Perhaps someone could help me fix this part. 也许有人可以帮助我修复此部分。

Simply add these lines: 只需添加以下行:

 #define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]   


 1. NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 
            [defaults setObject:@[@"en"] forKey:@"AppleLanguages"];  [defaults
            synchronize];

 2. _label.text = NSLocalizedStringFromTableInBundle(@"Key", nil, currentLanguageBundle, @"");

try this: object_setClass([NSBundle mainBundle],[MyBundle class]); 试试这个:object_setClass([NSBundle mainBundle],[MyBundle class]);

https://github.com/maximbilan/ios_language_manager/blob/master/README.md https://github.com/maximbilan/ios_language_manager/blob/master/README.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM