在运行iOS时,以编程方式动态更改语言

gab*_*uff 32 iphone localization runtime ios

我已经堆叠和谷歌搜索了几个小时.我现在有点绝望了.我想在应用程序中更改应用程序的语言,而不仅仅是使用默认语言.

从我的尝试开始,我就像所有人一样重新启动.意思是,苹果强制您手动重启应用程序.这意味着您必须退出应用程序然后再次启动它.

好吧,谷歌搜索后,我试图设置一个警报,然后强迫应用程序退出

exit(0);
Run Code Online (Sandbox Code Playgroud)

我的坏,苹果似乎不喜欢这样,并阻止开发者使用它...我想我没有指向正确的方向.

最后,尽管存在所有问题,我还是可以见面,我想讨论一下.

任何提示?


编辑,APPLE的相关信息

通常,您不应在应用程序中更改iOS系统语言(通过使用AppleLanguages pref键).这违反了在Settings应用程序中切换语言的基本iOS用户模型,并且还使用了未记录的首选项键,这意味着在将来的某个时候,键名可能会更改,这会破坏您的应用程序.

如果要在应用程序中切换语言,可以通过手动加载捆绑包中的资源文件来实现.您可以使用NSBundle:pathForResource:ofType:inDirectory:forLocalization:为此目的,但请记住,您的应用程序将负责所有本地化数据的加载.

关于退出(0)问题,Apple DTS无法评论应用程序批准过程.您应该联系appreview@apple.com以获得此问题的答案.

好吧,到目前为止我必须选择.

Swi*_*ude 21

这是一个相当古老的问题,但我只是在努力解决同样的问题,并找到了这个解决方案:

http://aggressive-mediocrity.blogspot.com/2010/03/custom-localization-system-for-your.html

这正是你所需要的(对于有同样问题的人可能会有所帮助:)


ano*_*eal 5

下面的链接有一个很好的实现,在应用程序中使用自定义语言.

iOS-App(iPhone和iPad)中的手动语言选择

尝试了SWIFT版本在这里找到它 LanguageSettings_Swift

LanguageChange

-anoop


mer*_*nix 4

是的,我遇到了同样的问题,然后我用我自己的 prefFile 中的语言设置来管理它,我在其中为语言设置设置了一个变量:

// write a new value in file and set the var
- (void)changeLangInPrefFile:(NSString *)newLanguage {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myPreference.plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //here add elements to data file and write data to file
    [data setObject:newLanguage forKey:@"language"];
    [data writeToFile:path atomically:YES];
    [data release];

// NSString *chosenLang; <- declared in .h file
    if (chosenLang != nil){
        [chosenLang release];
        chosenLang = nil;
    }
    chosenLang = [[NSString alloc] initWithString:(@"%@",newLanguage)];

}

// read the language from file and set the var:
- (void)readFromFileInBundleDocuments {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myPreference.plist"];
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    NSString *chosenLangTemp = [savedStock objectForKey:@"language"];
    NSLog (@"read in file: %@", chosenLangTemp);
    if (chosenLang != nil){
        [chosenLang release];
        chosenLang = nil;
    }
    chosenLang = [[NSString alloc] initWithString:(@"%@",chosenLangTemp)];
    [savedStock release];
}
Run Code Online (Sandbox Code Playgroud)

然后我根据语言加载不同文件中的所有内容,例如我可以加载“an_image_eng.png”或“an_image_ita.png”,或者有2个不同的.xib文件,对于要加载的文本,我使用不同的字典文件,每种语言都有一个,翻译了所有单词/表达式,我只需加载所选的一个并在其中读取要加载的每个文本的正确表达式(加载它的代码类似于我在本示例中编写的方法,您可以只需安排它为每个表达式读取正确的单词即可:只需查看右侧字典文件中 objectForKey 的值,其中 objectForKey 是要翻译的单词,其值是翻译的单词)...