Sunday 23 September 2012

How to localize (Internationalization/i18n) an iPhone application.

Localization inside the application without using iPhone language settings.


Today we will start with the internationalization of an iPhone app from within the application. This is something most of us want to do as it not only helps the user experience but also prevents us from changing the iphone's language setting every time we want a different language for the app.

So quickly lets get started. We create a project in xcode and name it as you like it.

Now, the way to go about localizing the app is to create a ".strings" file in the app which can be created from resource like this:
Create a  Localizable.strings  file from resource

By default the name of our created language resource string file is 'Localizable.strings '

What we have our app title at the moment is like this:

self.titleLabel.text = @"Welcome to CodeHunk!";
But we want to create our localization string taking values from our ".strings" file so we actually use a macro for this task which is NSLocalizedString and we use it like this:

self.titleLabel.text = NSLocalizedString(@"title", nil);
Now if you go deep into NSLocalizedString in the definition you will find that it actually issomething like this:

#define NSLocalizedString(key, comment)
    [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

this is responsible for getting the default strings file and search for the localized string we have passed in the method NSLocalizedString. The comment parameter is nothing but just to let you(as a translator) understand the use of key in our string file. So don't worry about that you can even leave it nil. More information can be found here in the Apple's reference doc.

Now is the most important and precise part of the application according to our demand. How do we manually guide the localizedStringForKey to pick our localized string from the ."strings" file(s) we have already created?

now if you look more into it you will find that the 'Localizable.strings' file has the following architecture.



Alright, now it is time we add our second language, let's take Spanish.
we need to create a second file for Spanish. So there's a project folder created for each of our language like 'en.lproj' and 'es.lproj' and inside we have our Localizable.strings files.


Our motive is to make use of these files as and when the user desires to change the language  from within the application. 

Here comes the most interesting part.

We will create a method lets say languageSelectedStringForKey and pass a NSString parameter called 'key'
Our method is created in place of default NSLocalizedString to be able to pick a specific language bundle from within the application which is actually our prime focus!

Consider this:

Let's say we are getting our language from some LanguageManager class of say ConfigurationManager class in our application or for the time being we can simply take en or es. 
So we take the language in a

NSString *lang = [ ConfigurationManager  language];
Next, based on the language received we pick the pathForResource i.e en.lproj or es.lproj and take the value in an another


NSString *path =  [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];

and that's it, 

finally we providethe NSBundle to pick the localized string as per the lang that was provided from the user like this:

NSBundle* languageBundle = [NSBundle bundleWithPath:path];
and make use of localizedStringForKey like this:


NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
Simple isn't it?
 return the 'str' string for the value of the localized string.

So finally our custom method looks like this, you can now call this method where ever you need to localize a string in your app:


/*
  @param: key(gives value of the key from Localizable.strings file)

*/
+(NSString*)languageSelectedStringForKey:(NSString *)key{
 
 NSString *lang = [ ConfigurationManager   language];
 NSString *path;
 if([lang isEqualToString:@"en"])
  path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
 else if([lang isEqualToString:@"es"])
  path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
 
 NSBundle* languageBundle = [NSBundle bundleWithPath:path];
 NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
 return str;
 
}


Hope you've enjoyed the post!
Follow CodeHunk for more.
Read more »

Saturday 22 September 2012

Apple and patents: You decide.

"HTC’s stock price has been sent into a tumble, after imports of new HTC handsets into the US were blocked in customs by an Apple complaint to the ITC"

"Apple wins preliminary injunction against Galaxy Tab 10.1 in US, Samsung appeals but in vain"

"Apples wins over HTC...
"Apple wins over Samsung...

10 years later
"Apple wins over nature" OMG!

and so on...


Hmm..seems like Apple's made its mind on now what could be the ever growing and fierce patent wars. Hold on Apple guys give me a break!,  that's getting really nervy now.


Apple patents a mockery to open source?

I have no idea what might be the position of the Open Source community at the moment. Sitting ducks? well quite comfortably. Open source with its heart, soul and mind will always be OPEN! No one could say you can not take that advantage of robust and short code while designing your web page. Its  for god's sake open and for the advantage of the technological upgrades to the society. 

I am actually deeply hurt by the court ruling the other day that gave Apple the somewhat ridiculous and absurd win over Samsung making a mockery of some legit lawsuits over some unthinkable reasons.

I have read the clauses of the lawsuit saying that it is not just the code, it could be a design, a metaphor, an approach.  Now if you see an application making use of a Dock, watch out for Apple lawyers.

So hold on James Gosling is coming to you to sue over the use of using Java in your code.
Coming to the rivalry, Samsung could actually reply back to Apple on numerous patent breaches, one that I personally think is I see Apple making use of the notification bar which was actually an invention of Android. Then Apple could reply back to Samsung on the shapes and so its like passing the ball game, which is eternal.


And so let me give you some idea about how this could go down. Its like a beast hovering over you for any of your movement waiting to catch you by your throat.

You see where is this all going to? It is like suing you of how you prepare your favorite dish. Its like suing you over the use of geometry in engineering, its like suing you over the use of a curved handle of your Grand Pa's walking stick. Phew! Come on this is never ending now...

This torn patent system is now taking us to a point where a patent holder controls the use of our imagination, creativity and advancement in creating the next step technological concepts.

Are we too harsh on Apple?

We believe that the true sense of innovation could come only through an unbiased and fair open source model, but wait. 
Isn't it the fact that for many years now Linux and open source model have been working on the Mac designs and UI for their systems? we know there has been a lot of achievements that came as a result of concepts originating from the open source model. 

But it is also a fact that for quite some time now Apple has been the driving force of all sorts of innovation to the latest technology in computing, hardware, gadgets and obviously the concepts(iPod, iPad, iPhone etc).

It is also a fact that the open source hasn't lately arrived with anything as revolutionizing as Apples hardware/software which has revolutionized the market beyond any recognition.

Also most of us are already eager to criticize on Apples idea and concepts out of our emotions and feelings for what we hear, do and for the sake of open source.
And most of them hasn't even used any Apple product to back any comparisons they make.


In the end what really offend us are the trolling patents of Apple which has screwed up Apple's reputation big time. Just for an example there was a news where Apple is taking issue with the trademark of an online grocery store that goes by the name A.pl.

Apple reasons that the store is deliberately using the similarity to promote itself while trying to confuse people with Apple's products. 

Imagine people getting confused over and iPhone and A.pl products.


Here's what the stackoverflow has to say about patents. Take a look here




In the end really it's up to you to decide.


Read more »

Tuesday 18 September 2012

The iPhone 5 launch: Disappointing?

Alright so Apple's iPhone 5 has broken all expectations about its sale and has smashed all records before hitting the shelves with more than 2 million pre-orders of the coveted smartphone in its first 24 Hours of launch on 12 September.

And the launch was no less dramatic as Apple gave the world the biggest hint in the shadow of number '5'.


Now what was expected?
A hell lot. Yup and so the old reminiscence of Steve Jobs's defining the use of the 'iPod Pocket' in our jeans at the launch of iPod then was there in our minds.


Honestly, to me it was disappointing to see Apple missing out on many areas to its contenders, specially Samsung Galaxy S3.  

Here's the top 3 areas I found was a bit of let down some may say, take a look:
  1. The biggest disappointment to me was from the developer's point of view where the bigger screen of the new iPhone demands changes in the old and running Apps of many publishers. That now requires some upgrades thanks to the bigger screen and not the hardware architecture mind you!
  2. There is no NFC feature for the new iPhone 5 and that's a real a let down considering the fact that much hype was created before the launch of iPhone 5 and for all the waiting buyers, well it is some disappointment. 
  3. The possible price without carrier remains a concern for less extravagant buyers. Well expected price, take $650 for now!
Beside the above three areas I think the others are less concerning and I say it because I have found the iPhone 5 to be more luxurious than driven towards some substantial tech upgrades.

But again somehow Apple has its own charm. Touch is aw-some with no lags and of course the graphics has always attracted many of us. The introduction of 4G-LTE, the enhanced battery life and many more.

Well next I am going to talk about the best free iPhone apps available. 
See ya folks !


Here's something interesting about the iPhone 5:

















Read more »

Saturday 15 September 2012

Cell height as per text length: iPhone Development

How to dynamically adjust cell's height as per the text length in iPhone development?









Well today I am going to start with a method that will calculate the cell's height appropriate to the length of text and then adjust the cell's height accordingly in an iPhone development.

The iPhone SDK comes with lots of interesting features and lets you design some of your custom made methods.

I was recently faced with a situation where my text was dynamic and hence I had to display it inside the cell that will automatically expand for the text's size. 
Previously my text would be half chunked! And so I came up with this solution.

Let's get started.

First we get our titles and subtitles for the display screen (If any)

NSString * title = @"Your title";
NSString * subTitle = @"Your Subtitle";


we can add it to the cell's height if we want like this:


[self heightOfCellWithTitle:title andSubtitle:subtitle]
Next we need to over ride the method "heightForRowAtIndexPath":


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

If we are getting our text of the cell dynamically, we can do something like this:
Here we pick the text from our sections located at indexPath.section:

NSString*  dynamicText = [[[sections objectAtIndex:indexPath.section] objectForKey:@"fields"] objectAtIndex:indexPath.row];

Next we define our dynamic text with font and its size, I preferred 12.0f.

dynamicText sizeWithFont:[UIFont systemFontOfSize:12.0f]

What we need next is the most important part, here we code what to expect from our cell when it receives our dynamic text:
We will ask the text to be constrained inside the width of the cell and so we do something like this:


[constrainedToSize:CGSizeMake(220.0, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap].height

We have set the CGFLOAT to MAX and we have also employed a wrap for our text this way it does not get chunked. And hence calculated the height of the dynamic text of the cell.

Finally we apply some constant to the above factor on its height this can be 0 or something. In my case it worked for an accurate 44.0!

So, finally our method code looks like this:


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSString*   dynamicText  =[[[sections objectAtIndex:indexPath.section] objectForKey:@"fields"] objectAtIndex:indexPath.row];
 
    return [ dynamicText  sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:
   CGSizeMake(220.0, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap].height + 44.0;
}
Beautiful isn't it ;)

Hope this helps you as it did helped me. Alright guys keep coming back for more. See u then.
Hasta la vista!
Read more »

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Hosting