Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ios - How to force "Respect Language Direction" from RTL to LTR and vice versa

I have an application which uses Auto Layouts.

The app switches RTL and LTR languages perfectly when the user selects he's or her's device phone language. All texts are localized, and language directions are working.

I also have a button inside the app, to change it's language without restarting the app. This also works great, and all texts are being replaced.

The problem is that when the user changes the language from within the app, "Respect Language Direction" option in the constraints i have pre-defined is ironically not respected.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Eventually, I have managed to solve this issue.

I couldn't find a way to force the change the RTL - LTR auto layout constraints, so i have decided to duplicate 2 additional storyboards for each language direction. So, actually my app now contains 3 storyboards - Main.storyboard, StoryboardRTL.storyboard and StoryboardLTR.storyboard.

Main handles changes in language direction when it is performed from the settings on the iDevice, and RTL / LTR is has it's own orientation, to support changing the app language from inside the app.

When the user selects to change to an RTL language, I set the selected language in the UserDefaults:

    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"he", nil] forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];

and right after i call a method i have written in the AppDelegate, which changes the storyboard according to the language layout.

- (void)reloadStoryboard
{
    UIStoryboard *storyboard;

    switch ([AALanguageManager getCurrentLanguage]) {

        case CurrentLanguageRTL:
            storyboard = [UIStoryboard storyboardWithName:@"StoryboardRTL" bundle:[NSBundle mainBundle]];
            break;
        default:
            storyboard = [UIStoryboard storyboardWithName:@"StoryboardLTR" bundle:[NSBundle mainBundle]];
            break;
    }

    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];
}

This looks to the user as if the app restarted, and display the proper storyboard with the RTL / LTR layout. When the user restarts the app again, the selected language is already set, and the main storyboard is displayed, with all the correct layout according to selected language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...