2.6 · Bundle, resources, localization tables

An app is a folder of code and files called a bundle. Finding those files, and choosing the right language for the words inside them, is mostly a question of which bundle you are asking.

Mental model. Every resource lookup is “ask this bundle for this name”. When it comes back nil or in the wrong language, check the bundle first.

Bundles and resources

Finding files inside an app, a package or a test.

  1. What is a Bundle, and how do you load a resource from it? 2.6.1 · L1
    A Bundle is a directory with a known layout that holds code and resources: images, JSON, sounds, Info.plist, and localisation folders. Bundle.main is the app…
  2. Why does Bundle.main fail in packages, extensions and tests, and what do you use instead? 2.6.2 · L3
    Bundle.main is the running process’s main bundle, not “the bundle my code is in”. In an app extension it is the extension. In a unit test it is the test runner…

Localization

Looking up strings, formatting them for the language, and knowing which language wins.

  1. How do you localise strings: Localizable.strings, string catalogs, String(localized:)? 2.6.3 · L1
    You mark user-visible text with a lookup instead of a literal: String(localized: "Hello") (iOS 15), NSLocalizedString, or in SwiftUI a string literal passed to…
  2. How do you handle plurals and word order in localised strings? 2.6.4 · L2
    Never write count == 1 ? "item" : "items". Plural rules differ by language: English has two forms (one, other), Russian four, Arabic six. Put plural variants…
  3. How does the app pick its language at runtime, and how is that different from Locale? 2.6.5 · L3
    The system keeps an ordered list of the user’s preferred languages. At launch, Bundle matches that list against the localisations your app actually ships (its…

More in Foundation & stdlib