Dalal Alharbi
Dalal Alharbi

Reputation: 191

localizable.strings not working with Arabic string

I created a Localizable.strings file in XCode and then 2 languages in it.(english + Arabic )

I filled up these files with the language translations, but the just show translation in english, when I start with Arabic the key appear!

in my code :

NSLocalizedString("title", comment: "")

Localizable.strings(english)

"title" = "Error" ;

Localizable.strings(Arabic)

      "title" = "خطأ" ;

Upvotes: 0

Views: 2512

Answers (1)

user3182143
user3182143

Reputation: 9589

I careated sample one and tried in Objective C.I got it.

I set "title" = "خطأ" in Arabic Localization files

"title" = "عنوان";

Now I have to change English to Arabic.

First I set the design in storyboard

enter image description here

Then Click Project.Choose Localization in Info

enter image description here

If you click the +(Below Localization) it shows the pop up view

enter image description here

Now choose Arabic.When click Arabic it shows window.You should click finish.

enter image description here

We need to create the string file for the localization now.I set string file name as LocalizationArabic

enter image description here

Once you create the String file it looks like below.

enter image description here

Then click File Inspector when pressing LocalizationArabic string file.Now click the Localization.It shows Empty Check box Arabic and English like below.

enter image description here

Here we must check the check box.Also when we check the check box the LocalizationArabic folder creates with three string files like below

enter image description here

Then I entered the language which I want to translate from English to Arabic in string file.

enter image description here

Finally I created the Header file for the Localization Language

enter image description here

The Header file name is LanguageHeader.It looks like below.

enter image description here

Now the code part starts here

First the Localization class of NSObject class

Localization.h

#import <Foundation/Foundation.h>
#import "LanguageHeader.h"

@interface Localization : NSObject
+(Localization *)sharedInstance;
+(NSString*) strSelectLanguage:(int)curLang;
+(NSString*) languageSelectedStringForKey:(NSString*) key;

@end

Localization.m

#import "Localization.h" int currentLanguage,selectedrow; @implementation Localization

+(Localization *)sharedInstance
{
    static Localization *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[Localization alloc] init];
    });
    return sharedInstance;
}


+(NSString*) strSelectLanguage:(int)curLang{
    if(curLang==ARABIC){
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar", nil]forKey:@"AppleLanguages"];
    }
    else{
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil]forKey:@"AppleLanguages"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
    currentLanguage=curLang;
    NSString *strLangSelect = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
    return strLangSelect;
}

+(NSString*) languageSelectedStringForKey:(NSString*) key
{
    NSString *path;
    NSString *strSelectedLanguage = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
    //When we check with iPhone,iPad device it shows "en-US".So we need to change it to "en"
    strSelectedLanguage = [strSelectedLanguage stringByReplacingOccurrencesOfString:@"en-US" withString:@"en"];
    if([strSelectedLanguage isEqualToString:[NSString stringWithFormat: @"en"]]){
        currentLanguage=ENGLISH;
        selectedrow=ENGLISH;
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    }
    else{
        currentLanguage=ARABIC;
        selectedrow=ARABIC;
        path = [[NSBundle mainBundle] pathForResource:@"ar" ofType:@"lproj"];
    }
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizationArabic"];
    return str;
}

@end

Then ViewController.h

#import <UIKit/UIKit.h>
#import "Localization.h"

@interface ViewController : UIViewController{
    Localization *localization;
}

@property (strong, nonatomic) IBOutlet UILabel *lblTitle;

- (IBAction)actionChangeLanguageToArabic:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize lblTitle;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    localization = [Localization sharedInstance];
    lblTitle.text = [Localization languageSelectedStringForKey:@"title"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)actionChangeLanguageToArabic:(id)sender {
    [Localization strSelectLanguage:ARABIC];
    lblTitle.text = [Localization languageSelectedStringForKey:@"title"];
}

@end

Above code works perfectly.

Output Screen shot are below

When run the app first

enter image description here

After clicking the button

enter image description here

Upvotes: 1

Related Questions