Search This Blog

Thursday 17 April 2014

Localization

Hi,

Today i will guide you to make your app to support different languages in the same app.

To support Localization in the app, there are two ways :

    First, is to create separate .xib files or create new storyboard for other language for eg. Hebrew. It means if we want to have English and Hebrew version of the app,then we will need to create 2 separate .xibs for each controller or if we are using storyboard then need to create 2 separate storyboard files for each language. Here, for every .xib ,we have to set the "text" as per the language. Eg. if we have label on the .xib say "titleLabel" for ENGLISH language, then we will set the "text" property in property window as "This is a Label" in English. But for other .xib, which is in HEBREW,its label title would be set as "זה מדבקה" in Hebrew and same with other tools.

    Second, is to have only 1 xib for all languages or only 1 storyboard file for all languages we are going to support in our app.Here, for every tool we have to set IBOUTLets,so that their values would be set through code(run-time),not on .xib.

The Second method is preferred,so i will explain you the same.

1. Click on the Project name > your project name under PROJECTS > Info > Go to Localizations             option. You would see default language as "English".Go down you would see "+" sign,select your       language to support in the app apart from English, in our eg "Hebrew".

2. We set Language for the entire app.Now we would set all the images we would be using in app.So       drag all images into the app.Now select images one by one and go to property window and click on        "Localize..." button under "Localization" section.Select Localize button on Popup.Now select the         same image again and see tow languages would appear under "Localization" section in property             window.

3. You would see that the image precedes with arrow to open.Click on it,you would see 2 images for 2     different languages.Click on "Hebrew" image > right click > "show in finder" replace this english         version image with respective "Hebrew" image for it.Thus, you would see "Hebrew" image                   here.Repeat the same procedure for all images.We are done with images now.

4.Now for Text like label text,button title etc. we need to add Localizable.string file in app and repeat        the same procedure we did for images so you would have 2 files inside Localizable.string.
   Here you would add "Key-Value" pair in both the files.Eg. We would write
    "SAVE" = "SAVE"
    entry for the text "SAVE" in English file.Here, First is a "key" and second is its "value".

   Now for the same eg, we would have,
   "SAVE" = "KAYDET";
   entry would be in Hebrew file.Here key is "SAVE" and its value is "KAYDET".

5.We create 2 Localizable.string file for both languages which has all the ENG and Hebrew text we          would use in app.Now we would see how to use it.


      NSLocalizedString(@"SAVE", nil
  
  using this method we would get test as SAVE or KAYDET based on language you set on the device.

so for "titleLabel" would set as ,

titleLabel.text = NSLocalizedString(@"SAVE"nil) ;

if device language is Eng then it would return SAVE else KAYDET.

That's it. You are done.

Feel free to ask questions...

Thank you :)


Scroll up view when keyboard appears

Hi,

Here I am about to help you to tackle the textfield or textview invisibility problem when keyboard appears on the screen.There are 2 delegate methods related to UITextfield and UITextView.You just have to move the current view up slightly to make the controls visible.

Here are the 2 delegates of UITextField.

1. Just declare  
   
          CGFloat animatedDistance;
     in viewController.h file.

2. Add these Constants as well,

          static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
          static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
          static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
          static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 240;
          static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;

3. Finally,add the UITextField delegates in your viewController.m as below,

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {    
         CGRect textFieldRect =
         [self.view.window convertRect:textField.bounds fromView:textField];
         CGRect viewRect =
         [self.view.window convertRect:self.view.bounds fromView:self.view];
         CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
         CGFloat numerator =
            midline - viewRect.origin.y
           - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
        CGFloat denominator =
              (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
                 * viewRect.size.height;
        CGFloat heightFraction = numerator / denominator;

       if (heightFraction < 0.0){

             heightFraction = 0.0;

        }else if (heightFraction > 1.0) {

                  heightFraction = 2.0;
        }
     
        UIInterfaceOrientation orientation =
               [[UIApplication sharedApplication] statusBarOrientation];
 
        if (orientation == UIInterfaceOrientationPortrait ||
                        orientation == UIInterfaceOrientationPortraitUpsideDown)
        {
                animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
         }
        else
        {
              animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
        }


        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y -= animatedDistance;
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
        [self.view setFrame:viewFrame];
    
        [UIView commitAnimations];
    }


    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
    
        if(self.view.frame.origin.y == 0)
              return;
    
        if (animatedDistance == 0.0)
             return;
    
         CGRect viewFrame = self.view.frame;
         viewFrame.origin.y += animatedDistance;
    
         [UIView beginAnimations:nil context:NULL];
         [UIView setAnimationBeginsFromCurrentState:YES];
         [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
         [self.view setFrame:viewFrame];
    
         [UIView commitAnimations];
    
         animatedDistance = 0.0;

     }

You are done !

For UITextView you have to replace above delegate methods with

- (void)textViewDidBeginEditing:(UITextView *)textVield and
- (void)textViewDidEndEditing:(UITextView *)textView

respectively.



Feel free to comment. Thanks :)