I have a couple of targets for my app. I am trying to localize them differently. I have found this to be pretty easy when dealing with a Localizable.strings file and the NSLocalizedString macro. To get this to work I simply adjust the target membership of the Localizable.strings file for the target I am working with. So my project file structure looks something like:
project
target1
en.lproj
Localizable.strings
it.lproj
Localizable.strings
target2
en.lproj
Localizable.strings
it.lproj
Localizable.strings
Now my project has 2 targets being built with completely different english translations. Perfect, this is what I want.
My difficulty comes into play when I try to do the same thing for the *.strings files that are generated when localizing a view. My deployment target is 6.0 and I am using storyboards with base internationalization. So when I add a view to my project structure it will look like:
project
target1
Base.lproj
SomeStoryboard.storyboard
en.lproj
Localizable.strings
SomeStoryboard.strings
it.lproj
Localizable.strings
SomeStoryboard.strings
target2
en.lproj
Localizable.strings
SomeStoryboard.strings
it.lproj
Localizable.strings
SomeStoryboard.strings
My goal was to let each target have a different localization for each target, yet maintain the same Base storyboard. I also wanted to avoid something like: naming the target2 storyboard string files something like REPLACEMENT_SomeStoryboard.string and then writing a script that for every file named such in a given target delete SomeStoryboard.string and rename REPLACEMENT_SomeStoryboard.string to SomeStoryboard.string.
Attempt #1
So what I tried to do was remove the target1 en.lproj/SomeStoryboard.strings & it.lproj/SomeStoryboard.strings from the target2 membership. But this also removed the Base.lproj/SomeStoryboard.storyboard from the target2 membership. So basically membership was managed as if it were one file, which makes sense.
Attempt #2
The second thing I tried was to add target1 SomeStoryboard.storyboard to the membership of target2 and also create a SomeStoryboard.strings file and add that to the membership of target2. I tried adding this extra strings file into the Copy Bundle Resources Phase. I also tried adding the Copy Files Phase (after the Copy Bundle Resources Phase). Either way it appeared to only occasionally replace the SomeStoryboard.strings from target1. So the end result was that sometimes target2 picked up the localization from target1 and sometimes it was localized from target2.
Try using the method described at http://smoothlocalize.com/ (click on the first tutorial in the first main box). You download a small library that allows you to use Localizable.string for your resource strings the same way you do for .m files.
Related
I have 2 Apps : Free & Full
I have 2 Languages : English & German
This results in 4 different App-Names
How can I achieve this.
Actually all my targets have the name "Free" in it, even it the the Full Version.
I know:
I have 1 InfoPlist.strings File, which is multiplied by Localization and I can assign them to targets. So for 1 App I can localize the BundleDisplayName.
But how can I assign the specific names for the second app ?
PlistBuddy is your friend here. What you need to do is update your plist in a build time.
You have to create custom scheme for every target you have (I assume you are already has that)
In your xcode go to scheme and add post action like that to custom scheme target:
Here is the code:
infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
/usr/libexec/PlistBuddy -c "Set CFBundleName some_new_name_here" "$infoplist"
Important thing with this approach is that you're not change nothing in your plist file but with post-actions you already changing the packet prepared for device, so you keep your code repository nice and clean.
Associate InfoPlist.strings to targets
From the Apple Developer Technical Notes:
If your app supports localization, be sure to localize CFBundleDisplayName by adding it to all your language-specific InfoPlist.strings files. Furthermore, be sure to use a name that complies with the App Review Guidelines for your app.
Prerequisites
I am assuming you already have multiple targets.
I am also assuming you already have an InfoPlist.strings file. If your do not, code > File > File... > Resource > Strings File > Next > Save As: InfoPlist > Base.lproj > Target: the english target > Create
Ensure you have enabled this in your Info.plist:
<key>LSHasLocalizedDisplayName</key>
<true/>
Step by step
In Project Navigator, select InfoPlist.strings.
In File Inspector > Localization, select all the languages. This will create and/or copy said InfoPlist.strings into their respective locations.
Would you want to add languages, you achieve this in Project Navigator > project > Project & Target List > Project > Info > Localizations > +. Again, keep Use Base Internationalization.
Unfortunately, when you execute step 2., Xcode does all the magic for you. It is worth noting that in versions of Xcode where that magic did not happen, you had to do the associations by hand. Step 5. is about reverting the magic.
Remove InfoPlist.strings from the Project Navigator. Of course, only Remove References when prompted.
In the Finder, locate en.lproj, drag it onto your Project Navigator, pick Create Groups, add to target your English, or Base target, Finish
Repeat step 6 with de.lproj, fr.lproj, each time dragging the entire .lproj from the Finder, and associating with the appropriate target.
Some .lproj may contain other localized files for which you want to enjoy the magic of step 3. Simply remove their references.
You are done.
The final setup, for say the German language, will look like this in the File Inspector:
References:
Share localization across targets:
This is the general method to achieving localization. See https://stackoverflow.com/a/33749062/218152.
Specific localization to specific targets:
This is only useful if you want to explicitly control which languages go into which target. See https://stackoverflow.com/a/33791181/218152.
I'm trying to prepare my app to be passed off to translators for localization, but I'm running into some issues. I added the file "Localizable.strings" to my project, and I have "Use Base Internationalization" checked.
Looking at my app in Xcode, however, I don't see any "Base.lproj" or "en.lproj" or .lproj files for any language, though when I open my app in Finder I can see an "en.lproj" and an "es.lproj" folder.
I'm guessing this may be the reason that the genstrings utility isn't working properly for me. I have many calls in my code like self.navigationItem.title = NSLocalizedString(...), but when I execute genstrings *.swift, nothing changes in Localizable.strings, and I still can't see any .lproj folders.
Xcode doesn't automatically add those files you generated into your build path, you have to manually add them. Also, genstrings generate Localizable.strings file that you can then localize. To generate the lproj files you were talking about, you have to localize your Main.storyboard file.
I am trying to understand Base.lproj but I am not still understanding it. If I create a new simple project with a view controller and Swift+Storyboard in XCode 6 beta 4 I get a Base.lproj folder out of the box containing the storyboard file.
I then add a simpel NSLocalizedString(...) in my view controller for testing purposes. This requires a Localizable.strings file. When I create one should I add it in the Base.lproj or should I add it in a specific language folder?
If I change the Localization native development region key in Info.plist to de for german and I no Localizable.strings file for de in the de.lproj folder it seems to fallback on Base.lproj if Localizable.strings file is found there. However, if I add a Localizable.strings file in the de.lproj folder but don't add the property (key + value) the app fails during runtime. It seems like Base.lproj isn't searched.
I don't understand which language Base is and when it is used? Is it only for Storyboard files?
I was under the impression that base.lproj is used in cases where the current language/ localisation file does not have a property key. For instance if I do have Main_Title = "Title Text"; in base.lproj and absent in en.lproj while the current language is English, I assumed iOS will find it in base.lproj but that is not the case.
Your initial conclusion is correct:
Base is only used when you did not support that language in localizable.strings
The specific language localizable.strings folder is used when device/ app is using that language
If you do not provide support for a specific file/ string key in a language and you have a folder for that language, it will throw run-time warning
In case of storyboard files and xibs if you only have base.lproj folder which contains your views, notwithstanding how many languages you support, it will not fail (this is what I am doing as well).
Only one Resource file is displaying When i'm adding Localizable.strings file for any language. My steps are,
Selected My main app's project file in xcode and found info option is there
in info - Localization - + option
Note: there is only one resource file (InfoPlist.strings) is available to add Language.
3- then Finish, but there is no Localizable.strings file is displaying in xcode but there is InfoPlist.strings file displaying.
But if i do these same steps in sample app which is working fine , I can able to select Multiple resource files And also I can see many Localizable.strings & InfoPlist.strings
in xcode.
Like:
Localizable.strings (English)
Localizable.strings (Spanish)
InfoPlist.Strings (English)
InfoPlist.Strings (Spanish)
Can anyone please tell me the solution to add multiple resource files when localizing any language?
If you want to make your app to support multiple languages, then you'll need to add .strings files to your project, make sure that you should name it "Localizable.strings" only, and need to put in particular language folder,
See this question and my answer there,
Localized filename should always be named Localizable.strings, and that file is within the particular language folder, for English, en.lproj and for French fr.lproj like wise.
Localizable.strings file should be inside the Base.lproj folder.
I deleted the Localizations of the project from the project's info by mistake. Both the storyboard and the related language were removed.
I got back the storyboard from previous file but when I want to add a new Localization, a window popped out and asked me to "Choose files and reference language to create English localization" and there is no resource file at all.
How can I add back the localizations?
Adding back a localization
When you delete your localizations sometimes you can get that situation where you can't add back any localizations in Xcode. Go to the command-line and create a ISO 639-1 two-letter language abbreviation folder with the lproj extension somewhere within your project.
Create an empty language folder
For example:
${SRCROOT}/Resources/Translations/nb.lproj
or
${SRCROOT}/Resources/Translations/en.lproj
Re-add language files to project
In the folder create an empty file called Localizable.strings and add this file to your project. Don't add the .lproj folder, just the contents. Then when you go back into the project file you will see the language under localizations. Now when you have added this, you should see the localization in your project.
No checkbox
If the added file, like a storyboard, doesn't have a checkbox next to it indicating that is part of that language localization you might need to round trip them through another language. To do this add a new language in the project. It should prompt you to copy the existing localized files over to the new language. Then delete the broken localization and re-add it.
My Example
As a specific example, I added the storyboard in the nb.lproj directory to the project. The Norwegian Bokmål appears in the project, but the checkbox for the language in the storyboard inspector won't let me add it. I created nb_NO in the project and it prompted me to copy over the storyboard. Then the storyboard had that checkbox checked. I deleted the 'nb' localization and re-added it. Now everything was the way I wanted it.
I had to solve same problem, and found one solution:
You need to find in finder your *.xcodeproj file and open it as container (ctrl+click on it and you will see it in context menu)
Here you can see project.pbxproj file, open it in Xcode. Its large (but still readable file).
(it will be better to make some copy on save spot, before you start)
Now its the task to add the missing language to this file, and its done.(U can try it on other project with localizations).
There is paragraph you need to add. Search in this file for this /* End PBXSourcesBuildPhase section */(its paragraph right before localization paragraph which is missing) and add something like this after:
/* Begin PBXVariantGroup section */
27548D921611B0BE008EA1CD /* Localizable.strings */ = {
isa = PBXVariantGroup;
children = (
27548D941611B0BE008EA1CD /* en */,
);
name = Localizable.strings;
path = ../Code;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */
You have to do some changes first. Look at the long number 27548D921611B0BE008EA1CD, its ID of the localization file (or some xib file, or other localizable file...), if you dont have any in the procject, just add some Localizable.string file to the project, reopen(or refresh) this project.pbxpro, and find this Localizable.string you should find something like this:
275490591611B0BE008EA1CD /* Localizable.strings in Resources */
and this is it what you need to replace in localization paragraph
also you need to do right settings on path parametr and sourceTree. This twos are readable in fileinspector in xcode.
The "sourceTree" param is Location - mostly its relative to group so sourceTree = "<group>";
The path is path relative from the group to the file (if you dont know, try to make some localization files in different project and look at project.pbxpro, what all this works)
After this you can save project.pbxpro, and you should see in xcode info section "en" localization and you can add more localizations and all other localization stuff.
In xcode 6, do the following:
With the command line, create the following directory/file structure somewhere outside your project directory:
/en.lproj/InfoPlist.strings
/en.lproj/Localizable.strings
In Xcode, open your project and choose File>Add Files to ProjectName to add the recently created en.lproj directory.
After that, you will see English in the Localizations list.
This is an old topic, but after having this issue, and then trying the solution (and having it not work exactly), I thought I'd give details on what DID work.
Like the original poster, I accidentally deleted all of my localizations and was given no obvious recourse in Xcode-- in the project settings under Info it simply said 'this project has not been localized', and when I clicked on the plus button beneath, a blank list of resource files was presented.
I followed #Cameron's guide but found that language folders with Localizable.strings files were already present in Finder, but weren't being acknowledged by Xcode. I couldn't decide how to proceed, but I happened to see an unrelated comment about adding files to Xcode, so I tried adding the existing en.lproj folder and its Localizable.strings file.
This worked, and the listing for English as the development language appeared under Info as well.
Hopefully this will help anyone who experiences the same problem!
Simply create a Folder "Base.lproj" in finder and paste the Xib you want to localize inside it, and add this folder to your project. Now you can able to add other languages without issue.
I've followed a similar approach of #user2070775 reply.
Firstyl when you delete Storyboard and Launchscreen to use your own creation or code programmatically, on Localization you might get similar following empty screen.
Then firstly you should know the language code for example to use French localization its fr etc
With project folder in the Project Navigator you must add New Group -> {LANGUAGE_CODE}.lproj. And then you should add New File -> Strings File -> Localizable.strings. In the #user2070775 answer, also InfoPlist.strings file has added so I added too for now.
In the Localizable.strings file you can add <KEY> = <VALUE> pairs for string to localizated.
Our pair is "this-is-key" = "and-this-is-value-for-spesific-language-on-localizable-string"; for now.
HINT: In key-value pair, when I did not put the semicolon, the XCode
gives error for this situation.
Until now, the following project scheme, must the result of you have.
Additionally, when you looked at the Project -> Info -> Localizations the French or etc. language must be added like in the following image.
TESTING
Basically you can get the results of the what've you done
The remaining part can be set from the Simulator. You must change the simulator language, for this answer the correct one is French.
Go to Settings -> General -> Language & Region -> French and run the project again.