Skip to content
13 changes: 9 additions & 4 deletions Sources/BuilderIO/BuilderIOManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ public final class BuilderIOManager {

private let apiKey: String
public let customNavigationScheme: String
public let locale: String?

private static var registered = false

public static func configure(apiKey: String, customNavigationScheme: String = "builderio") {
public static func configure(
apiKey: String, customNavigationScheme: String = "builderio", locale: String? = nil
) {
guard _shared == nil else {
print(
"Warning: BuilderIOManager has already been configured. Ignoring subsequent configuration.")
return
}
Font.registerFonts()
_shared = BuilderIOManager(apiKey: apiKey, customNavigationScheme: customNavigationScheme)
_shared = BuilderIOManager(
apiKey: apiKey, customNavigationScheme: customNavigationScheme, locale: locale)
}

// MARK: - Private Initialization

private init(apiKey: String, customNavigationScheme: String) {
private init(apiKey: String, customNavigationScheme: String, locale: String?) {
self.apiKey = apiKey
self.customNavigationScheme = customNavigationScheme
self.locale = locale

if !Self.registered {
BuilderComponentRegistry.shared.initialize()
Expand Down Expand Up @@ -60,7 +65,7 @@ public final class BuilderIOManager {
model: model,
apiKey: apiKey,
url: resolvedUrl,
locale: "",
locale: locale ?? "",
preview: ""
) {
return .success(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ extension BuilderViewProtocol {
func codeBindings() -> [String: String]? {
return nil
}

func localize(localizedValue: AnyCodable) -> String? {

if let localeDictionary = localizedValue.dictionaryValue {
if let currentLocale = BuilderIOManager.shared.locale {
if let localizedString = localeDictionary[currentLocale]?.stringValue {
return localizedString
}
}

return localeDictionary["Default"]?.stringValue

} else {
return localizedValue.stringValue
}
}
}

struct BuilderEmptyView: BuilderViewProtocol {
Expand Down
10 changes: 7 additions & 3 deletions Sources/BuilderIO/Components/BuilderImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct BuilderImage: BuilderViewProtocol {
var block: BuilderBlockModel
var children: [BuilderBlockModel]?

var imageURL: URL?
var imageURL: URL? = nil
var aspectRatio: CGFloat? = nil
var lockAspectRatio: Bool = false
var contentMode: ContentMode = .fit
Expand All @@ -16,8 +16,12 @@ struct BuilderImage: BuilderViewProtocol {

init(block: BuilderBlockModel) {
self.block = block
self.imageURL = URL(
string: block.component?.options?.dictionaryValue?["image"]?.stringValue ?? "")

if let imageLink = block.component?.options?.dictionaryValue?["image"] {
self.imageURL = URL(
string: localize(localizedValue: imageLink) ?? "")
}

if let ratio = block.component?.options?.dictionaryValue?["aspectRatio"]?.doubleValue {
self.aspectRatio = CGFloat(1 / ratio)
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/BuilderIO/Components/BuilderText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ struct BuilderText: BuilderViewProtocol {

init(block: BuilderBlockModel) {
self.block = block
self.text = block.component?.options?.dictionaryValue?["text"]?.stringValue ?? ""

if let textValue = block.component?.options?.dictionaryValue?["text"] {
self.text = localize(localizedValue: textValue) ?? ""
}

self.responsiveStyles = getFinalStyle(responsiveStyles: block.responsiveStyles)

if let textBinding = block.codeBindings(for: "text") {
Expand Down
Loading