Per-Document Locale

Our app lets users pick the locale for a particular document (macOS document-based AppKit), then produce outputs using various inputs (user-provided and others) in that locale. We've written much of the plumbing, but are getting really close to String Catalogs and the like.

What can we do to use AppKit and Xcode's localization tools correctly and efficiently in this case?

We already have "Localization" objects, and are splitting the work into multiple String Catalogs for clarity. In each case the app will need to use the given Localization to produce (or replace) text at runtime, including the result of formatters

Hi TyngJJ, thanks for the question.

I can only give some general advices based on your description. You may use the Locale parameter on the LocalizedStringResource type. SwiftUI API like Text use the locale from the current environment automatically. So you can write .environment(\.locale; your locale) as well.

Let me know if that works for your use case.

Feel free to share more about your specific use case.

In this case the UI has very little contribution, because the localization is of/for the document's "output", so what is shown onscreen will be e.g. a PDF in PDFView.

We're still looking to use Apple's localization tools throughout, but the context isn't the user's session, or the whole app, it's each document. The app will ship with the same lproj, etc, but they will be used explicitly in our code as abstracted in the Localization object.

From what you described I think your best bet is still to rely on the LocalizedStringResource and passing the locale parameter picked by the user to extract the strings from the bundle for the given locale from your .lproj.

One caveat, you may need to pay attention to locale fallback (i.e. user has selected Spanish Mexican but your App is supporting Spanish only). LocalizedStringResource implements an automatic fallback that may or may not match your requirements.

Yes, I was wondering if there were any tips or pitfalls when the localization won't be applied by the frameworks themselves, and if the results can't be examined using the UI tools.

Per-Document Locale
 
 
Q