multiple targets with one and only one different localisation each - ios

My project has 6 targets to one source code base. The 6 targets are different programs with different content thanks to different pre-processor statements.
I defined 2 localisations which are French and English.
BUT 4 of the programs should be French only and 2 should be English only.
At the moment, I get French and English for all 6 programs.
How do I remove english from the 4 French programs and french from the 2 English programs please?
I tried target memberships but checking the boxes are the same for either the Localizable.strings (french ) and the localizable.strings (english).
Also if I try to uncheck a localisation for a target, it tries to delete it...
So either I am not doing correctly or I just don't know how to do it
Thanks in advance for any help
Cheers,
geebee

I found an answer to this ; so for completion in the main.m file:
// ---- target 1 app 1
#ifdef target1_VERSION
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"fr", nil]
forKey:#"AppleLanguages"];
#endif
// ---- target 2 app 2
#ifdef target2_VERSION
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil]
forKey:#"AppleLanguages"];
#endif

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.

Disable Target Localization in Xcode

In our project we have multiple build targets which each use an English, French and Base (English) localization files. Some of these are string files while others are localized storyboard files that have been constructed to include both of these languages.
What is the best way to, on a single build target, restrict the localization so that it only uses English and not French while at the same time keeping it so all the other build targets still have both English and French localizations working? I try to not include the French localizations for that specific build but that removes the entire localization file since this seems to be done at project level.
I found a few posts here but nothing that worked for disabling a single localization language from a specific build target while leaving the others alone.
We are working in Xcode 6 currently.
I'm not sure it's exactly what you're looking for, but if you want to force your app to be displayed in a specific language, you can do that without removing the other localizations from your project. You can change your main.m implementation to:
int main(int argc, char *argv[])
{
#autoreleasepool
{
if (/*Some condition for this build*/)
{
NSString *newLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:kNewLanguageCode];
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:newLanguage, nil] forKey:#"AppleLanguages"];
}
else // If you want other conditions, or just do nothing if you want to support the device's current localization settings
{
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"en", nil] forKey:#"AppleLanguages"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
I know it's not exactly what you asked about, but I thought that it may solve your problem from a slightly different direction, so I hope this helps. Good Luck!

iOS Localization (setting it in the app)

in my application I must have two languages, but the problem is that they should be switched by pressing a button in the application (on the first screen, or screen settings). As I understand, all the methods of localization (Localizing the Storyboard, Localizing Dynamic Strings) based on the language settings of the iPhone. The only option that comes to my mind - do it by the record in NSUserDefault about language preference, and in ViewDidLoad methods of all ViewControllers check the record about language and in accordance with it set strings, picture and so on. Can it be done on a more clever way?
Try this:
If de is the new language selected by the user. Also assure that you are reinitiating the current view.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"de", nil]
forKey:#"AppleLanguages"];
See the below link and source code
http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html
and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps
All the best.
this tutorial & sample app helpful you more
localization-tutorial-for-ios
ios-programming-tutorial-localization-apps
Ya The way you are following for Localization is not up to Mark.
Please Follow this beautiful tutorial http://www.raywenderlich.com/2876/localization-tutorial-for-ios
You have to manually mange the currently selected language into user default and load the bundle folder according to selected language key like, i have done this and worked like charm
if ([currentLanguage isEqualToString:#"fr"]) { // If French.
languageBundle = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
languageBundle = [NSBundle bundleWithPath:path];
}

Localization in iOS 7 : Selects the language settings used previously

I am using localization in iOS 7 and have the localizable string files for German and English.
When ever i select the corresponding language the localisation works perfect. But if i switch to any other language, then the display is based on the previous selection.
I want it to select english incase of any other language selections, Any thoughts would be appreciated.
AFAIK, this behaviour is a feature, but undocumented? :)
In iOS7, users can set a sorted list of preferred languages. For instance; a French user fluent in German, but not in English, could set French, German, and English as language preference. It's a great feature!!!
So, I think you shouldn't override this feature.
Users can set English as 2nd language easily, choosing 1st English and then choosing it's preferred main language.
Use the below checking in your main.m
NSString * deviceLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSArray *supportedLanguages = [NSArray arrayWithObjects:#"en",#"de", nil];
if ([supportedLanguages containsObject:deviceLanguage])
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:deviceLanguage, nil] forKey:#"AppleLanguages"];
else
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil] forKey:#"AppleLanguages"];

How to select an storyboard programmatically?

I have an iOS 5 app that supports 3 different languages. Localizing an storyboard creates a copy in each language and each one must be translated.
The language selection depends on system settings, so if my iPhone has 'English' as a system language the choosen storyboard in my application will be the English one. If I change to Catalan, my app will show Catalan words.
This approach has several drawbacks in my opinion:
An app cannot be translated to languages not supported by Apple. This could be important for moniroty languages ( Catalan has not been supported until iOS 5 ).
If a user wants to have my app in Catalan but the rest of the system in English ?
So my question is how can I select the storyboard language at app startup time ? Is it possible at all ? Also it will work for strings localized using NSLocalizedString ?
Regards,
JoanBa
It seems like you can split the storyboard into localized versions just like localized.strings as to how to do it?
This question has been answered before although I haven't tried from experience.
After reading the Jacob answer and testing a little bit, this is the code that has worked for me:
int main(int argc, char *argv[])
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Reset system defaults to get the complete language list.
[defaults removeObjectForKey:#"AppleLanguages"];
// Default language choosen by user.
NSString *defLanguage = [defaults objectForKey:#"Language"];
NSArray *sysLangugages = [defaults arrayForKey:#"AppleLanguages"];
// System default language: first element of array.
NSString *sysLanguage = [sysLangugages objectAtIndex:0];
NSArray *array = [NSArray arrayWithObjects:defLanguage, sysLanguage, nil];
[defaults setObject:array forKey:#"AppleLanguages"];
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Just restarting the app after changing the default language, all localized resources are changed: strings and storyboards.
Also tried this code in AppDelegate.m, in method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
but it doesn't work. Only in main.m works fine. I don't know why.
Note that with this code you can have an application with a language not supported by iOS.
Regards,
JoanBa

Resources