title: Swift package manager localization
body: <p>I'm wondering, how localization works with the swift package. I saw the WWDC20/10169 video - <strong>Swift packages: Resources and localization</strong>. I'm trying to do the same things within my own project.</p>
  <p>If I do same configuration (as on WWDC video) and add <code>.environment(\.locale, Locale(identifier: &quot;es&quot;))</code> to <code>View</code> preview, it works (but only when test inside sp).</p>
  <p>The problems start when I try to use localized resources inside the actual app.
  The process is very simple - I just use a view with a localized string from the package inside the app. For test purposes I launch my app with a specific locale (scheme settings) - the result, I always get <code>en</code> translation.</p>
  <p>My manifest:</p>
  <pre><code>let package = Package(
    name: &quot;MyLibrary&quot;,
    defaultLocalization: &quot;en&quot;,
    platforms: [
      .iOS(.v14),
    ],
    products: [
      .library(
        name: &quot;MyLibrary&quot;,
        targets: [&quot;MyLibrary&quot;]),
    ],
    dependencies: [
    ],
    targets: [
      .target(
        name: &quot;MyLibrary&quot;,
        dependencies: [],
        path: &quot;Sources&quot;
      ),
      .testTarget(
        name: &quot;MyLibraryTests&quot;,
        dependencies: [&quot;MyLibrary&quot;]),
    ]
  )
  </code></pre>
  <p>Structure of the package:</p>
  <p><a href="https://i.stack.imgur.com/WRrWp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WRrWp.png" alt="Structure of the package" /></a></p>
  <p><code>resource_bundle_accessor</code> is generated fine - so I have an access to <code>Bundle.module</code>, and indeed I can list all localizations as expected - <code>en</code> and <code>es</code>.</p>
  <p>I also added supported languages into the project itself:</p>
  <p><a href="https://i.stack.imgur.com/l31gV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l31gV.png" alt="Supported languages" /></a></p>
  <p>Here is a quick demo:</p>
  <p><a href="https://i.stack.imgur.com/JApgd.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JApgd.gif" alt="demo" /></a></p>
  <p>For generating localized string I want to use SwiftGen:</p>
  <pre><code>extension L10n {
    private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -&gt; String {
      let format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
      return String(format: format, locale: Locale.current, arguments: args)
    }
  }
  
  // swiftlint:disable convenience_type
  private final class BundleToken {
    static let bundle: Bundle = {
      #if SWIFT_PACKAGE
      return Bundle.module
      #else
      return Bundle(for: BundleToken.self)
      #endif
    }()
  }
  // swiftlint:enable convenience_type
  </code></pre>
  <p>Alternatively, I tested</p>
  <pre><code>let text = NSLocalizedString(&quot;welcome&quot;, tableName: &quot;Localizable&quot;, bundle: .module, value: &quot;&quot;, comment: &quot;&quot;)
  </code></pre>
  <p>or similar from <code>Bundle</code> - <code>Bundle.module.localizedString</code> method and from <code>SwiftUI</code> <code>Text(&quot;welcome&quot;, bundle: .module)</code>.</p>
  <p>The result the same - it won't change the language - always I get dev language - <code>en</code>.</p>
  <p>I also can confirm, that build contains localization (inside testLocalization.app in lib bundle):</p>
  <p><a href="https://i.stack.imgur.com/6MD8z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6MD8z.png" alt="testLocalization.app" /></a></p>
  <p>Here is a <a href="https://drive.google.com/file/d/1Wb5QYBWdVWd9GkTabWP7luSCpvCzMFsN/view?usp=sharing" rel="nofollow noreferrer">link for project</a>.</p>
  <p>So my question - what I'm doing incorrectly? Where is the error?</p>