Lokalisierung mit localizedStringResource

This commit is contained in:
Ingo Rohlf 2025-10-21 11:20:10 +02:00
parent 705479c710
commit eefe80b984
5 changed files with 64 additions and 6 deletions

41
Localizable.xcstrings Normal file
View file

@ -0,0 +1,41 @@
{
"sourceLanguage" : "en",
"strings" : {
"My Vehicle" : {
"extractionState" : "manual",
"localizations" : {
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "Mein Fahrzeug"
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "my Ride"
}
}
}
},
"Train %@ with %@ has ^[%lld minute](inflect: true) delay." : {
"comment" : "A localized string resource that describes a train with a bike. The first argument is the name of the train. The second argument is the description of the bike.",
"isCommentAutoGenerated" : true,
"localizations" : {
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "Zug %1$@ mit %2$@ hat ^[%3$lld Minute](inflect: true) Verspätung."
}
},
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Train %1$@ with %2$@ has ^[%3$lld minute](inflect: true) delay."
}
}
}
}
},
"version" : "1.1"
}

View file

@ -1,4 +1,4 @@
import Playgrounds
import Foundation
@ -6,12 +6,20 @@ protocol Vehicle {
var name: String { get }
var color: String { get set }
}
struct Train: Vehicle, CustomStringConvertible {
struct Train: Vehicle, CustomStringConvertible , CustomLocalizedStringResourceConvertible {
var name: String
var color: String
var delay: Int
var content: Bike
var description: String { "Zug \(name) mit \(content) hat \(delay) Minuten Verspätung" }
var description: String { "Zug \(name) mit \(content) hat \(delay) Minuten Verspätung" }
var localizedStringResource: LocalizedStringResource {
"Train \(name) with \(content) has ^[\(delay) minute](inflect: true) delay."
}
// init(describing obj: CustomLocalizedStringResourceConvertible){
// self.init(describing: obj.localizedStringResource)
// }
init(
name: String = "ICE",
color: String = "white",
@ -24,6 +32,7 @@ struct Train: Vehicle, CustomStringConvertible {
self.content = content()
}
}
struct Bike: Vehicle, CustomStringConvertible {
var name: String
var color: String

View file

@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
0CC14A872E92EC7B00271E8D /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A842E92EC7A00271E8D /* App.swift */; };
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A882E92EEA900271E8D /* Playground.swift */; };
FBA00D6D2EA78411006F8B9A /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */; };
FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA6FA5D2EA63EA300C373EC /* Models.swift */; };
FBA6FA602EA66C2E00C373EC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */; };
FBA6FA622EA76AAD00C373EC /* BikeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA6FA612EA76AAD00C373EC /* BikeView.swift */; };
@ -20,6 +21,7 @@
0CC14A762E92EC4700271E8D /* TheSwiftWeek.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TheSwiftWeek.app; sourceTree = BUILT_PRODUCTS_DIR; };
0CC14A842E92EC7A00271E8D /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
0CC14A882E92EEA900271E8D /* Playground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Playground.swift; sourceTree = "<group>"; };
FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
FBA6FA5D2EA63EA300C373EC /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
FBA6FA612EA76AAD00C373EC /* BikeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BikeView.swift; sourceTree = "<group>"; };
@ -48,6 +50,7 @@
FBA6FA612EA76AAD00C373EC /* BikeView.swift */,
FBA6FA652EA7725A00C373EC /* TrainView.swift */,
FBA6FA5D2EA63EA300C373EC /* Models.swift */,
FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */,
0CC14A772E92EC4700271E8D /* Products */,
);
sourceTree = "<group>";
@ -104,6 +107,7 @@
knownRegions = (
en,
Base,
de,
);
mainGroup = 0CC14A6D2E92EC4700271E8D;
minimizedProjectReferenceProxies = 1;
@ -122,6 +126,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FBA00D6D2EA78411006F8B9A /* Localizable.xcstrings in Resources */,
FBA6FA602EA66C2E00C373EC /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -204,6 +209,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 6.0;
};
@ -261,6 +267,7 @@
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 6.0;
VALIDATE_PRODUCT = YES;
};

View file

@ -4,13 +4,14 @@ import SwiftUI
struct TrainView: View {
var train = Train(name: "RE3412", color: "beige", delay: 45){ Bike.trek}
var train2 = Train(name: "RE1423", delay: 5){ Bike(name: "Bulls", color: "orange")}
var train = Train(name: "RE3412", color: "beige", delay: 2 ){ Bike.trek}
var train2 = Train(name: "RE1423", delay: 1){ Bike(name: "Bulls", color: "orange")}
var body: some View {
Text(describing: train)
Text(describing: train2)
Text(train.localizedStringResource)
Text(.myVehicle)
}
}