Problem with multiple Xcode targets Localizable.string

I have a project with several goals that represent the same application with only different styles and translations.

Since almost the entire project looks the same for every purpose, I need to have only a few lines in the Localizable.strings file, which I need to be different. And I don’t want to copy the entire huge Localizable.strings file into each project just because it has several lines.

I only need to have 1 line file because of the third-party libraries / SDKs that are included in the project. Therefore, I cannot use tableName for localizedString .

Problem - I need the flexibility to override only a few lines from Localizable.strings for each purpose separately. And I don't like the idea of ​​just copying the whole file to each target, because it will lead to an annoying stream in the future if I have 10 goals, and I need to add 1 line for everyone.

goal is to have 1 huge Localizable.strings file with all the strings included, which will be distributed for all purposes and have a small configuration for each purpose for strings that should report different. Thus, the target file should merge and override the one that is shared.

AFAIK is not Xcode based, so I was probably looking for a script that would make it work.

So, the script should analyze the general and target localizable files, combine them, and if some keys are defined in both cases, then it should use one of the target file.

Can someone help me with such a script?


PS A similar problem exists with .xcassets , and CocoaPods solves it by combining several assets into 1, and works as expected - if for some purposes there is an object containing an image with the same name that is already included in the shared asset, then one from the target will replace.

PS2. A similar function is supported for Android developers - each image, each translation can be overridden by a "child" flag or whatever it is called :)

+5
source share
2 answers

TL DR:

Sample project: https://github.com/JakubMazur/SO45279964


OK, the easiest way would be a shell / python script, because it will work for each build server. I assume that you have a different scheme for each goal (otherwise it would be pointless). So what you can do is:

Let's say your goal is called:

  • target1
  • target2
  • target3

1) Creating separate files contains all the lines that should be different (I put it in the Localizable directory.

Your Localizable.strings file might look like this:

 "someGeneralString" = "General string 1"; "AppName" = "This is a string that you probably need to change"; "someOtherGeneralString" = "General string 2"; 

And any of your targetX.strings files might look like this:

 "AppName" = "target[x]" 

And this is how it should look in your project:

enter image description here

Note that localizable target files should only have target membership for one purpose, but your Localizable.strings should be for all purposes!

This is all for project configuration. Let's move on to writing scripts (for this I will use python):

 #!/usr/bin/python import sys supportedLanguages = ["en","pl"] commonPath = ".lproj/Localizable.strings" keys = ["AppName"] class CopyLocalizable(): target = "" def __init__(self,arg): self.target = arg self.perform() def perform(self): for lang in supportedLanguages: pathToLocalizable = lang+commonPath textToFile = "" with open(pathToLocalizable,"r") as languageFile: for line in languageFile.readlines(): for key in keys: if key in line: textToFile += self.foundAndReplace(key,lang) else: textToFile += line self.saveInFile(pathToLocalizable,textToFile) def foundAndReplace(self,key,lang): pathToTargetFile = "Localizable/"+lang+".lproj/"+self.target+".strings" with open(pathToTargetFile,"r") as targetFile: for targetLine in targetFile.readlines(): if key in targetLine: return targetLine def saveInFile(self,file,stringToSave): with open(file,"w+") as languageFile: languageFile.write(stringToSave) 

You can optimize it yourself. This is a simpler script. I can think of doing the job.

And at the end, let it automate it a bit: - Go to your goal - add a new build phase - Add a new script:

 export PATH="/usr/local/bin:$PATH" cd SO45279964/ python localize.py target[x] 

and watch the magic happen;)

http://www.giphy.com/gifs/26n6NKgiwYvuQk7WU

Here you can find an example project that I created to run this example: https://github.com/JakubMazur/SO45279964

+2
source

To make it simple, define a macro for each target in the build settings and define target lines in the macro section, for example

 #ifdef __TARGET__ //key values in localizable file #endif 
0
source

Source: https://habr.com/ru/post/1270149/


All Articles