Localization in ios is usually done using this way.
But the main problem with this approach is that to view the application
in other language the language has to be changes from the settings of
the ios device.
In this post i will present an approach in which you can set the language of the application from within the application.
but before starting lets understand the current localization procedure. we use NSLocalizedString() to get the localized version of any string, which acts as a key to match the key value pair in the localized version of Localizable.strings file.what infact NSLocalizedString() calls in background is [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] where [NSBundle mainBundle] is the default main bundle.
The trick to use is to get the value of localizedStringForKey from the bundle of your choice. So what we will do is that depending on the language selected we will select the specific bundle and use the [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] to get the localized value of the string.
so lets get started
1- Localize your application using the method describe here. i recommend using this method just so that you can use "genstrings" to create string table.
2- put the localized version of string values in the localized Localizable.strings.
3- Replace the NSLocalizedString with any function name of your choice, i will be using languageSelectedStringForKey .
4- Write a method definition and implementation of -(NSString*) languageSelectedStringForKey:(NSString*) key where it is accessible in the scope where the localized string is needed.
5- in the languageSelectedStringForKey method select the appropriate bundle depending on the language selected and call [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] on the bundle.
e.g.
for NSString *path= [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
and then get the string like this
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
str here is the localized string.
again all which i described above have been put in this code and i have placed it on github for any reference.
'프로그래밍 > iOS' 카테고리의 다른 글
OAuthConsumer를 이용한 xAuth (0) | 2012.02.29 |
---|---|
localize (0) | 2012.02.17 |
로컬라이징 (0) | 2012.02.17 |
문자열 지역화 (0) | 2012.02.17 |
Localizing iPhone Apps – Internationalization (0) | 2012.02.17 |