0

I develop a Swift 5 framework that runs on iOS 12 and above for iPhone and iPad.

I added French localization in addition to English, but the project info tab shows there's no localized files for both languages as shown on the image below:

enter image description here

When I run Xcode Editor|Export for localization I get the bundled files (including the fr.xliff file). I’ve edited fr.xliff with Xcode to translate some strings, and imported it back. But nothing changed in the app.

Btw, I can’t see any of the *.strings files in the project files hierarchy, which sounds weird.

[Update]

So I manually added Localizable.Strings file, and added English and French.

  1. When I import xliff files, the Localizable.strings files remains empty. No apparent issue pops up.

  2. When I put the key=value list in the string file, the NSLocalizedString() does not return the right localised version of the string (it always return the key).

Help appreciated.

Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95

2 Answers2

1

Found what's wrong: I had to use the full (extended) version of NSLocalizedString() in order to make the function call get the translation from my framework Localizable.strings files (rather than the client application), as follows:

NSLocalizedString("Continue", tableName: tableName, bundle: bundle, comment: "Continue in the button title of UIAladdinOnboardingViewController")

Where:

  • tableName: a let global that hold the name of my localisable string file (in my case "Aladdin.Localizable"). I did this to avoid potential collision of .strings files (as my framework name is Aladdin);
  • bundle: a let global in my the framework name space to designates the framework model bundle to pick the string from. I defined it like this: let bundle = Bundle(for: ExtendedAppDelegate.self) where ExtendedAppDelegate is one of my framework custom class.

Side note of importance:

Btw, don’t put your interpolating string as the key parameter of the NSLocalizedString call, otherwise it’s gonna be interpolated at runtime, leading to an interpolated string that won't exist in your string file.

Don’t do that:

let s = NSLocalizedString("{Start your \(introductoryPeriod) free trial.}", tableName: tableName, bundle: bundle, comment: "in price display")

Do this:

let s = String(format: NSLocalizedString("{Start your %@ free trial.}", tableName: tableName, bundle: bundle, comment: "{Start your \\(introductoryPeriod) free trial.}"), introductoryPeriod)

With the corresponding Aladdin.Localizable.strings (en):

/* {Start your \(introductoryPeriod) free trial.} (in price display) */
"{Start your %@ free trial.}" = "{Start your %@ free trial.}";

To generate the strings:

I used the gestrings from the terminal as follows:

 $genstrings -o en.lproj/ *.swift

 $genstrings -o fr.lproj/ *.swift

And so on for every target languages you have. Finally, do the translation straight in the string files.

Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95
  • 2
    Actually, instead of `String(format: NSLocalizedString(...), ...)` you typically want to use `String.localizedStringWithFormat(NSLocalizedString(...), ... )`. [localizedStringWithFormat](https://developer.apple.com/documentation/swift/string/1414192-localizedstringwithformat) takes into account the current locale for number formatting, and also [handles format strings with mixed language direction](https://stackoverflow.com/a/55569952/3727099). – guru_meditator Jan 21 '20 at 05:10
  • Good to know this addition. – Stéphane de Luca Jan 22 '20 at 09:52
0

in Localization you can Localize your string directly from Storyboard.

after adding your language..click on Main.storyboard -> go to identity and type... you can see your localize languages string...Image of your Main.strings -> select them select all Languages..

after that you can see main.strings(French) and main.strings(English) in your Navigation panel...

change your strings on prefer languages like...

Image of how to change your strings in English

Image of how to change your strings in French

now, change Your Simulator Language from Edit schema -> Run -> Your language(French or English).

it's working..

Milan Varasada
  • 300
  • 1
  • 11