[Objc] Change App language

Hi all,
today a simple trick to change the app languagewithout change phone language.

Add a Settings bundle in your project

Add settings.bundle in app

Now, open your settings (better if you open it as Source Code):

Screenshot 2015-06-03 16.16.48

And add your languages,

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Key</key>
<string>app_language</string>
<key>Title</key>
<string>App Language</string>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>DefaultValue</key>
<string>auto</string>
<key>Titles</key>
<array>
<string>Auto</string>
<string>English</string>
<string>Italian</string>
<string>French</string>
<string>Spanish</string>
<string>German</string>
</array>
<key>Values</key>
<array>
<string>auto</string>
<string>en</string>
<string>it</string>
<string>fr</string>
<string>es</string>
<string>de</string>
</array>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>[/code]

Build and run, in your device Settings, you should see the option list:

iOS Simulator Screen Shot 03.giu.2015 16.13.53

iOS Simulator Screen Shot 03.giu.2015 16.13.54

iOS Simulator Screen Shot 03.giu.2015 16.13.57

How to use it?

You need to read the app_language key to retrieve the language value at start.

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[NSUserDefaults standardUserDefaults] stringForKey:@"app_language"];[/code]

Load different localization string file:

Open your main.m file, and add this method,

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]void configureAppCustomLanguage()
{
NSString *forcedLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:@"app_language"];
NSString *key = @"AppleLanguages";

if ( forcedLanguage &amp;&amp; forcedLanguage.length &amp;&amp; ![forcedLanguage isEqualToString:@"auto"] )
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSLocale preferredLanguages] forKey:@"PreferredLanguages"];
[defaults setObject:[NSArray arrayWithObject:forcedLanguage] forKey:key];
[defaults synchronize];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
forcedLanguage = [defaults valueForKey:key];
if ( forcedLanguage )
{
[defaults removeObjectForKey:key];
}

[defaults synchronize];
}
}[/code]

this method load your app settings language key string and override Apple default.

Next, override your int main() with this one:

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]int main(int argc, char * argv[])
{
@autoreleasepool {

// change language on start!
configureAppCustomLanguage();

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}[/code]


Now add your language strings in your project, for example:

Screenshot 2015-06-03 16.34.08

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]// Localizable.strings (Italian)
"sample-string-key" = "ITA";

// Localizable.strings (English)
"sample-string-key" = "ENG";

// etc…[/code]

Well, now, your app load string localizations file, based on your language settings, without to change phone language.


Try it!

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]NSLog(@"%@", NSLocalizedString(@"sample-string-key", nil));[/code]

enjoy.

Alberto Pasca
Latest posts by Alberto Pasca (see all)