How to change Layout direction RTL to LTR for Whole Application in Xamarin.IOS

Paliwal, Vishal (Cognizant) 21 Reputation points
2022-05-20T09:52:29.523+00:00

Hi,

I am facing one issue in Xamarin.IOS.

My app is multi language supported. App is running Arabic (RTL) language is working perfectly. When i changed Arabic to french (other) language in runtime. Language is changed but layout direction is not changed for whole application. Still layout direction is RTL for whole app. how to change layout direction (LTR) for whole application.

Can you please help me out for this problem.

Thanks,

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,451 Reputation points Microsoft Vendor
    2022-05-23T08:10:54.857+00:00

    Hello,

    When you change the language, there is a ObserveCurrentLocaleDidChange method, then you can get the language and determine if it is Arabic and change the UI by setting UIView.Appearance.SemanticContentAttribute.
    About how to use this notification, refer to https://learn.microsoft.com/en-us/dotnet/api/foundation.nslocale.currentlocaledidchangenotification?view=xamarin-ios-sdk-12#remarks

    About setSemanticContentAttribute, refer to the following code:

    using System.Runtime.InteropServices;  
    using ObjCRuntime;//namespace  
    
        [DllImport(ObjCRuntime.Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]  
        internal extern static IntPtr IntPtr_objc_msgSend(IntPtr receiver, IntPtr selector, UISemanticContentAttribute arg1);  
    

    // add to your notification method

     IntPtr_objc_msgSend(UIView.Appearance.Handle, selector.Handle, isArabic ? UISemanticContentAttribute.ForceRightToLeft : UISemanticContentAttribute.ForceLeftToRight);  
    

    I'm not sure how did you change the language, if you change language in Settings, it's better to restart the application for the changes to take effect.

    As @Paliwal, Vishal (Cognizant) said , " in some default UIPrintInteractionController and WKWebView is not showing the correct language without app restart."
    From the Apple Doc we can see we have to guide users to the system settings for language selection when we want to change the language.

     UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Paliwal, Vishal (Cognizant) 21 Reputation points
    2022-05-23T08:51:23.567+00:00

    Hi,

    I am changing the language using my App not setting. how am i changing language. you can see below code

    sealed class SingletonManager
    {
    private static SingletonManager sharedInstance = new SingletonManager();
    public NSBundle LanguageBundle { get; private set; } = NSBundle.MainBundle;
    private SingletonManager()
    {
    }
    static internal SingletonManager SharedInstance()
    {
    return sharedInstance;
    }
    public void SetLanguage()
    {
    var countryCode = AppSettings.CountryCode;
    var currentLanguage = GetCurrentLanguage();
    var path = NSBundle.MainBundle.PathForResource(currentLanguage, Constants.LocalizationIos.Lproj);
    if (path != null)
    LanguageBundle = NSBundle.FromPath(path);
    }
    }

    public static string GetCurrentLanguage()
    {
    var currentLanguage = NSUserDefaults.StandardUserDefaults.ValueForKey(new NSString("AppleLanguages")).ToString();
    Console.WriteLine(currentLanguage);
    currentLanguage = currentLanguage.Replace('\n', ' ');
    currentLanguage = currentLanguage.Replace('(', ' ');
    currentLanguage = currentLanguage.Replace(')', ' ');
    currentLanguage = currentLanguage.Trim();
    currentLanguage = currentLanguage.Replace('\"', ' ');
    currentLanguage = currentLanguage.Trim();
    var countryCode = AppSettings.CountryCode;
    if ((countryCode == "CA" && currentLanguage == "fr") || (countryCode == "GB" && currentLanguage == "en") || countryCode == "MA" || countryCode == "SK" || countryCode == "CZ" || countryCode == "PL" || countryCode == "JP" ||countryCode == "IL")
    {
    currentLanguage = currentLanguage + "-" + countryCode;
    if(currentLanguage.Length > 6)
    {
    currentLanguage = currentLanguage.Remove(5);
    }
    }
    Console.WriteLine(currentLanguage);
    return currentLanguage;
    }

    The code you shared with me (UIView.Appearance.SemanticContentAttribute ) but that code is not working because In UIView.Appearance is not contain SemanticContentAttribute property.

    Below line of code we can use :

    this.View.SemanticContentAttribute = UISemanticContentAttribute.ForceLeftToRight;/

    Note :- we need to change the layout direction without restart the app.

    Can you please share me your thoughts. what am i mistaking.

    Thanks