1

To demonstrate my SwiftUI localization problem I have prepared a simple test project at Github:

screenshot

It displays a list of games in the ContentView.swift:

List($vm.currentGames, id: \.self) { $gameNumber in
    NavigationLink(
            destination: GameView(gameNumber: gameNumber)
        ) {
        Text("game-number #\(gameNumber)")
    }
}

which is coming from the GamesViewModel.swift:

class GamesViewModel: ObservableObject /*, WebSocketDelegate */ {
    @Published var currentGames: [Int] = [20, 30]
    @Published var displayedGame: Int = 0

And for localization in DE, EN, RU I have added Localizable.strings:

"app-title" = "Wähle ein Spiel!";
"update-games" = "Spiele erneuern";
"join-random-game" = "Zufallsspiel beitreten";
"game-number #%d" = "Spiel #%d";

While the top 3 entries work well (you can see them as app title and the 2 button labels in the screenshot above), the last entry does not work (shown by the red arrow in the screenshot),

I have unsuccessfully tried many things:

  • Replaced %d by %lld
  • Removed the # char on both sides
  • Replaced game-number by just game
  • Tried to use Text(LocalizedStringKey("game-number #\(gameNumber)"))
  • Tried to use Text(String(format: NSLocalizedString("game-number #%d", comment: ""), gameNumber)) - always shows the EN string "Game #..."

Nothing has helped, what could be wrong here please?

UPDATE:

I have received nice suggestions at the Hacking with Swift forum.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • 1
    Use verbatim or LocalizedStringKey? Or are you saying that the other ones with a placeholder (and so a variable) works too? See https://developer.apple.com/documentation/swiftui/text – Larme Nov 13 '21 at 22:39
  • 1
    And `Text(LocalizedStringKey("game-number #\(gameModel.gid)"))` it doesn't fork either? – Larme Nov 14 '21 at 13:54
  • @Larme please take a look at my updated question! I have added a simple test project to it, to demo my problem. Unfortunately `Text(LocalizedStringKey("game-number #\(gameNumber)"))` does not help there either. – Alexander Farber Nov 14 '21 at 18:05
  • 1
    I'm not a SwiftUI user, so... But `Text(String(format: NSLocalizedString("game-number #%d", comment: ""), gameNumber))` seems to work... I don't know why though... – Larme Nov 14 '21 at 18:17
  • Not really localized, unfortunately - your suggestion `Text(String(format: NSLocalizedString("game-number #%d", comment: ""), gameNumber))` only displays the English string "Game" for me. Regardless of the language I select in the Menu. – Alexander Farber Nov 14 '21 at 19:10

1 Answers1

1

Probably there is a bug for number formats, at least below works for strings.

List($vm.currentGames, id: \.self) { $gameNumber in
    NavigationLink(
        destination: GameView(gameNumber: gameNumber)
    ) {
        Text("game-number #\(String(gameNumber))")  // << here !!
    }
}

with combination in .strings

"game-number #%@" = "Game #%@";
"game-number #%@" = "Spiel #%@";
"game-number #%@" = "Игра #%@";

Tested with Xcode 13 / iOS 15

demo

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690