11

I have an Application with 2 targets. String resources in the app are scattered across the code and storyboard file. Now I need totally distinct set of strings for both the targets (Although in English language though for both targets). I was able to create 2 different versions of Localized.strings per string following this link - Alternative strings for different targets of same App - use NSLocalizedString?

But I could not find any such similar approach for segregating the Mainstoryboard.strings file per target. Should i make 2 copies of the MAinstoryboard.strings file and assign for each target. I haven't tried this this but something tells me that this might not be the best approach.

Surprisingly nothing on this on Apple tutorials. Any suggestions ?

Community
  • 1
  • 1
Dibzmania
  • 1,934
  • 1
  • 15
  • 32
  • My guess is you need to set all the strings by code. Implement a method that gets you the proper string depending on target by accessing the proper bundle path. – hasan Feb 16 '17 at 12:45
  • I am already doing that for the strings which are in code. But I have a whole lot of strings in storyboard and relocating them on code is not an option (huge rework) – Dibzmania Feb 16 '17 at 12:55
  • What about adding a fake localisation language and localise your storyboard with instead of having a hall another storyboard. you can check the device preferred language on app start and reset the app language accordingly (only on one of the targets). – hasan Feb 16 '17 at 13:39
  • I will add an answer with some code snippets to demonstrate better – hasan Feb 16 '17 at 13:41
  • So, how did you solve you problem? – hasan Feb 22 '17 at 23:28

2 Answers2

3

A code snippet for adding a fake language that could be the solution:

language = Bundle.main.preferredLocalizations[0]

if target == SOME_TARGET {

    if language == "en" {
        language = "fr" // fake language    
    } else if language == "ar" {
        language = "rs" // another fake language
    } else {
        // default app language
        language = "whatever" // default fake language 
    }

    UserDefaults.standard.set([language], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
} else {
    // do nothing
}

Another proposed solution. Is to have two storyboard localisation files for each language. and replace them manually using the OS X finder when you switch between targets. But, I guess that wont help for the strings on the storyboard itself.

hasan
  • 23,815
  • 10
  • 63
  • 101
  • I haven't managed the time to look into the first solution you proposed. The second solution won't work for me because i have a CI setup which generates app bundles for different targets and there is no manual intervention – Dibzmania Feb 23 '17 at 23:45
1

I did make a Github project for Localization problems.

Using my iOS components, you will be able to put localizedString directly in your Storyboard file.

Xcode Example

CZ54
  • 5,488
  • 1
  • 24
  • 39