Skip to content

Commit

Permalink
fix: solve SPM localization issue (#145)
Browse files Browse the repository at this point in the history
* fix: support static lib localization (#141)

* fix: add default value for localized string

* fix: add more default values for localized strings

* fix: support static lib localization

* Fix spm localization (#144)

* fix: support static lib localization (#141) (#142)

* fix: add default value for localized string

* fix: add more default values for localized strings

* fix: support static lib localization

* chore: release 1.7.4 (#143)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: bumped version to 1.7.4

* chore: add resources to Swift Package Manager file

* fix: remove some information for a test

* chore: add resources to Package.swift

* chore: exclue info.plist

* fix: update exclude Info.plist path

* fix: search "Resources" if "MantisResources" no found

* fix: Change MantisResource to MantisResources

* fix: support SPM based on if defined MANTIS_SPM

* fix: fix a compile error when using SPM

* chore: add supporting static Frameworks in README

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: add a space after exclude:

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
guoyingtao and github-actions[bot] committed Nov 6, 2021
1 parent 2f7b3d6 commit 8dedb8b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 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 = {
"MantisResource" => ["Sources/**/*.lproj/*.strings"]
"MantisResources" => ["Sources/**/*.lproj/*.strings"]
}

s.pod_target_xcconfig = {
Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PackageDescription

let package = Package(
name: "Mantis",
defaultLocalization: "en",
platforms: [.iOS(.v11), .macOS(.v10_15)],
products: [
.library(
Expand All @@ -14,6 +15,9 @@ let package = Package(
targets: [
.target(
name: "Mantis",
dependencies: [])
exclude: ["Resources/Info.plist"],
resources: [.process("Resources")],
swiftSettings: [.define("MANTIS_SPM")]
)
]
)
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ Please use the transformation infomation obtained previously from delegate metho
</p>

* SwiftUI project
please check this [link](https://github.com/guoyingtao/Mantis/discussions/123#discussioncomment-1127611)
Please check this [link](https://github.com/guoyingtao/Mantis/discussions/123#discussioncomment-1127611)

* Static frameworks
If you use static frameworks in CocoaPods, you need to add the code below in order to find the correct resource bundle.

```
Mantis.locateResourceBundle(by: Self.self)
```

* Custom localization tables and bundle

Expand Down
16 changes: 14 additions & 2 deletions Sources/Mantis/Helpers/LocalizedHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct LocalizedHelper {

static func setBundle(_ bundle: Bundle) {
guard let resourceBundleURL = bundle.url(
forResource: "MantisResource", withExtension: "bundle")
forResource: "MantisResources", withExtension: "bundle")
else { return }
LocalizedHelper.bundle = Bundle(url: resourceBundleURL)
}
Expand All @@ -25,16 +25,28 @@ struct LocalizedHelper {
) -> String {
let value = value ?? key

#if MANTIS_SPM
let bundle = localizationConfig.bundle ?? Bundle.module

return NSLocalizedString(
key,
tableName: localizationConfig.tableName,
bundle: bundle,
value: value,
comment: ""
)
#else
guard let bundle = LocalizedHelper.bundle ?? (localizationConfig.bundle ?? Mantis.bundle) else {
return value
}

return NSLocalizedString(
key,
tableName: localizationConfig.tableName,
bundle: bundle,
value: value,
comment: ""
)
#endif
}
}
2 changes: 1 addition & 1 deletion Sources/Mantis/Mantis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public struct Config {
return nil
}

if let url = bundle.url(forResource: "MantisResource", withExtension: "bundle") {
if let url = bundle.url(forResource: "MantisResources", withExtension: "bundle") {
let bundle = Bundle(url: url)
return bundle
}
Expand Down

0 comments on commit 8dedb8b

Please sign in to comment.