5

Trying to localize a SwiftUI app, I was under the impression that things had gotten a lot simpler.
I created the Localizable.strings files for both of my supported languages, but I can't populate them with the usual Terminal genstring command : genstrings -o en.lproj *.swift, even though I am launching this command from the correct directory.

Does anyone know if genstring is broken in swiftUI or if there is another way ? Would HATE to have to go through the hundred strings in my app...

I did find many articles online about localization in SwiftUI, but nothing that clearly explains it.

My understanding is that in SwiftUI, we can simply write things like

Text("A text to be displayed")and this would be understood as a LocalizedStringKeyby default.

Am I wrong ?

Added Screenshot of issue after trying @Asperi's solution :

enter image description here

Asperi
  • 228,894
  • 20
  • 464
  • 690
Esowes
  • 287
  • 2
  • 13

1 Answers1

6

Use -SwiftUI option (as in variant below, or any other used)

$ find ./ -name "*.swift" -print0 | xargs -0 genstrings -a -SwiftUI -o en.lproj

for details

$ man genstrings
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thanks again Asperi, The strings were generated, but they are greyed, not red as usual and the build fails with this warning : could not decode input file using specified encoding: Unicode (UTF-8). I did set the text Encoding to UTF 8 in Xcode though. Screenshot added to question... – Esowes May 29 '20 at 20:19
  • @Esowes, this might be due to conflict of encodings in existed & newly generated .strings files. Try to store somewhere existed Localizable.strings (if any present) files and regenerate from scratch *without* option `-a`, then copy-paste old content using Xcode - it automatically detects encodings. – Asperi May 30 '20 at 08:25
  • OK, after running `pl < Localizable.strings`, as suggested by Jerome [here](https://stackoverflow.com/a/57491174/8736449), I found out the issue was just a missing ";"... Thanks for the help. – Esowes May 31 '20 at 05:42