{"id":20642,"date":"2018-02-20T15:08:08","date_gmt":"2018-02-20T20:08:08","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20642"},"modified":"2019-05-21T16:05:00","modified_gmt":"2019-05-21T20:05:00","slug":"how-to-use-adaptive-width-strings-for-localization","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/02\/20\/how-to-use-adaptive-width-strings-for-localization\/","title":{"rendered":"How to Use Adaptive Width Strings for Localization"},"content":{"rendered":"<p><a href=\"https:\/\/cocoaengineering.com\/2018\/02\/11\/how-to-use-adaptive-width-strings-for-localization-on-ios\/\">Daniel Mart&iacute;n<\/a>:<\/p>\n<blockquote cite=\"https:\/\/cocoaengineering.com\/2018\/02\/11\/how-to-use-adaptive-width-strings-for-localization-on-ios\/\"><p>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&rsquo;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?).<\/p><p>[&#8230;]<\/p><p>In order to solve this problem, if you don&rsquo;t want to engineer your own solution Apple introduced &ldquo;adaptive strings&rdquo; with iOS 9. This feature is based on <a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/MacOSX\/Conceptual\/BPInternational\/StringsdictFileFormat\/StringsdictFileFormat.html\">string dictionaries<\/a> (.stringsdict files), which are commonly used to support pluralization rules in apps.<\/p><p>[&#8230;]<\/p><p>For each key that you want to support multiple localizations, add a <code>NSStringVariableWidthRuleType<\/code> dictionary with key\/value pairs, one for each &ldquo;class&rdquo; of screen width that you want to support.<\/p><\/blockquote>\n<p>This relies on a private <code>__NSVariableWidthString<\/code> string subclass, which survives conversion to a Swift <code>String<\/code>, though you may need to be careful not to lose it if you manipulate the <code>String<\/code> rather than passing it directly to a control.<\/p>\n\n<p><a href=\"https:\/\/pspdfkit.com\/blog\/2018\/localizations-at-pspdfkit\/\">Christoph Mantler<\/a>:<\/p>\n<blockquote cite=\"https:\/\/pspdfkit.com\/blog\/2018\/localizations-at-pspdfkit\/\">\n<p>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 &mdash; this time on our end &mdash; to ensure that all strings are correct. There are also tests in place to check whether or not the code syntax is correct.<\/p>\n<p>[&#8230;]<\/p>\n<p>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)[&#8230;]<\/p>\n<\/blockquote>\n\n<p id=\"how-to-use-adaptive-width-strings-for-localization-update-2019-04-10\">Update (2019-04-10): <a href=\"https:\/\/twitter.com\/kuba_suder\/status\/1115990343537840128\">Kuba Suder<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/kuba_suder\/status\/1115990343537840128\">\n<p>Looks like the i18n pluralization rules used in Cocoa &ldquo;stringsdict&rdquo; files are a Unicode standard - there are some nice charts <a href=\"http:\/\/www.unicode.org\/cldr\/charts\/latest\/supplemental\/language_plural_rules.html\">here<\/a>.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Daniel Mart&iacute;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-04-10T18:34:57Z","apple_news_api_id":"1914b53d-4f08-4ceb-8928-62e8fe150f6f","apple_news_api_modified_at":"2019-05-21T20:05:05Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AGRS1PU8ITOuJKGLo_hUPbw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,1472,309,30,1529,71,901,258],"class_list":["post-20642","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-11","tag-localization","tag-mac","tag-macos-10-13","tag-programming","tag-swift-programming-language","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=20642"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20642\/revisions"}],"predecessor-version":[{"id":24947,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20642\/revisions\/24947"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}