Search code examples
iosxcodeinternationalizationxliff

Weird hidden characters in exported XLIFF file


Whenever I try to 'Export For Internationalization', the exported file contains a weird hidden character, making it unparsable for XLIFF editors. The problem seemed to be in the original .string files, somehow the weird character was inserted in those files. I have since deleted the weird character but whenever I export it still sneaks into the xliff file. I tried cleaning and rebuilding the project, restarting Xcode... none of that seems to work.

Is Xcode somehow using a cached version of the 'bad' .strings file containing the bad character?

Using Xcode

If I try Editor > Export For Localization I get:

/usr/bin/xmllint exited with status 1

Xcode message after exporting for localization


Using Terminal

When I run it from Terminal like so:

xcodebuild -exportLocalizations -localizationPath /Users/Kymer/Downloads/Wolf -project Wolf.xcodeproj -exportLanguage fr

I get the following errors:

parser error : attributes construct error
parser error : Couldn't find end of Start Tag trans-unit
parser error : PCDATA invalid Char value 19
parser error : PCDATA invalid Char value 19
parser error : Opening and ending tag mismatch
parser error : invalid character in attribute value
parser error : attributes construct error
parser error : Couldn't find end of Start Tag
parser error : PCDATA invalid Char value

/Uxcodebuild: error: /usr/bin/xmllint exited with status 1

In both cases the exported xliff file contains the weird hidden character upon inspection with Sublime Text:

Screenshot of 'hidden' character in Sublime Text

If I manually remove the bad characters the file is perfectly readable by xliff-editors but that's not a good long-term solution of course.


Solution

  • I found the problem: when exporting to an XLIFF file Xcode doesn't look at your .string files, it is all generated from the project itself (i.e. it looks at all NSLocalizedString calls and your storyboards). Which makes sense. I found the weird hidden character in one of my code files. Removing it from the source file fixed the export issue.

    Easiest XLIFF workflow

    I'll also mention this for future reference: the easiest way to add a new language to your project is to first use the command line:

    cd to the your project and run:

    xcodebuild -exportLocalizations -localizationPath <path>  -project <projectname>.xcodeproj -exportLanguage <language code>
    

    That creates a new XLIFF file and will correctly set the target language in the file (source language will be your base language). A translator can now easily add all necessary translations. Afterwards you can import the translated XLIFF file back into Xcode (select target and Editor > Import localizations). Xcode will then generate all necessary .string files.

    Updating existing language: If you add new UI elements and want to update an existing localization language, you can simply export an existing localization (select target and Editor > Export for localization). That XLIFF file will contain all previous translations together with the new strings. A translator simply has to fill in the 'blank' lines. There's no need to touch the .string files yourself, because managing that manually is a pain (especially with the crazy Storyboard ID's).