Ashwin Kanjariya Ashwin Kanjariya - 2 months ago 20
iOS Question

How Can I set Localised Direction within application?(RTL if user select arabic, LTR is selected language is English)

My Application must support Arabic(right to left direction) and English(left to right direction) language, I need to set UI and based on user select language from my application.

I will implement UI with NSLayoutConstraint so it can update UI automatically, based on leading and trailing constraints.

Now My Question is how can i achieve this? As My device language is say English and from my application user select Arabic(limited to my app only), So my Flow,UI,Animations etc should be right to left.

here are the sample snap for ui Direction
enter image description here
enter image description here

Thanks

Answer

Short answer:

Even if you managed to do that. Layout will not change according to the language selected without an app. restart by user.

Apple's guide line:

Apple doesn't provide that because they don't prefere to allow the user to change language from inside the app. Apple pushes the developers to use the device language. and not to change it from inside the app at all.

Workarwound:

Some apps that supports Arabic language notes that's in the settings page where the user can change the language. noting to the user that layout will not take effect without an app. restart. store link for sample app

Example code to change app language to arabic:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar", @"en", nil] forKey:@"AppleLanguages"];

That won't take effect without a restart

Alternative Solution:

Is to provide your own localization solution:

  1. Do not Use Base Internationalization.
  2. Create one strings file for each language.
  3. Place the change language feature in a ViewController that starts before any other ViewController even before your main one.
  4. Set the constraints and strings for your views in viewDidLoad method for all view controllers.

You don't have to satisfy point 5 if you didn't Use Base Internationalization

  1. Change every constraint at the storyboard from Leadring/Trailing to Right/Left.

Language class sample:

@implementation LanguageDetails

@synthesize language;
@synthesize bundle;

#define LANGUAGE_KEY @"LANGUAGE_KEY"

// language var is also the strings file name ar or en

- (id)init
{
    if (self = [super init]) {

        language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

        if (![language isEqualToString:@"ar"])
            language = @"en";

        language = [Common valueForKey:LANGUAGE_KEY defaultValue:language];

        bundle = [NSBundle mainBundle];
    }
    return  self;
}

- (BOOL)rtl {
    return [NSLocale characterDirectionForLanguage:language] == NSLocaleLanguageDirectionRightToLeft;
}

- (void)changeLanguage
{
    if ([language isEqualToString:@"ar"])
        language = @"en";
    else
        language = @"ar";

    [Common setValue:language forKey:LANGUAGE_KEY];
}

- (void)changeLanguageTo:(NSString *)lang
{
    language = lang;

    [Common setValue:language forKey:LANGUAGE_KEY];
}

- (NSString *) LocalString:(NSString *)key {
    return NSLocalizedStringFromTableInBundle(key, language, bundle, nil);
}

@end

Initialise an object of this class in your singlton class.

Comments