Search code examples
swiftlistswiftuilocalizationlocalizable.strings

Localizable.strings not working inside a List


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.


Solution

  • 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