4

I am trying to change MFmailComposer Language programmatically. Initially if I select the "English mail" button first MFMailComposer is opening in English language. now if I select "Arabic Mail" button then MFMailComposer will open in English Language, not in Arabic Language.

Here is the screenshot of my ViewController

enter image description here

Screen shot when English Button is selected

Screen shot when Arabic button is selected

So, how to change MFMailComposer Language Programmatically without restarting the application?

Here is code

- (IBAction)showInArabic:(id)sender 
{ 

   [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar", nil] forKey:@"AppleLanguages"];
   [[NSUserDefaults standardUserDefaults] synchronize];
   [self viewDidUnload];
   [self viewDidLoad];
   Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
   if (mailClass != nil) 
   {
      if ([mailClass canSendMail]) 
      {
        [self displayComposerSheet];

      } else {

    [self launchMailAppOnDevice];
      }

    } else {

    [self launchMailAppOnDevice];
    }
}
- (IBAction)showInEnglish:(id)sender 
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSArray *languages = [NSArray arrayWithObject:@"en"];
    [[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];

    [self viewDidUnload];
    [self viewDidLoad];

    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    if (mailClass != nil) {

        if ([mailClass canSendMail]) {

            [self displayComposerSheet];

        } else {

            [self launchMailAppOnDevice];
        }

    } else {

        [self launchMailAppOnDevice];
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Apple
  • 550
  • 3
  • 10
  • 1
    i think this link can help you http://stackoverflow.com/questions/1321281/mfmailcomposeviewcontroller-in-other-languages – Abdul Rauf May 18 '12 at 11:41
  • Check this : 1.http://stackoverflow.com/questions/9971469/multilanguage-application-in-iphone/9971641#9971641 2.http://stackoverflow.com/questions/10647706/how-to-use-spanish-french-chinese-german-keyboard-in-app/10647934#10647934 – Naveen Thunga May 18 '12 at 12:07
  • @NaveenThunga Means Every time MFMailComposer takes the language of Button on which you click First. if you click the another language button without closing application, MFMailComposer will not change Language – Apple May 18 '12 at 12:34
  • @Alelish : If you want to change your keyboard language you can change it there only. If you have localization in your app, Then you must set language in setting. For that you have to send your application to background. – Naveen Thunga May 18 '12 at 12:57

2 Answers2

0

There seems to be no way to change MFMailComposer Language Programatically as of now. It is set to English by default.

Maulik
  • 19,348
  • 14
  • 82
  • 137
girish_vr
  • 3,041
  • 1
  • 24
  • 27
  • But, Initially if i select "Arabic mail" button, then MFMailComposer open in Arabic Language and again if i click on "English Mail" button without closing application then MFMailComposer will open in arabic, not in English – Apple May 18 '12 at 12:02
  • Means Every time MFMailComposer takes the language of Button on which you click First. if you click the another language button without closing application, MFMailComposer will not change Language – Apple May 18 '12 at 12:12
0

Try this. It worked fine for me. Please do have your localized string file for english and arabic in NSBundle. With this you need to toggle between device setting and app. You can change language through app only.

#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]];

For english

[self changeLanguage : @"en"];

For arbic

[self changeLanguage : @"ar"];

Put this in Button's action method

- (void)changeLanguage : (NSString*)inLang
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:inLang forKey:@"AppleLanguages"];
    NSLog(@"test %@", NSLocalizedStringFromTableInBundle(@"NewKey", nil, currentLanguageBundle, @""));
}

Feel free if you have any doubts regarding this.

Naveen Thunga
  • 3,675
  • 2
  • 24
  • 31
  • 2
    This will work for the Static Text of the Application. But i want to change the Language of MFMailComposer. e.g. TO: CC: Bcc: This will be displayed in arabic when arabic button is selected, and in English when english button is selected. – Apple May 23 '12 at 12:53
  • You are right brother.. I have a idea, Create 2 nib files same as MFMailComposer one for English and another for arabic. Load it according to language selected. When user tap on send, Create MFMailComposer object add properties data and send it in background. – Naveen Thunga May 24 '12 at 05:21
  • 1
    I wan't to change default Text like "To , Cc, Bcc, Send" etc. Text according to Language Selection. But, We can not make the Nib file of MFMailComposer. i want to use default MFMailComposer – Apple May 24 '12 at 05:26