Populating Localizable.strings file with Terminal genstring command in SwiftUI
Asked Answered
M

1

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

Milreis answered 29/5, 2020 at 18:37 Comment(0)
S
7

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
Standish answered 29/5, 2020 at 19:1 Comment(3)
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...Milreis
@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.Standish
OK, after running pl < Localizable.strings, as suggested by Jerome here, I found out the issue was just a missing ";"... Thanks for the help.Milreis

© 2022 - 2024 — McMap. All rights reserved.