Ad

Our DNA is written in Swift
Jump

XLIFF Fix

I reported in an earlier post that there are some gotchas trying to localize an app with the combination of Xcode’s XLIFF export and POEditor.com. The latter stated that it is not their fault and somebody responsible for the former thanked me for my sample.

A future fix in Xcode notwithstanding, I found it necessary to create the Localizable and Main storyboard strings file from the XLIFF files exported from the translation site. This way I could be certain that the translations I care about all all accounted for.

The XLIFF format is a simple XML format. As such it can be parsed by means of NSXMLParser in contrast to strings files for which I had to create lots of custom code in the past. Then the exercise is simply to output the strings contained in the target tags into appropriately named strings files.

As a bonus exercise I wrote a command line utility in Swift which can even deal with multiple XLIFF files in sequence. It creates an lproj folder for each language and there it outputs the strings files contained in the XLIFF. The screenshot shows the main part of the utility.

XLIFFix main

In Swift, you access the argument count via Process.argc and the individual arguments via Process.arguments. Fortunately those are already expanded – if you have wild cards in the name – and in swift String format. Note that contrary to C there is no main function, the code just starts. I grouped the code for writing one strings file into a writeFile function.

The other thing I learned today is to properly write “for i in the range from 1 to – not including – something”. You use the key word “in” and the range operator.

Conclusions

It was interesting to write my first command line utility in Swift. Once you know about a few differences that Swift has here compared to C or Objective-C the rest is straightforward. You can leverage all you know from working with Apple’s Foundation framework.

The source code for XLIFFix utility is on available on GitHub. If you find it useful and have some comments please get in touch.


Categories: Recipes

Leave a Comment

%d bloggers like this: