SwiftUI: LocalizedStringKey with indices
Here is a solution. Tested with Xcode 12.1 / iOS 14.1
ForEach (1..<4) { value in
Text(LocalizedStringKey(stringLiteral: "Question\(value)"))
}
NSLocalizedString and value with SwiftUI
I have finally found an answer right after posting my question:
// ContentView.swift
Text("3 Tomatoes")
/* or */
Text("\(tomatoes.count) Tomatoes")
/* replaced by */
Text(String(format: NSLocalizedString("%lld text-tomatoes", comment: ""), tomatoes.count))
// Localizable.strings (en)
"%lld text-tomatoes" = "%lld Tomatoes";
It's working like a charm!
Other example: Localization with String interpolation in SwiftUI
SwiftUI Text and LocalizedStringKey with parameter
Use this extension
extension String {
public func localized(with arguments: [CVarArg]) -> String {
return String(format: NSLocalizedString(self, comment: ""), locale: nil, arguments: arguments)
}
}
Usage :
Text(LocalizationWrapper.helloWorldWithParameter.localized(with: [name]))
Also, This approach is correct
static func helloWithParameter(parameter: String) -> LocalizedStringKey {
return "helloWorld \(parameter)"
}
SwiftUI Text doesn't localise when inside ForEach
It works when you hardcode the string in, because you are using this initialiser, which takes a LocalizedStringKey
.
LocalizedStringKey
is ExpressibleByStringLiteral
, but the word string
in Text(string)
is not a "string literal", so it calls this initialiser instead, and is not localised. You can initialise an instance of LocalizedStringKey
directly, in order to localise it:
Text(LocalizedStringKey(string))
SwiftUI Localisation not working with @State Strings
You can take convert the string into a NSLocalizedString
Text(NSLocalizedString(type, comment: ""))
or change the type of type
into a LocalizedStringKey
@State var type: LocalizedStringKey
How to sort a array with localized String in SwiftUI
Don't forget to provide a minimal reproducible example when you ask questions. You will get better and more focused answers. Below is one way you can do it.
class LocalizeTitleViewModel: ObservableObject {
@Published var items: [SampleItem] = [SampleItem(title: "orange", isFavorite: true), SampleItem(title: "pink", isFavorite: false), SampleItem(title: "red", isFavorite: true)]
@Published var sortedDown = false
@Published var showFavoritesOnly = false
var filteredItems: [SampleItem] {
var sortedItems: [SampleItem]
let filteredItems = items.filter { item in
(!showFavoritesOnly || item.isFavorite)
}
if sortedDown {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.localizedTitle > item2.localizedTitle
})
} else {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.localizedTitle < item2.localizedTitle
})
}
return sortedItems
}
}
struct LocalizeTitle: View {
@StateObject var modelData: LocalizeTitleViewModel = LocalizeTitleViewModel()
var body: some View {
NavigationView {
List {
Toggle(isOn: $modelData.showFavoritesOnly, label: {
Text("showFavorites")
})
ForEach(modelData.filteredItems) { (item) in
Text(item.localizedTitle)
}
}
}
}
}
struct SampleItem: Identifiable {
var id: UUID = UUID()
let title: String
var isFavorite: Bool
var localizedTitle: String{
get{
//You need a Localizable.strings file in your project that contains all your `title`
return NSLocalizedString(self.title, comment: "Sample Item title")
}
}
}
struct LocalizeTitle_Previews: PreviewProvider {
static var previews: some View {
LocalizeTitle()
}
}
Cast a '[String?]' to 'LocalizedStringKey' in SwiftUI
Your error stem from having [String?]
in the Label, where a Label takes just String/LocalizedStringKey
.
To fix your error you could do this:
if let telephone = emergency.police.all.compactMap{$0}, let firstNumber = telephone.first {
Label(firstNumber, systemImage: "phone.fill").font(.headline)
}
If you want all numbers to be displayed, use this:
let telephone = emergency.police.all.compactMap{$0}
ForEach(telephone, id: \.self) { phone in
Label(phone, systemImage: "phone.fill").font(.headline)
}
Related Topics
Testing If a Decimal Is a Whole Number in Swift
Disable Bounce Scrolling for Wkwebview in MACos
How to Release a Cgdataprovider in Swift on iOS
Implement Protocol Through Extension
Swift Lazy Stored Property Versus Regular Stored Property When Using Closure
Instance Member 'View' Cannot Be Used on Type 'Gamescene'
Comparing Non-Optional Any to Nil Is Always False
Why Is Uint64 Max Equal -1 in Swift
Swift Protocol Property in Protocol - Candidate Has Non-Matching Type
Xcode 9 Fails to Build Swift 4 Project with Pod
Nscollectionviewitem Never Instantiate
Swiftui Pass Two Child Views to View
How to Wait for Http Requests to Finish
Simple Observable Struct with Rxswift