Multilanguage Application in Iphone - iphone

How do I change my applications default language within my application? I am trying to change my applications language to Arabic, and I'm not sure how to accomplish this.

There is a way:
First make a different folder named as ar.lproj and put localizable.String
May following sample code help you. You can call this function in viewWillAppear with the key for which you need to get value.
-(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
if([[userDefault valueForKey:#"language_Selected"] intValue] == 0)
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
else if([[userDefault valueForKey:#"language_Selected"] intValue] == 1)
path = [[NSBundle mainBundle] pathForResource:#"ar" ofType:#"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[[languageBundle localizedStringForKey:key value:#"" table:nil] retain];
return str;
}
Hope you will understand the concept.

Related

Error in internationalization iPhone

I am using iOS 5, XCODE 4.2
In my loginViewController:
-(IBAction)loginButton:(id)sender{
textClass=[[MainScreenController alloc ] init];
NSLog(#"ShowText called");
textClass.selectedLanguage=currentLanguage;
[self.view addSubview:textClass.view] ;
NSLog(#"txtclaslan=%d",textClass.selectedLanguage);
}
In my MainViewController viewDidLoad: method:
mainScreenTitle.text=[self languageSelectedStringForKey:#"screenTitle"];
And in languageSelectedStringForKey:
-(NSString*) languageSelectedStringForKey:(NSString*) key{
NSString *path;
if(selectedLanguage==ENGLSIH_LANGUAGE)
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
else if(selectedLanguage==TURKISH_LANGUAGE)
path = [[NSBundle mainBundle] pathForResource:#"tr" ofType:#"lproj"];
else
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str = [languageBundle localizedStringForKey:key value:#"" table:nil];
return str;
}
Here in this method languageSelectedStringForKey when I load the MainViewController I my application crashes. I don't get any error just this line is highlighted
NSBundle* languageBundle = [NSBundle bundleWithPath:path]; < Thread 1
What can be the issue with this?
Thanks in advance
As suggested:
Looking at your code it seems that if the language is not English and not Turkish then path is not set.
You may also want to follow this advice from the [NSBundle localizedStringForKey:value:table:] method description:
"Using the user default NSShowNonLocalizedStrings, you can alter the behavior of localizedStringForKey:value:table: to log a message when the method can’t find a localized string. If you set this default to YES (in the global domain or in the application’s domain), then when the method can’t find a localized string in the table, it logs a message to the console and capitalizes key before returning it."

Runtime change the language/localization in Three20

Is it possible to change the Three20 language/localization at runtime without restarting the app?
Currently, I managed to change the language via altering the value of AppleLanguages in the main.m
There's a "hack" for it. You can load your own NSBundle with the localized text and use that NSBundle instead. Note that if the localized language file is missing, the app won't run, so make sure you set a correct language.
Above your AppDelegate implementation, add a custom NSBundle declaration:
static NSBundle *bundle = nil;
And then load the language you desire into that bundle:
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"he", nil] forKey:#"AppleLanguages"];
NSLocale* locale = TTCurrentLocale();
NSString *path = [[NSBundle mainBundle] pathForResource:[locale localeIdentifier] ofType:#"lproj" ];
bundle = [[NSBundle bundleWithPath:path] retain];
You will add a custom function in your AppDelegate to get the localized text too (instead of NSLocalizedString)
///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NSString*)get:(NSString*)key {
return [bundle localizedStringForKey:key value:nil table:nil];
}
To make things easier, you can add a static function in the pch file:
#import "AppDelegate.h"
#define MyLocalizedString(key, alt) [AppDelegate get:key]

Multiple SQLite Databases for Multiple Languages?

I would like to implement a multi language support for my app. So I created the Localizing.strings file and all that stuff and translated my interface. So far so good …
Now I want to duplicate my database in order to have a *.db-file for every single language. So I did and then I clicked via XCode on the "+" under the Localization tab. I now have a *.db-file in my en.lproj and de.lproj folder.
My problem: If I want to copy the db-files to the app's documents directory the *.db file is not available of course because it is in the *.lproj-folder. Is there any command to get the right lproj-folder?
To clarify my needs:
This doesn't work
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"mydatabase.db"]
… this does:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"de.lproj/mydatabase.db"]
… but I don't want to add the "de.lproj" and "en.lproj" etc. manually. Is there any way to fix it dynamically?
just do the following:
NSString *dbpathResource =
[[NSBundle mainBundle] pathForResource:#"databaseName" ofType:#"db"];
and if you have your localized .db file in xx.lproj so the correct database will be taken.
What you want is the current language locale, the following code should return the code:
NSArray *languagesArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languagesArray objectAtIndex:0];
You can then do the following
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.lproj/mydatabase.db", currentLanguage]];
You may want to check if the path exists and is a valid file, if not maybe use some default path like the one for English (en.lproj)
Edit: There is another way you can do this using NSLocale's preferred languages because then you get a list of the preferred languages, so some updated code for the first bit would be:
NSArray *languagesArray = [NSLocale preferredLanguages];
NSString *currentLanguage = [languagesArray objectAtIndex:0];
In the end, you'd end up with something like so:
NSString *pathComponent = [NSString stringWithFormat:#"%#.lproj/mydatabase.db", currentLanguage];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathComponent];
NSString *activePath = nil; // This will store the active language file
// Check if the file exists...
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
activePath = path;
} else {
activePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"en.lproj/mydatabase.db"]; // Fallback
}
Please note, the above code is untested but should suffice. You may need to modify it a little...
Something like this:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString * rootPath = [[NSBundle mainBundle] resourcePath];
NSString * resourcePath = [[NSBundle mainBundle] pathForResource: #"mydatabase" ofType: #"db" inDirectory: rootPath forLocalization: language];

How to create a NSMutableDictionary using dictionaryWithContentsOfFile: method

I want create and return a dictionary using the keys and values found in a file specified by a given path. I have my file on my Desktop:ciudades.txt (a human readable file!!! no a xml, just for practice). What method of my NSString i need to use and how? Please can somebody help me filling on my code the XXXXXXX. Thanks in advance
- (NSMutableDictionary)ciudades
{
if (!ciudades) {
NSString *path = [NSString XXXXXXXXX];
ciudades = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Define a function.
-(NSMutableDictionary*) ReadFileAsDictionaryForPath:(NSString* ) path
{
return [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Use it as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSMutableDictionary* myDictionary = [self ReadFileAsDictionaryForPath:path];
if(!myDictionary)
{
NSLog(#"Error while reading data from file at path:%#",path);
}
First add that file to your application bundle By adding a existing file from xcode to your project. Then use this method to get the file path for example I'm getting a image's path.
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
then try the dictionaryWithContentsOfFile method and see if it works or not.

internationalization in iphone application

my application run vary well but now when UISwitch button is on at that time i have to convet whole application in spanish when off then convert in to english how it possible plz give any replay for that.
i18n
Create the following structure:
resources/i18n/en.lproj/Localizable.strings
resources/i18n/es.lproj/Localizable.strings
Create an additional directory with the corresponding two letter code for each additional language supported.
It's recommended to encode Localized.strings in UTF-16. You can convert between encodings in the inspector pane of XCode.
If the files are recognized as i18n resources, they will be presented like this:
A sample file has the following content:
"hello"="hola";
Then use the following in your program:
NSString *string = NSLocalizedString(#"hello", nil);
Choose language dynamically
To change the language for your application dynamically use this code:
#implementation Language
static NSBundle *bundle = nil;
+(void)initialize {
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:#"AppleLanguages"];
NSString *current = [[languages objectAtIndex:0] retain];
[self setLanguage:current];
}
/*
example calls:
[Language setLanguage:#"es"];
[Language setLanguage:#"en"];
*/
+(void)setLanguage:(NSString *)code {
NSLog(#"preferredLang: %#", code);
NSString *path = [[ NSBundle mainBundle ] pathForResource:code ofType:#"lproj" ];
// Use bundle = [NSBundle mainBundle] if you
// dont have all localization files in your project.
bundle = [[NSBundle bundleWithPath:path] retain];
}
+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
return [bundle localizedStringForKey:key value:alternate table:nil];
}
#end
Then translate your strings like this:
NSString *hello [Language get:#"hello", nil, nil];
The code above was originally posted by Mauro Delrio as an answer to How to force NSLocalizedString to use a specific language.