How do I assign a template file's localized name?

My application allows a user to begin by selecting a template document. For example, in Pages you could choose to start with the "Essay" template. That template document is copied and opened.


I'm running into a localization issue and cannot find out how to localize the displayed name of the resulting copied file. Again, an example with Pages. If my language is set to French and I select the "Reportage" (Essay) template, the name of the copied file is "Reportage".


My template files are located in my app's bundle resources. I'm using UIDocumentBrowserViewController and the following method to create the document


func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void)


The importHandler is eventually called with .copy like so:


importHandler(url, .copy)


My template files are quite large and it does not make sense to ship duplicates with localized names. We have a workaround where we first create a copy with the properly localized file name. We then use that resulting file's URL in importHandler(url, .copy), but having to duplicate the file twice seems wasteful.


I've seen reference to localizedNameKey in the documentation, but there's very little information about how to set that value. How do I make use of the localizedNameKey? Is there some plist where this per file localization info is specified?

Replies

Are your templates/documents bundles/packages? If so, you can localize the CFBundleDisplayName key from the Info.plist by using Infoplist.strings file.

Our documents are file bundles. Unfortunatley, I've been unable get this working. Do you have any other information or a pointer to some documentation on how to set this up properly?


Here's what I did. I tried to mimic the structure of the app bundle. For example, I set up one of our template bundle files for German like so (each additional locale would get its own lproj directory and InfoPlist.strings):


TemplateBundle/

--data

--Info.plist

--de.lproj

----InfoPlist.strings. (strings file with "CFBundleDisplayName" = "Some German";)


I gather my URLs as follows:


        let mgr = FileManager.default
        let templatesURL = Bundle.main.resourceURL!.appendingPathComponent("Templates")
        templateURLs = try! mgr.contentsOfDirectory(at: templatesURL, includingPropertiesForKeys: [.localizedNameKey], options: [])

Checking in the debugger with po templateURLs[0].resourceValues(forKeys: [.localizedNameKey]) shows only the English name.


I am not 100% sure about what should or shouldn't be included in the Info.plist. I've set it up as follows:


  CFBundleDevelopmentRegion
  en
  CFBundleDisplayName
  Template Bundle
  CFBundleInfoDictionaryVersion
  6.0
  CFBundlePackageType
  APPL
  CFBundleIdentifier
  com.mycompany.templatebundle
  CFBundleName
  templatebundle
  CFBundleSignature
  ????

When you say po shows only the English name, I assume you mean even when the system language is set to German.


What is the name of the template bundle in the file system? Or, put another way, what is the lastPathComponent of the URL? I don't really know about iOS, but I know that on macOS, the localized display name is only used if the file name (minus extension) matches the unlocalized display name in the Info.plist. That's so that if the user renames the bundle, their renaming overrides any localization stuff. (Doesn't necessarily apply to templates, but is for the general case.)

Hey Ken, thank you so much for your help.


You are correct about the language setting. I've tried both setting the scheme "Application Language" option to German and just in case that had an issue, I set my iPad system language to German to be 100% sure that I was testing in German.


I made an error in the directory structure I showed in my previous reply. To closer matach what I really have on my file system, TemplateBundle should be


"Template Bundle.myextension"


and in the plist I have the CFBundleDisplayName as shown in the plist above (so that the URL's lastPathComponent (minus the extension) indeed matches the unlocalized CFBundleDisplayName).

You're welcome, but I'm afraid I'm stumped. Maybe file bundle localization doesn't work like I thought on iOS. Or maybe somebody else can off help.