Tuesday, February 20, 2018

How to Use Adaptive Width Strings for Localization

Daniel Martín:

One of the challenges of localization lies in the length of translated texts. Languages like German are especially problematic because of its longer texts compared to English. In addition to that, translators are often working with isolated strings, where the only context they get (if they get any) is the place where the string is going to be placed and its purpose, but they don’t usually have any idea about the available physical space on the screen. Moreover, the available space may not be constant as the same app may be run on an iPhone or an iPad (or even a Mac in the future?).

[…]

In order to solve this problem, if you don’t want to engineer your own solution Apple introduced “adaptive strings” with iOS 9. This feature is based on string dictionaries (.stringsdict files), which are commonly used to support pluralization rules in apps.

[…]

For each key that you want to support multiple localizations, add a NSStringVariableWidthRuleType dictionary with key/value pairs, one for each “class” of screen width that you want to support.

This relies on a private __NSVariableWidthString string subclass, which survives conversion to a Swift String, though you may need to be careful not to lose it if you manipulate the String rather than passing it directly to a control.

Christoph Mantler:

When Wordcrafts is finished with the translations, the company will push its changes in the dedicated branch, create a pull request on GitHub, and send us an email stating that everything is finished. Once this occurs, the translations go through a second process of QA — this time on our end — to ensure that all strings are correct. There are also tests in place to check whether or not the code syntax is correct.

[…]

Below is a great example of how a plural string will look when translated. The key is defined on the top, and below are all the various cases that can apply for this string (zero, one, other)[…]

Update (2019-04-10): Kuba Suder:

Looks like the i18n pluralization rules used in Cocoa “stringsdict” files are a Unicode standard - there are some nice charts here.

1 Comment RSS · Twitter

Note that "normal" stringsdict usage (for doing pluralized strings) also returns a private string subclass, so the variable-width stuff here isn't new.

Also does anyone know if variable-width strings can be localized as well? Are the values just plain strings, or are they string formats that are then looked back up in the defined formats list in the stringsdict?

Leave a Comment