Swift Localization: Genstrings - ios

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.

Related

XCode 9 localization export/import doesn't include NSLocalizedString() or comments

Situation:
We use XCode exporting/importing tools to generate localizable files for our translators. This was going fine in XCode 8. Since we updated to XCode 9 we see a couple of problems.
Problems:
When exporting a localization via Editor-->Export for localization...-->Development language only, it doesn't include NSLocalizedString() key/values as it should according to this documentation: When you export localizations, Xcode searches your code for the macros and includes the strings files in the exported localization file for translation.
Comments that are added to the Localizable.strings development-language file seem to be non-existent in the export .xliff file.
When importing a translated .xliff file, XCode seems to delete all the comments that are in the file. It gets replaced by "(No Comment)"
Configuration:
XCode 9.0 (9A235)
Dev language: NL
Localized languages: Base, NL, DE, BE, LU
Objective-C & Swift hybrid project
What I've tried:
I have tried looking at XCode 9 documentation or changelog to see what has changed in Localization, though nothing I can find mentions this change
I can get my NSLocalizedString() strings to show up in the export when I first generate the development Localizable.strings file manually, by using the commandline command find ./ -name "*.m" -print0 | xargs -0 xcrun extractLocStrings -o . (I run it a second time for .swift files as my project is a swift/objc hybrid). Then combine the results, which let my strings show up in the export, however without comments, which are needed as guides for translators.
Question:
Is this an XCode 9 bug? Did I miss any documentation or new feature/option for localization exports/imports? Are there workarounds?
This is an older question, but I had this same issue with Xcode 10.
The solution is to simply place the strings files (e.g. InfoPlist.strings) in the same folder as plist.info. Only then are existing translations included in localization exports.
Previously I had them in a dedicated "Localizations" group. While this works for the localization itself (i.e. when running the app), localization exports seem to only include translations from existing strings files that are "near" plist.info.
What fixed both importing and exporting for me was turning off Base Internationalization by unchecking Use Base Internationalization under Project > Info > Localizations. I set all my translations in code, so I don't need it enabled for my .storyboard and .xib files.
Previously, when it was enabled, the "Localizable.strings (Base)" file didn't have anything in it, but Xcode 8 still seemed to work properly by using what was in my "Localizable (English)" file when exporting. For Xcode 9, if I had the source text in Localizable (Base) then exporting worked fine, but I still had problems with importing.
I am using Xcode 9.4.1.
I finally found the reason of the issue.
The issue exists because Xcode never saved files in the encoding it promised.
There are two places to set text encoding, one is "Default text encoding", which including a "convert existing files on save" checkbox.
There is also a file specified "Text Encoding" on each file when you open it. If you don't specify the text encoding on the file, the "Default text encoding" is used.
However, Xcode never saved the file as it promised. For example, if you choose a file specified text encoding, say to "UTF-16", an alert window shows with "Cancel", "Reinterpret", "Convert".
As the alert said, if you chose convert, Xcode would convert the file to the encoding. However, it didn't.
So when you exporting the translations, the file is opened with the encoding it never converted to and ignored.
You can verify that by these steps.
choose a source file.
change it to another text encoding.
in the alert window, choose convert
in Xcode, right click the file and choose Show in Finder.
quit Xcode.
open the file with another text editor that can deal with text encoding, mine is BBEdit.
in BBEdit, choose file -> save as
Now you can see, that the file is not saved in the encoding as it promised.
Solution
The solution is easy. For files that you specified the text encoding, keep it saved in the encoding that it specified. You can do that with other editors, like BBEdit, by save as and choose the right encoding.
For those files that are not specified text encoding. Be sure them are saved as "Default text encoding". If not, change the "Default text encoding" so you won't need to change each file.

iOS: Localization gone wrong

I'm starting to get confused..I've wanted to finally add my translation file for all strings to my app according to ray wenderlich's tutorial. But somehow now everything got messed up. I removed now every localization again, which deleted my strings file, the storyboard etc. And now I have no localization language set in the project settings and I've copied all files back from the trash.
But my localized string (NSLocalizedString(..)) somehow don't load..or load something else. I've changed some of them to check if my strings file in the project explorer is actually used but it still uses the old strings and I have no idea from where..I've checked the project folder and there aren't any other files.
How can I set up everything again that it works? How can I set the strings file it should use? And how should I set up then the localization, with or without base language?
Edit: And in the simulator I cannot get my app running..it's showing only a black screen. Whereas on my iphone it still works (if I use my iphone from Xcode).
I guess I've deleted all my languages in project settings and now I cannot add any localization anymore :-(
Here's what I would try:
Check your project folder for any leftover .lproj folders / .strings file and delete
Clean / Build
Start localization from scratch following SmoothLocalize's Tutorial
This should delete all old files and create a new default localizable.strings, and then you can add more languages.

I deleted the Localizations in Xcode, then I cannot add any Localizations back

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.

iOS Localization how to add an existed folder with localization files

How to add an existed folder with localization files.
I have an existed project with english as base, then have en.lproj.
And I have other localization files in file system names with ja.lproj.
However, If i directly import folder ja.lproj to xcode, seems not work, in project tab, it shows 0 files localized, and while start app, it will crash.
I know it may work, I choose one english version file, add plus in file inspector, then will add another lang version, however, it's not convenient, and new file will locate in en.lproj sub-folder, it's strange.
anybody can help me ?
Thanks a lot!
I don't think you would be able to do that with folders. Although you can do that with files. Just define different versions of the same file for different languages. Like this:

Localizable.strings not found at runtime

I develop ios application (iPhone) in MonoTouch(5.2.12)/MonoDevelop(3.0.3.2)
I tried to localize it.
I created localization folders (de.lproj)(Add->New folder) and I placed 'Localizable.strings' file there. This file was added to project (Add->Add files ...).
Localization doesn't work.
I find out that localization files (Localizable.strings) don't locate in runtime place
(/user/jpa/Library/Application Support/iPhone Simulator/5.1/Applications/A99 ...B13/MyApp.app/de.lproj/), even though in MonoDevelop they are.
Why? What is wrong? File is UTF-8 coded. (It doesn't depict with UTF-16)
Thanks.
I believe you'll need to set the build action of your files to "Content" (right click file -> Build Action -> Content).
How do you call your Localized string ? Can you give us a piece of code ?
You don't need to create manually the localization folders, just:
create a Localizable.string file (Right click in project explorer then "New File")
select it
open the Xcode right panel (if not already open)
click on + in the Localization part, and select the language
This method will prevent from manipulation errors because Xcode create the localization folders itself.

Resources