how to force an ios app to use a certain localization? - ios

i have an app that have two localizations (hebrew and english), and i've been asked to cancel the english localization so that whatever your device's language, the app will be the same (but save the localization for future use).
the localization is via Localizable.strings and also Xib localization (allot of them).
is there a way to tell the app to always use a certain localization and ignore the device language?
thanks !

I have adapted this to Swift and use this to test localizations with ease. When placed in AppDelegate->didFinishLaunchingWithOptions the simulator needs to run twice to get the correct settings, but maybe there is a better way.
In the example Danish localization is used and more can be found here: Locale codes for iPhone lproj folders
let langCultureCode: String = "da"
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject([langCultureCode], forKey: "AppleLanguages")
defaults.synchronize()
And to remove the defaults once again:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("AppleLanguages")
defaults.synchronize()

in that case just set the defaults key AppleLanguages EARLY at startup to override IOS settings
(EARLY = before a xib/storyboard or NSLocalizedString is used)
NSString *langCultureCode = #"he";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#[langCultureCode] forKey:#"AppleLanguages"];
[defaults synchronize];

Related

iOS: Localizable.strings doesn't work

This has been bothering for days now.
I've set up Arabic localisation in my project and I've set my device Language to Arabic.
When I run my app from Xcode with the run scheme option set to use Arabic Localization, localisation works fine.
When I run the app without Xcode, the app uses English.
I've read the answers to similar questions but none of them have worked for me thus far.
I am using NSLocalizableString(#"login",#""); to load the strings from the Localizable.strings file. This works fine as long as I set the Run scheme localization option to Arabic.
I've tried uninstalling the app, cleaning the project and then re-installing. Now the app uses the Localizabe.strings key names instead of their arabic values.
The Localizable.strings file is named correctly, and is listed under "Copy Bundle Resources".
The Localizable.strings is perfectly formatter. I've verified this using plutil.
What else could I be missing?
Example:
-(void) viewDidLoad
{
[super viewDidLoad];
// ...
[self setupLocalization];
}
-(void) setupLocalization
{
self.mailAddress.placeholder = NSLocalizedString(#"email_address", #"");
self.password.placeholder = NSLocalizedString(#"password", #"");
}
I found out what the issue is:
AppDelegate's application:didFinishLoadingWithOptions: contained this snippet of code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#[#"en"] forKey:#"AppleLanguages"];
[defaults synchronize];
which forces NSLocalizedString() to return English values.
The project was outsourced and I had no idea that the snippet was there.
Are you calling NSLocalizedString(_:,comment:) to convert your strings to localized strings?
If you are not doing that, that is the reason it is not working.

How to localize server-side data that i get in my app

I am using Italian language in my app. I have made a Localizable.string file in English and one in Italian.
It's working OK, and everything is working fine with static data, but there are mostly string responses from the server side. How can I convert them in Italian?
If I get data in different-different any string variable then how I will compare it from localizable.string(Italian).
And I also have server side data in English and Italian language. But in app it comes from server.
Thank you..
you can get your application current language like this :
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *appleLanguagesArray = [userDefaults objectForKey:#"AppleLanguages"];
// Get Current language of an application...
NSString *currentLanguage = [appleLanguagesArray objectAtIndex:0];
if(currentLanguage==#"en") {
// English language.
}
The other option is if you have two buttons for two languages, then you can check currently which language is selected.

Storing bool in shared NSUserDefaults always returns false

I've set up App Groups for my app so that I can access the same NSUserDefaults in the app and in my keyboard extension. I have a problem though. I can successfully write a boolean to the defaults but when I access it in the keyboard, it always returns false. I know for a fact the id it's using for the key and the suite name are the exact same. I verified App Groups is indeed enabled for both the app and the keyboard and the suite name matches the app group identifier exactly. What could cause it to always be false when accessing from within the keyboard?
In the app:
NSUserDefaults *SharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.myname.sharedPrefsName"];
[SharedDefaults setBool:YES forKey:#"com.myname.appname.dataName"];
[SharedDefaults synchronize];
I did confirm it did successfully get set to YES the next time I launch the app:
NSUserDefaults *SharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.myname.sharedPrefsName"];
BOOL dataIsYes = [SharedDefaults boolForKey:#"com.myname.appname.dataName"]; //YES
Yet in the keyboard it is always false:
let sharedprefs = NSUserDefaults(suiteName: "group.com.myname.sharedPrefsName")!
let data = sharedprefs.boolForKey("com.myname.appname.dataName") //false
I found the answer in the App Extension Programming guide.
By default, a keyboard has no network access and cannot share a container with its containing app. To enable these things, set the value of the RequestsOpenAccess Boolean key in the Info.plist file to YES.
To fix the issue I had to change the RequestsOpenAccess field to YES in the keyboard's Info.plist > NSExtension > NSExtensionAttributes > RequestOpenAccess. Then remove the keyboard in Settings, delete the app, run it again, and add the keyboard again. Then be sure to tap on the keyboard name and then flip the switch to Allow Full Access.
If users don't enable Full Access, the extension will still be able to access the shared container (iOS 8.3+ only) but it will not be able to write to it, for security and privacy reasons. In 8.2- you cannot read from it without open access granted.
Don't use alloc/init for NSUserDefaults. It's a singleton. You're supposed to use the class method
standardUserDefaults to get a pointer to the shared user defaults object:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

iOS: changing system language doesn't affect the app

I've got a real pain with my current project which has few Localizable.strings files and the problem is that the language doesn't change when user change it in system settings - NSLocalizedString macros continues to return previous language strings. I can't really undestand what wrong, I've been doing everyhing 'like in manual'. Neigher app restarting nor device restarting helps. Only reinstall does.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
this always returns the language being set on application first launch.

According to the selected language either from application or from device can I convert the language in application?

I have to make my application multi language supported on the selection of particular radio buttons(i.e. English, French, German etc...). For that what I have to do? any external framework or API is required?
To do so from where I have to start & if any inbuilt method is there then please suggest me.
I want to add another thing, can it be possible according to the selected language of the device we can get all the effect in application same as the selected device language?
In code you can access current locale and language that user set in Settings:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(#"Current Locale: %#", [[NSLocale currentLocale] localeIdentifier]);
NSLog(#"Current language: %#", currentLanguage);
You need to support Localization in the app for all the hardcoded strings.
The below code returns the code for the device language selected. eg: en, zh, etc..
let langStr = Locale.current.languageCode
Based on the code returned, the repective Localizable.strings file displays the value for the strings.

Resources