Skip to content

Commit

Permalink
fix: support static lib localization (#141) (#142)
Browse files Browse the repository at this point in the history
* fix: add default value for localized string

* fix: add more default values for localized strings

* fix: support static lib localization
  • Loading branch information
guoyingtao committed Nov 5, 2021
1 parent d355578 commit fd2de0f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Mantis.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/guoyingtao/Mantis.git", :tag => "#{s.version}" }
s.source_files = "Sources/**/*.{h,swift}"
s.resource_bundles = {
"Resource" => ["Sources/**/*.lproj/*.strings"]
"MantisResource" => ["Sources/**/*.lproj/*.strings"]
}

s.pod_target_xcconfig = {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Mantis/CropViewController/CropToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class CropToolbar: UIView, CropToolbarProtocol {
}

private func createCancelButton() {
let cancelText = LocalizedHelper.getString("Mantis.Cancel")
let cancelText = LocalizedHelper.getString("Mantis.Cancel", value: "Cancel")

cancelButton = createOptionButton(withTitle: cancelText, andAction: #selector(cancel))
}
Expand All @@ -81,7 +81,7 @@ public class CropToolbar: UIView, CropToolbarProtocol {
resetButton = createOptionButton(withTitle: nil, andAction: #selector(reset))
resetButton?.setImage(image, for: .normal)
} else {
let resetText = LocalizedHelper.getString("Mantis.Reset")
let resetText = LocalizedHelper.getString("Mantis.Reset", value: "Reset")
resetButton = createOptionButton(withTitle: resetText, andAction: #selector(reset))
}
}
Expand All @@ -92,7 +92,7 @@ public class CropToolbar: UIView, CropToolbarProtocol {
}

private func createCropButton() {
let doneText = LocalizedHelper.getString("Mantis.Done")
let doneText = LocalizedHelper.getString("Mantis.Done", value: "Done")
cropButton = createOptionButton(withTitle: doneText, andAction: #selector(crop))
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/CropViewController/FixedRatioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FixedRatioManager {
}

if ratioOptions.contains(.square) {
let squareText = LocalizedHelper.getString("Mantis.Square")
let squareText = LocalizedHelper.getString("Mantis.Square", value: "Square")
let square = (squareText, 1.0, squareText, 1.0)
appendToTail(ratioItem: square)
}
Expand All @@ -45,7 +45,7 @@ class FixedRatioManager {
}

func getOriginalRatioItem() -> RatioItemType {
let originalText = LocalizedHelper.getString("Mantis.Original")
let originalText = LocalizedHelper.getString("Mantis.Original", value: "Original")
return (originalText, originalRatioH, originalText, originalRatioH)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropViewController/RatioPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RatioPresenter {

actionSheet.handlePopupInBigScreenIfNeeded(sourceView: sourceView)

let cancelText = LocalizedHelper.getString("Mantis.Cancel")
let cancelText = LocalizedHelper.getString("Mantis.Cancel", value: "Cancel")
let cancelAction = UIAlertAction(title: cancelText, style: .cancel)
actionSheet.addAction(cancelAction)

Expand Down
11 changes: 10 additions & 1 deletion Sources/Mantis/Helpers/LocalizedHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@
import Foundation

struct LocalizedHelper {
private static var bundle: Bundle?

static func setBundle(_ bundle: Bundle) {
guard let resourceBundleURL = bundle.url(
forResource: "MantisResource", withExtension: "bundle")
else { return }
LocalizedHelper.bundle = Bundle(url: resourceBundleURL)
}

static func getString(
_ key: String,
localizationConfig: LocalizationConfig = Mantis.localizationConfig,
value: String? = nil
) -> String {
let value = value ?? key

guard let bundle = localizationConfig.bundle ?? Mantis.bundle else {
guard let bundle = LocalizedHelper.bundle ?? (localizationConfig.bundle ?? Mantis.bundle) else {
return value
}

Expand Down
13 changes: 8 additions & 5 deletions Sources/Mantis/Mantis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import UIKit

private(set) var bundle: Bundle? = {
return Mantis.Config.bundle
} ()
}()

internal var localizationConfig = LocalizationConfig()

Expand All @@ -48,6 +48,10 @@ public func cropCustomizableViewController(image: UIImage,
cropToolbar: cropToolbar)
}

public func locateResourceBundle(by hostClass: AnyClass) {
LocalizedHelper.setBundle(Bundle(for: hostClass))
}

public func getCroppedImage(byCropInfo info: CropInfo, andImage image: UIImage) -> UIImage? {
return image.getCroppedImage(byCropInfo: info)
}
Expand Down Expand Up @@ -168,19 +172,19 @@ public struct Config {

static private var bundleIdentifier: String = {
return "com.echo.framework.Mantis"
} ()
}()

static private(set) var bundle: Bundle? = {
guard let bundle = Bundle(identifier: bundleIdentifier) else {
return nil
}

if let url = bundle.url(forResource: "Resource", withExtension: "bundle") {
if let url = bundle.url(forResource: "MantisResource", withExtension: "bundle") {
let bundle = Bundle(url: url)
return bundle
}
return nil
} ()
}()

public init() {
}
Expand All @@ -203,4 +207,3 @@ public struct Config {
}
}
}

0 comments on commit fd2de0f

Please sign in to comment.