如何强制“尊重语言方向"从 RTL 到 LTR,反之亦然

2023-09-10移动开发问题
13

本文介绍了如何强制“尊重语言方向"从 RTL 到 LTR,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个使用自动布局的应用程序.

I have an application which uses Auto Layouts.

当用户选择他或她的设备电话语言时,该应用会完美地切换 RTL 和 LTR 语言.所有文本均已本地化,并且语言方向有效.

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.

有什么想法吗?

推荐答案

最终,我设法解决了这个问题.

Eventually, I have managed to solve this issue.

我找不到强制更改 RTL - LTR 自动布局约束的方法,因此我决定为每个语言方向复制 2 个额外的故事板.所以,实际上我的应用现在包含 3 个故事板 - Main.storyboard、StoryboardRTL.storyboard 和 StoryboardLTR.storyboard.

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.

主要处理从iDevice上的设置执行时语言方向的变化,并且RTL/LTR有自己的方向,以支持从应用程序内部更改应用程序语言.

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.

当用户选择更改为 RTL 语言时,我在 UserDefaults 中设置所选语言:

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];

在我调用我在 AppDelegate 中编写的方法之后,它会根据语言布局更改情节提要.

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];
}

这对用户来说就像应用重新启动一样,并显示具有 RTL/LTR 布局的正确情节提要.当用户再次重新启动应用程序时,选择的语言已经设置好,主情节提要显示出来,所有的布局都根据选择的语言正确.

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.

这篇关于如何强制“尊重语言方向"从 RTL 到 LTR,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13