Localization in SwiftUI and WKWebView

Hi,

I have an app that is currently in English, and it uses SwiftUI and WKWebViews converting HTML into a webpage.

Is there an easy way to add localization to these separate parts?

I've looked around online, but haven't found anything close to what I'm looking for.

Thanks!
Answered by Developer Tools Engineer in 613951022
The general Xcode localization process is described here. See all articles under "Localize your app".

Executive Summary:
  1. Select languages in the project editor.

  2. Click Localize… in the inspector for any non-code resources.

  3. Go back to the project editor. Editor > Export for Localization… in the menu bar.

  4. Have someone (or yourself) translate the exported xcloc/XLIFF files using their favorite localization tool and return them.

  5. Back to project editor. Editor > Import Localizations… in the menu bar.

To have strings in your code participate in this process, you can do any of the following for your use case:

Non-UI Code
Whether you use Swift or Objective-C for your non-UI code, you can wrap your string in NSLocalized to specify localized strings in your code. This is also what you would do for UI code that is not SwiftUI. Note that NSLocalizedString expects a static string. You can use String.localizedStringWithFormat(NSLocalizedString("stringVariable is %@", comment: "comment for translator context"), stringVariable)

SwiftUI
In SwiftUI views and view modifiers that take strings typically take LocalizedStringKeys as arguments, so they will automatically be treated as localized strings. If you are using the export/import process described above, you'll want to make sure these strings are wrapped in Text objects in order for them to be exported into the xcloc file.

HTML
  • If this is coming from a server, it may be best to handle localization on the server side. For example, you could have the app tell the server the locale in a request, then the server can respond with the right file.

  • If this is static local HTML, you could keep it in its own file and just hit Localize… in the file inspector.

  • If you're programmatically generating the HTML and the strings themselves are the only things needing localized (not the HTML structure), you could just create the strings using NSLocalizedString and pass them to the HTML.

Accepted Answer
The general Xcode localization process is described here. See all articles under "Localize your app".

Executive Summary:
  1. Select languages in the project editor.

  2. Click Localize… in the inspector for any non-code resources.

  3. Go back to the project editor. Editor > Export for Localization… in the menu bar.

  4. Have someone (or yourself) translate the exported xcloc/XLIFF files using their favorite localization tool and return them.

  5. Back to project editor. Editor > Import Localizations… in the menu bar.

To have strings in your code participate in this process, you can do any of the following for your use case:

Non-UI Code
Whether you use Swift or Objective-C for your non-UI code, you can wrap your string in NSLocalized to specify localized strings in your code. This is also what you would do for UI code that is not SwiftUI. Note that NSLocalizedString expects a static string. You can use String.localizedStringWithFormat(NSLocalizedString("stringVariable is %@", comment: "comment for translator context"), stringVariable)

SwiftUI
In SwiftUI views and view modifiers that take strings typically take LocalizedStringKeys as arguments, so they will automatically be treated as localized strings. If you are using the export/import process described above, you'll want to make sure these strings are wrapped in Text objects in order for them to be exported into the xcloc file.

HTML
  • If this is coming from a server, it may be best to handle localization on the server side. For example, you could have the app tell the server the locale in a request, then the server can respond with the right file.

  • If this is static local HTML, you could keep it in its own file and just hit Localize… in the file inspector.

  • If you're programmatically generating the HTML and the strings themselves are the only things needing localized (not the HTML structure), you could just create the strings using NSLocalizedString and pass them to the HTML.

Localization in SwiftUI and WKWebView
 
 
Q