Use String Catalog and Localization with class and struct

Hi Everyone,

I was able to create the String Catalog with all my strings getting automatic into the stringCatalog except the strings from my models where is not swiftUI and where all I have a class with a lot of info for my app.

Some classes are short and I was able to just make the strings localizable by adding on every line: (String(localized: "Telefone"))

But I have one class which has Line: 1071 and Col: 1610 and every line I have 7 strings that needs to get localized. These 7 strings are repeated on every line.

So I was trying to create a localization for these 7 strings on this class without having to write (String(localized: "Telefone")) 7 times on every line.

is there a way?

Here is short version of my class:

import Foundation

class LensStructFilter: Identifiable {
    var description: String
    
    init(description: String) {
        self.description = description
    }
}

let lensEntriesFilter: [LensStructFilter] = [

    LensStructFilter(description: "Focal: 24mm \nAbertura Máxima: F2.8 \nCobertura: FULL FRAME \nBocal: Nikon F \nFoco Mínimo: 0,30m \nDiâmetro Frontal: 52mm \nPeso: 275g \n\nFocal: 35mm \nAbertura Máxima: F2.0 \nCobertura: FULL FRAME \nBocal: Nikon F \nFoco Mínimo: 0,25m \nDiâmetro Frontal: 52mm \nPeso: 205g \n\nFocal: 50mm \nAbertura Máxima: F1.8 \nCobertura: FULL FRAME \nBocal: Nikon F \nFoco Mínimo: 0,45m \nDiâmetro Frontal: 52mm \nPeso: 185g \n\nFocal: 85mm \nAbertura Máxima: F1.8 \nCobertura: FULL FRAME \nBocal: Nikon F \nFoco Mínimo: 0,80m \nDiâmetro Frontal: 67mm \nPeso: 350g \n\nFocal: 105mm MACRO \nAbertura Máxima: F2.8 \nCobertura: FULL FRAME \nBocal: Nikon F \nFoco Mínimo: 0,31m \nDiâmetro Frontal: 62mm \nPeso: 720g"),

    LensStructFilter(description: "Focal: 16-35mm  \nAbertura Máxima: F2.8 \nCobertura: FULL FRAME  \nBocal: EF \nFoco Mínimo: 0,28m \nDiâmetro Frontal (rosca): 82mm \nPeso: 790Kg"),

Thanks

One option is to change the type of description from String to LocalizedStringResource.

That type is still expressible by string literals and string interpolation, but will automatically be extracted as localizable.

Then when you need to use the string you can pass the value of type LocalizedStringResource to String(localized:).

Thanks so much for the help. It kind of worked but the string catalog brought all the description as localizable. I just want these strings to be localizable

Focal, Abertura Maxima, Bocal, Cobertura, Diametro frontal, Peso, Foco Minimo

Is there a way to do that?

For now the only thing I added was:

class LensStructFilter: Identifiable {
    var description: LocalizedStringResource
    
    init(description: LocalizedStringResource) {
        self.description = description
    }
}

See screen attached. :

I found out how. I was able to do it creating a dictionary of the words I need it and making them localized

Here is how:

import Foundation

class LensStructFilter: Identifiable {
    var description: String
      
    init(description: String) {
        self.description = description
    }
    
    var localizedDescription: String {
        var result = description
        
        let terms: [String: LocalizedStringResource] = [
            "Focal": "Focal",
            "Abertura Máxima": "Abertura Máxima",
            "Bocal": "Bocal",
            "Cobertura": "Cobertura",
            "Diâmetro Frontal": "Diâmetro Frontal",
            "Peso": "Peso",
            "Foco Mínimo": "Foco Mínimo"
        ]
        
        for (originalTerm, resource) in terms {
            let localizedTerm = String(localized: resource)
            result = result.replacingOccurrences(of: originalTerm, with: localizedTerm)
        }
        
        return result
    }
}

Then on the ContentView where you want to use the description just change description to localizedDescription.

If there is another way please let me know.

Thanks

Use String Catalog and Localization with class and struct
 
 
Q