Wednesday, November 21, 2018

Modern Localization With Xcode

Eric Blair:

What makes XLIFF support so interesting to me as a developer is how the files are generated. While Xcode will happily process your base language’s .strings file, it can also directly scan your project for NSLocalizedString calls and generate the XLIFF files straight from code. Once you cross that bridge, you no longer have to worry about things like mismatched string identifiers or tediously updating your .strings files because your latest updated added 12 strings and edited 3 more across 7 different languages. Since XLIFF is a variant of XML, you can also use your favorite XML processing techniques to automate any changes to the file. For instance, I will sometimes tag dummy strings with a known comment and use a XSL transformations to remove them before sending them off to my translators.

That’s not to say the XLIFF workflow was painless from the get-go. In fact, the XLIFF round-trip process didn’t gain support for stringsdict files until Xcode 9. Furthermore, that stringsdict support was initially limited to the language pluralization rules. It’s only as of Xcode 10.1 that the export and import workflow gained support for round-tripping variable-width strings. Previous version of Xcode not only ignored variable-width strings when exporting, they also failed to honor the width variants on import and would overwrite any existing width translations.

With the addition of improved variable-width string support, the stringsdict support in the XLIFF workflow now feels complete. The generation process is aware of the language-specific pluralization rules and the exported files includes the appropriate translation requests instead of just mirroring the items in your development language’s stringsdict file… and yes, you do need to provide a stringsdict file in your base language. Unlike .strings’s 1:1 relationship between the identifier and the display string, the NSLocalizedString() call site doesn’t contain enough information to synthesize a stringsdict entry.

I’ve long liked generating keys for NSLocalizedString() calls in code. Blair mentions two tools that go the other way and generate code from the strings files.

Previously: How to Use Adaptive Width Strings for Localization.

Comments RSS · Twitter

Leave a Comment