Skip to content

Commit

Permalink
[DEV][Added Localization Support]
Browse files Browse the repository at this point in the history
  • Loading branch information
developersancho committed Mar 7, 2022
1 parent 0e2d4c9 commit a6a7d69
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 22 deletions.
35 changes: 34 additions & 1 deletion SwiftRorty.iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
3155791E6AFC1A9297EC0C5C /* Pods_SwiftRorty_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 397CB4F3F68E35B07531DC81 /* Pods_SwiftRorty_iOS.framework */; };
4F65BBBCDA8F855D60E13350 /* Pods_SwiftRorty_iOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A7D41769FB9452A3D307BAEA /* Pods_SwiftRorty_iOSTests.framework */; };
B562E625E95EC19F9FE52064 /* Pods_SwiftRorty_iOS_SwiftRorty_iOSUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FF9784B384CDDC854807601 /* Pods_SwiftRorty_iOS_SwiftRorty_iOSUITests.framework */; };
E222E80327D693C900831125 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E222E80527D693C900831125 /* Localizable.strings */; };
E222E80727D6980600831125 /* LocalizedStringKeyExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E222E80627D6980600831125 /* LocalizedStringKeyExtension.swift */; };
E2291D3327A5E908006D3FD8 /* SwiftRorty_iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2291D3227A5E908006D3FD8 /* SwiftRorty_iOSApp.swift */; };
E2291D3527A5E908006D3FD8 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2291D3427A5E908006D3FD8 /* ContentView.swift */; };
E2291D3727A5E909006D3FD8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E2291D3627A5E909006D3FD8 /* Assets.xcassets */; };
Expand Down Expand Up @@ -54,6 +56,7 @@
E2900B9827D3F6290055AD96 /* VerticalToggleStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2900B9727D3F6290055AD96 /* VerticalToggleStyle.swift */; };
E2900B9A27D3F74A0055AD96 /* ColoredToggleStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2900B9927D3F74A0055AD96 /* ColoredToggleStyle.swift */; };
E2900B9C27D3FF720055AD96 /* UITabBarControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2900B9B27D3FF720055AD96 /* UITabBarControllerExtension.swift */; };
E2900B9E27D40EC70055AD96 /* FavoriteRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2900B9D27D40EC70055AD96 /* FavoriteRow.swift */; };
E2991AA927AC72ED00BF4B9A /* Status.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2991AA827AC72ED00BF4B9A /* Status.swift */; };
E2991AAF27AC740800BF4B9A /* LocationInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2991AAE27AC740800BF4B9A /* LocationInfo.swift */; };
E2991AB127AC760E00BF4B9A /* LocationResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2991AB027AC760E00BF4B9A /* LocationResponse.swift */; };
Expand Down Expand Up @@ -107,6 +110,8 @@
A7D41769FB9452A3D307BAEA /* Pods_SwiftRorty_iOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftRorty_iOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AA125747E2CA12BB1CC9DC61 /* Pods-SwiftRorty.iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftRorty.iOS.release.xcconfig"; path = "Target Support Files/Pods-SwiftRorty.iOS/Pods-SwiftRorty.iOS.release.xcconfig"; sourceTree = "<group>"; };
D591464920D24BEDACCC3F78 /* Pods-SwiftRorty.iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftRorty.iOS.debug.xcconfig"; path = "Target Support Files/Pods-SwiftRorty.iOS/Pods-SwiftRorty.iOS.debug.xcconfig"; sourceTree = "<group>"; };
E222E80427D693C900831125 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
E222E80627D6980600831125 /* LocalizedStringKeyExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedStringKeyExtension.swift; sourceTree = "<group>"; };
E2291D2F27A5E908006D3FD8 /* SwiftRorty.iOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftRorty.iOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
E2291D3227A5E908006D3FD8 /* SwiftRorty_iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftRorty_iOSApp.swift; sourceTree = "<group>"; };
E2291D3427A5E908006D3FD8 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -155,6 +160,7 @@
E2900B9727D3F6290055AD96 /* VerticalToggleStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VerticalToggleStyle.swift; sourceTree = "<group>"; };
E2900B9927D3F74A0055AD96 /* ColoredToggleStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColoredToggleStyle.swift; sourceTree = "<group>"; };
E2900B9B27D3FF720055AD96 /* UITabBarControllerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITabBarControllerExtension.swift; sourceTree = "<group>"; };
E2900B9D27D40EC70055AD96 /* FavoriteRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteRow.swift; sourceTree = "<group>"; };
E2991AA827AC72ED00BF4B9A /* Status.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Status.swift; sourceTree = "<group>"; };
E2991AAE27AC740800BF4B9A /* LocationInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationInfo.swift; sourceTree = "<group>"; };
E2991AB027AC760E00BF4B9A /* LocationResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationResponse.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -221,6 +227,14 @@
name = Frameworks;
sourceTree = "<group>";
};
E222E80027D693A900831125 /* languages */ = {
isa = PBXGroup;
children = (
E222E80527D693C900831125 /* Localizable.strings */,
);
path = languages;
sourceTree = "<group>";
};
E2291D2627A5E908006D3FD8 = {
isa = PBXGroup;
children = (
Expand All @@ -246,6 +260,7 @@
E2291D3127A5E908006D3FD8 /* SwiftRorty.iOS */ = {
isa = PBXGroup;
children = (
E222E80027D693A900831125 /* languages */,
E2AF0A3F27CD88EB0012EE85 /* extensions */,
E232FA1527CD63AB0073FCC8 /* SwiftRorty-iOS-Info.plist */,
E232FA0727CD4DAF0073FCC8 /* theme */,
Expand Down Expand Up @@ -414,6 +429,7 @@
E2291D6927A5EA77006D3FD8 /* favorites */ = {
isa = PBXGroup;
children = (
E2900B9D27D40EC70055AD96 /* FavoriteRow.swift */,
E2555C0827A87292007E5266 /* FavoritesScreen.swift */,
E2555C0A27A872A2007E5266 /* FavoritesViewModel.swift */,
);
Expand Down Expand Up @@ -578,6 +594,7 @@
E2B5A95A27D0223700C14287 /* UIApplicationExtension.swift */,
E2B5A95E27D0258200C14287 /* BundleExtension.swift */,
E2900B9B27D3FF720055AD96 /* UITabBarControllerExtension.swift */,
E222E80627D6980600831125 /* LocalizedStringKeyExtension.swift */,
);
path = extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -685,7 +702,7 @@
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
tr,
);
mainGroup = E2291D2627A5E908006D3FD8;
productRefGroup = E2291D3027A5E908006D3FD8 /* Products */;
Expand All @@ -707,6 +724,7 @@
E2291D3A27A5E90A006D3FD8 /* Preview Assets.xcassets in Resources */,
E232FA1227CD56780073FCC8 /* Raleway-Regular.ttf in Resources */,
E232FA0F27CD56780073FCC8 /* Raleway-SemiBold.ttf in Resources */,
E222E80327D693C900831125 /* Localizable.strings in Resources */,
E2291D3727A5E909006D3FD8 /* Assets.xcassets in Resources */,
E232FA1027CD56780073FCC8 /* Raleway-Medium.ttf in Resources */,
E232FA1127CD56780073FCC8 /* Raleway-Bold.ttf in Resources */,
Expand Down Expand Up @@ -847,6 +865,7 @@
E2991AB327AC766300BF4B9A /* EpisodeInfo.swift in Sources */,
E2512DB027C82B0000FC6129 /* KeyValueModel.swift in Sources */,
E2AD779A27CAC1E8006565C2 /* RestClient.swift in Sources */,
E222E80727D6980600831125 /* LocalizedStringKeyExtension.swift in Sources */,
E232FA0627CD4D570073FCC8 /* Endpoint.swift in Sources */,
E2D5A98327C96E3E0017DA32 /* APIMethod.swift in Sources */,
E232FA1B27CD6DC10073FCC8 /* FontTemplateModifier.swift in Sources */,
Expand All @@ -866,6 +885,7 @@
E2991AB127AC760E00BF4B9A /* LocationResponse.swift in Sources */,
E2555C0527A87238007E5266 /* CharacterRepository.swift in Sources */,
E2291D3527A5E908006D3FD8 /* ContentView.swift in Sources */,
E2900B9E27D40EC70055AD96 /* FavoriteRow.swift in Sources */,
E2555C0D27A872D5007E5266 /* SettingsScreen.swift in Sources */,
E2D5A98927C977A30017DA32 /* BaseError.swift in Sources */,
E2D5A98B27C977EF0017DA32 /* Decoder.swift in Sources */,
Expand Down Expand Up @@ -932,11 +952,23 @@
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
E222E80527D693C900831125 /* Localizable.strings */ = {
isa = PBXVariantGroup;
children = (
E222E80427D693C900831125 /* en */,
);
name = Localizable.strings;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */

/* Begin XCBuildConfiguration section */
E2291D5127A5E90A006D3FD8 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
Expand Down Expand Up @@ -998,6 +1030,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
Expand Down
1 change: 1 addition & 0 deletions SwiftRorty.iOS/SwiftRorty_iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct SwiftRorty_iOSApp: App {
var body: some Scene {
WindowGroup {
SplashScreen().preferredColorScheme(isDarkMode ? .dark : .light)
.environment(\.locale, .init(identifier: "en"))
}
}
}
37 changes: 37 additions & 0 deletions SwiftRorty.iOS/extensions/LocalizedStringKeyExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// LocalizedStringKeyExtension.swift
// SwiftRorty.iOS
//
// Created by developersancho on 7.03.2022.
//

import Foundation
import SwiftUI

extension LocalizedStringKey {

/**
Return localized value of thisLocalizedStringKey
*/
public func toString() -> String {
//use reflection
let mirror = Mirror(reflecting: self)

//try to find 'key' attribute value
let attributeLabelAndValue = mirror.children.first { (arg0) -> Bool in
let (label, _) = arg0
if(label == "key"){
return true;
}
return false;
}

if(attributeLabelAndValue != nil) {
//ask for localization of found key via NSLocalizedString
return String.localizedStringWithFormat(NSLocalizedString(attributeLabelAndValue!.value as! String, comment: ""));
}
else {
return "Swift LocalizedStringKey signature must have changed. @see Apple documentation."
}
}
}
25 changes: 25 additions & 0 deletions SwiftRorty.iOS/languages/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Localizable.strings
SwiftRorty.iOS

Created by developersancho on 7.03.2022.

*/

"bottom_menu_characters" = "Characters";
"bottom_menu_favorites" = "Favorites";
"bottom_menu_settings" = "Settings";
"toolbar_characters_title" = "Characters";
"toolbar_favorites_title" = "Favorites";
"toolbar_settings_title" = "Settings";
"toolbar_detail_title" = "Detail";

"text_theme_mode" = "Theme Mode";
"text_app_version" = "App Version";

"text_name" = "Name";
"text_species" = "Species";
"text_gender" = "Gender";
"text_last_know_location" = "Last Know Location";
"text_location" = "Location";

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct CharactersScreen: View {
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text("Characters").fontTemplate(AppFontTemplate.title)
Text(LocalizedStringKey("toolbar_characters_title")).fontTemplate(AppFontTemplate.title)
}
}
}.onAppear(perform: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct DetailScreen: View {
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text("Details").fontTemplate(AppFontTemplate.title)
Text(LocalizedStringKey("toolbar_detail_title")).fontTemplate(AppFontTemplate.title)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import UIKit
import Combine
import Resolver
import SwiftUI

class DetailViewModel: ObservableObject {
@Injected
Expand All @@ -35,35 +36,35 @@ class DetailViewModel: ObservableObject {
list.append(
KeyValueModel(
id: 0,
key: "Name",
key: LocalizedStringKey("text_name").toString(),
value: character.name
)
)
list.append(
KeyValueModel(
id: 1,
key: "Species",
key: LocalizedStringKey("text_species").toString(),
value: character.species
)
)
list.append(
KeyValueModel(
id: 2,
key: "Gender",
key: LocalizedStringKey("text_gender").toString(),
value: character.gender
)
)
list.append(
KeyValueModel(
id: 3,
key: "Last Know Location",
key: LocalizedStringKey("text_last_know_location").toString(),
value: character.origin?.name ?? "-"
)
)
list.append(
KeyValueModel(
id: 4,
key: "Location",
key: LocalizedStringKey("text_location").toString(),
value: character.location?.name ?? "-"
)
)
Expand Down
83 changes: 83 additions & 0 deletions SwiftRorty.iOS/presentation/features/favorites/FavoriteRow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// FavoriteRow.swift
// SwiftRorty.iOS
//
// Created by developersancho on 6.03.2022.
//

import SwiftUI

struct FavoriteRow: View {
var dto: CharacterDto

var body: some View {
HStack {
if let image = dto.image,
let url = URL(string: image) {
AsyncImage(url: url) { image in
image.resizable()
} placeholder: {
Color.gray
}
.aspectRatio(contentMode: .fit)
.frame(width: 80, height: 80)
.clipShape(RoundedRectangle(cornerRadius: 8))
} else {
RoundedRectangle(cornerRadius: 8)
.frame(width: 80, height: 80)
.foregroundColor(.gray)
}
VStack(alignment: .leading, spacing: 8) {
Text("\(dto.name ?? "Loading...")")
.fontTemplate(AppFontTemplate.body2)
.redacted(reason: dto.name == nil ? .placeholder : [])
Text("\(dto.species ?? "")")
.fontTemplate(AppFontTemplate.body2)
.redacted(reason: dto.species == nil ? .placeholder : [])
HStack {
LazyHStack {
if dto.status == Status.alive {
Circle()
.size(width: 12, height: 12)
.fill(Color.green)
} else if dto.status == Status.dead {
Circle()
.size(width: 12, height: 12)
.fill(Color.red)
} else {
Circle()
.size(width: 12, height: 12)
.fill(Color.gray)
}

Text("\(dto.status?.rawValue ?? "")")
.fontTemplate(AppFontTemplate.body3)
.redacted(reason: dto.status?.rawValue == nil ? .placeholder : [])
}.frame(
maxWidth: .infinity,
maxHeight: .infinity,
alignment: .leading
)
}
}
.padding(EdgeInsets(top: 8, leading: 0, bottom: 8, trailing: 0))
.frame(
maxWidth: .infinity,
maxHeight: .infinity,
alignment: .topLeading
)
}
.frame(width: .infinity)
.padding(10)
.background(Color.Card)
.cornerRadius(8)
.shadow(radius: 2)

}
}

struct FavoriteRow_Previews: PreviewProvider {
static var previews: some View {
FavoriteRow(dto: CharacterDto.defaultDto())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import SwiftUI
struct FavoritesScreen: View {
var body: some View {
NavigationView {
ZStack{
Color.Background.edgesIgnoringSafeArea(.all)
HStack {
Text("Favorites Screen")
.fontTemplate(AppFontTemplate.title)
}
ScrollView {
LazyVStack {
NavigationLink(
destination: DetailScreen(id: CharacterDto.defaultDto().id ?? 1),
label: {
FavoriteRow(dto: CharacterDto.defaultDto())
})

}.padding(10)
}
.background(Color.Background)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text("Favorites").fontTemplate(AppFontTemplate.title)
Text(LocalizedStringKey("toolbar_favorites_title")).fontTemplate(AppFontTemplate.title)
}
}
}
Expand Down
Loading

0 comments on commit a6a7d69

Please sign in to comment.