Change iphone app language with buttons and withou

2020-04-08 13:10发布

I want to change the language of my app. At the moment I am doing it at the following way. I have two buttons which are change the languages in NSUserDefaults. But before this affects my app I need to restart it.

Here is my code.

- (IBAction)changeDutch:(id)sender {
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];

}
- (IBAction)changeEnglish:(id)sender {
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"nl", nil] forKey:@"AppleLanguages"];
}

This works fine. But I don't want every time to restart my app. Can anyone tell me how to do that?

Kind regards!

4条回答
▲ chillily
2楼-- · 2020-04-08 13:17

which way you're using for localization, I mean to ask is that native in which we just only maintain the folder structure or taken from Db.

Anyways, If you're using native part, then look at following link which may assist you as I think we can replace the language option there:

"http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios" (Frankly speaking, I've no idea)

But if you are using the db part, then in that case you just use some enum based tact and according to which just fetch your data, that approach is simple as I've already applied in so many apps.

In any concern, just back to me. :)

查看更多
放荡不羁爱自由
3楼-- · 2020-04-08 13:28

If your data is in UITableView then you could use [tableView reloadData];. You can even set the app languages in appDelegate, defined in Constants, keys, then on the IBAction call those keys then be stored in NSUserDefaults.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-04-08 13:29

There is a method to change app language without restart mentioned this tutorial post I've tried it in an app and it works mostly but still if you are using system items like More tab in tabbar, Edit button on MoreNavigationController and Cancel button on UISearchBar and so on, there text can't be changed for selected language without restarting app. If there is no such item that is controlled by iOS instead of your app, this is a perfect solution for you.

查看更多
老娘就宠你
5楼-- · 2020-04-08 13:44

Check out HMLocalization: https://github.com/HeshamMegid/HMLocalization

It's a replacement for the default localisation framework. It has a demo showing how to change language without having to restart the app.

查看更多
登录 后发表回答